Skip to content

Commit

Permalink
fix: fixed invalid custom scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
XPoet committed Aug 3, 2022
1 parent b3cc538 commit e4c0aff
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 30 deletions.
4 changes: 4 additions & 0 deletions .cz-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ module.exports = {
},

allowCustomScopes: true,
customScopesName: 'customScopes',
allowEmptyScopes: true,
emptyScopesName: 'emptyScopes',

allowBreakingChanges: ['feat', 'fix'],
// skip any questions you want
skipQuestions: ['body'],
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ Here are the options you can set in your `.cz-config.js`:
}
```
* **allowCustomScopes**: {boolean, default false}: adds the option `custom` to scope selection so you can still type a scope if you need.
* **allowEmptyScopes**: {boolean, default false}: allow non-selection or not to fill in the scopes.
* **customScopesName**: {string, default 'custom scopes'}: custom scopes label in terminal scopes list.
* **emptyScopesName**: {string, default 'empty scopes'}: empty scopes label in terminal scopes list.
* **allowBreakingChanges**: {Array of Strings: default none}. List of commit types you would like to the question `breaking change` prompted. Eg.: ['feat', 'fix'].
* **skipQuestions**: {Array of Strings: default none}. List of questions you want to skip. Eg.: ['body', 'footer'].
* **skipEmptyScopes**: {boolean, default false}: If a chosen type has no scopes declared, skip the scope question
Expand Down
3 changes: 3 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ declare module "cz-customizable" {
confirmCommit?: string,
};
allowCustomScopes?: boolean;
customScopesName?: string;
allowEmptyScopes?: boolean;
emptyScopesName?: string;
allowBreakingChanges?: string[];
skipQuestions?: string[];
appendBranchNameToCommitMessage?: boolean;
Expand Down
19 changes: 11 additions & 8 deletions lib/build-commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@ const addTicketNumber = (ticketNumber, config) => {
return `${ticketNumber.trim()} `;
};

const addScope = (scope, config) => {
const addScope = (scope, customScope, config) => {
const separator = _.get(config, 'subjectSeparator', defaultSubjectSeparator);

if (!scope) return separator; // it could be type === WIP. So there is no scope
if (customScope) {
scope = customScope;
}

if (!scope || scope === 'empty') {
return separator;
}

return `(${scope.trim()})${separator}`;
};

const addSubject = subject => _.trim(subject);
const addSubject = (subject) => _.trim(subject);

const addType = (type, config) => {
const prefix = _.get(config, 'typePrefix', '');
Expand All @@ -35,10 +41,7 @@ const addType = (type, config) => {
};

const addBreaklinesIfNeeded = (value, breaklineChar = defaultBreaklineChar) =>
value
.split(breaklineChar)
.join('\n')
.valueOf();
value.split(breaklineChar).join('\n').valueOf();

const addFooter = (footer, config) => {
if (config && config.footerPrefix === '') return `\n\n${footer}`;
Expand Down Expand Up @@ -74,7 +77,7 @@ module.exports = (answers, config) => {
// eslint-disable-next-line max-len
const head =
addType(answers.type, config) +
addScope(answers.scope, config) +
addScope(answers.scope, answers.customScope, config) +
addTicketNumber(answers.ticketNumber, config) +
addSubject(answers.subject.slice(0, config.subjectLimit));

Expand Down
52 changes: 30 additions & 22 deletions lib/questions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const _ = require('lodash');
const buildCommit = require('./build-commit');
const log = require('./logger');

const isNotWip = answers => answers.type.toLowerCase() !== 'wip';
const isNotWip = (answers) => answers.type.toLowerCase() !== 'wip';

const isValidateTicketNo = (value, config) => {
if (!value) {
Expand All @@ -13,13 +13,10 @@ const isValidateTicketNo = (value, config) => {
return true;
}
const reg = new RegExp(config.ticketNumberRegExp);
if (value.replace(reg, '') !== '') {
return false;
}
return true;
return value.replace(reg, '') === '';
};

const getPreparedCommit = context => {
const getPreparedCommit = (context) => {
let message = null;
if (fs.existsSync('./.git/COMMIT_EDITMSG')) {
let preparedCommit = fs.readFileSync('./.git/COMMIT_EDITMSG', 'utf-8');
Expand Down Expand Up @@ -81,39 +78,52 @@ module.exports = {
message: messages.scope,
choices(answers) {
let scopes = [];

if (scopeOverrides[answers.type]) {
scopes = scopes.concat(scopeOverrides[answers.type]);
} else {
scopes = scopes.concat(config.scopes);
}

if (config.allowCustomScopes || scopes.length === 0) {
scopes = scopes.concat([
new cz.Separator(),
{ name: 'empty', value: false },
{ name: 'custom', value: 'custom' },
{ value: 'custom', name: config.customScopesName || 'custom scopes' },
]);
}

if (config.allowEmptyScopes || scopes.length === 0) {
if (!config.allowCustomScopes) {
scopes = scopes.concat([new cz.Separator()]);
}
scopes = scopes.concat([{ value: 'empty', name: config.emptyScopesName || 'empty scopes' }]);
}

if (config.allowCustomScopes || config.allowEmptyScopes) {
scopes = scopes.concat([new cz.Separator()]);
}

return scopes;
},
when(answers) {
let hasScope = false;

if (scopeOverrides[answers.type]) {
hasScope = !!(scopeOverrides[answers.type].length > 0);
hasScope = scopeOverrides[answers.type].length > 0;
} else {
hasScope = !!(config.scopes && config.scopes.length > 0);
hasScope = config.scopes && config.scopes.length > 0;
}

if (!hasScope) {
// TODO: Fix when possible
// eslint-disable-next-line no-param-reassign
answers.scope = skipEmptyScopes ? '' : 'custom';
return false;
}
return isNotWip(answers);

return hasScope && isNotWip(answers);
},
},
{
type: 'input',
name: 'scope',
name: 'customScope',
message: messages.customScope,
when(answers) {
return answers.scope === 'custom';
Expand Down Expand Up @@ -160,13 +170,11 @@ module.exports = {
message: messages.breaking,
when(answers) {
// eslint-disable-next-line max-len
if (
return !!(
config.askForBreakingChangeFirst ||
(config.allowBreakingChanges && config.allowBreakingChanges.indexOf(answers.type.toLowerCase()) >= 0)
) {
return true;
}
return false; // no breaking changes allowed unless specifed
);
// no breaking changes allowed unless specifed
},
},
{
Expand All @@ -192,10 +200,10 @@ module.exports = {
},
];

questions = questions.filter(item => !skipQuestions.includes(item.name));
questions = questions.filter((item) => !skipQuestions.includes(item.name));

if (config.askForBreakingChangeFirst) {
const isBreaking = oneQuestion => oneQuestion.name === 'breaking';
const isBreaking = (oneQuestion) => oneQuestion.name === 'breaking';

const breakingQuestion = _.filter(questions, isBreaking);
const questionWithoutBreaking = _.reject(questions, isBreaking);
Expand Down

0 comments on commit e4c0aff

Please sign in to comment.