Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed invalid custom scopes #203

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
},
"plugins": ["prettier"],
"rules": {

"no-param-reassign": "off"
}
}
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think allowEmptyScopes default should be true because it makes it easier for people to use this tool. What are your thoughts?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we make allowEmptyScopes default true, would that be a breaking change? No problems if so, we just need to make the commit text have:

BREAKING CHANGES: 
- describe the breaking change
- steps to upgrade

* **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) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could even consider calling this addScopeIfNeeded

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may not need this comment anymore with the direct function return.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you can remove comments

},
},
{
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