Skip to content

Commit

Permalink
feat: add new option askForBreakingChangeFirst (#84)
Browse files Browse the repository at this point in the history
This will allow us to have the breaking change as first question

ISSUES CLOSED: 32
  • Loading branch information
leonardoanalista authored May 13, 2019
1 parent eee6212 commit 0ab06f2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ Here are the options you can set in your `.cz-config.js`:
* **breakingPrefix**: {string, default 'BREAKING CHANGE:'}: Set a custom prefix for the breaking change block in commit messages.
* **footerPrefix**: {string, default 'ISSUES CLOSED:'}: Set a custom prefix for the footer block in commit messages. Set to empty string to remove prefix.
* **breaklineChar**: {string, default '|'}: It gets replaced with \n to create the breakline in your commit message. This is supported for fields `body` and `footer` at the moment.
* **upperCaseSubject**: { boolean, default false }: Capitalizes first subject letter if set to `true`
* **upperCaseSubject**: { boolean, default false }: Capitalizes first subject letter if set to `true`
* **askForBreakingChangeFirst**: { boolean, default false }: It asks for breaking change as first question when set to `true`

## Related tools
- (https://github.com/commitizen/cz-cli)
Expand Down
3 changes: 2 additions & 1 deletion cz-config-EXAMPLE.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,6 @@ module.exports = {
// limit subject length
subjectLimit: 100,
// breaklineChar: '|', // It is supported for fields body and footer.
// footerPrefix : 'ISSUES CLOSED:', // default value
// footerPrefix : 'ISSUES CLOSED:'
// askForBreakingChangeFirst : true, // default is false
};
15 changes: 14 additions & 1 deletion questions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const _ = require('lodash');
const buildCommit = require('./buildCommit');
const log = require('./logger');

Expand Down Expand Up @@ -133,7 +134,10 @@ module.exports = {
message: messages.breaking,
when(answers) {
// eslint-disable-next-line max-len
if (config.allowBreakingChanges && config.allowBreakingChanges.indexOf(answers.type.toLowerCase()) >= 0) {
if (
config.askForBreakingChangeFirst ||
(config.allowBreakingChanges && config.allowBreakingChanges.indexOf(answers.type.toLowerCase()) >= 0)
) {
return true;
}
return false; // no breaking changes allowed unless specifed
Expand Down Expand Up @@ -164,6 +168,15 @@ module.exports = {

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

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

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

questions = _.concat(breakingQuestion, questionWithoutBreaking);
}

return questions;
},
};
15 changes: 12 additions & 3 deletions spec/questionsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,20 +183,30 @@ describe('cz-customizable', () => {
allowTicketNumber: false,
};

// question 4 with
expect(getQuestion(4).name).toEqual('ticketNumber');
expect(getQuestion(4).when()).toEqual(false);
});
});

describe('ask for breaking change first', () => {
it('when config askForBreakingChangeFirst is true', () => {
config = {
types: [{ value: 'feat', name: 'feat: my feat' }],
askForBreakingChangeFirst: true,
};

expect(getQuestion(1).name).toEqual('breaking');
expect(getQuestion(1).when()).toEqual(true);
});
});

describe('TicketNumber', () => {
it('disable TicketNumber question', () => {
config = {
types: [{ value: 'feat', name: 'feat: my feat' }],
allowTicketNumber: false,
};

// question 4 with
expect(getQuestion(4).name).toEqual('ticketNumber');
expect(getQuestion(4).when()).toEqual(false);
});
Expand All @@ -210,7 +220,6 @@ describe('cz-customizable', () => {
},
};

// question 4 with
expect(getQuestion(4).name).toEqual('ticketNumber');
expect(getQuestion(4).message).toEqual('ticket number');
});
Expand Down

0 comments on commit 0ab06f2

Please sign in to comment.