Skip to content

Commit

Permalink
feat(): separate question for breaking changes
Browse files Browse the repository at this point in the history
Automatic header for breaking changes.
Config option to allow breaking changes question only for
certain types.

Closes #5
  • Loading branch information
Guria committed Jan 8, 2016
1 parent 866284d commit ad46da6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 24 deletions.
4 changes: 3 additions & 1 deletion cz-config-EXAMPLE.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ module.exports = {
{name: 'e2eTest'},
{name: 'unitTest'}
]
}
},

allowBreakingChanges: ['feat', 'fix']

};
30 changes: 22 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ var wrap = require('word-wrap');
var SYMLINK_CONFIG_NAME = 'cz-config';
var log = require('winston');


/* istanbul ignore next */
function readConfigFile() {
// this function is replaced in test.
Expand All @@ -16,15 +15,14 @@ function readConfigFile() {
// This file is a symlink to the real one usually placed in the root of your project.
config = require('./' + SYMLINK_CONFIG_NAME);
} catch (err) {
log.warn('You don\'t have a file "' + SYMLINK_CONFIG_NAME + '" in your porject root directory. We will use the default configuration file inside this directory: ' + __dirname);
log.warn('You don\'t have a file "' + SYMLINK_CONFIG_NAME + '" in your project root directory. We will use the default configuration file inside this directory: ' + __dirname);
log.warn('You should go to your "node_modules/cz-customizable" and run "npm run postinstall" to fix it up. Please report on Github if this doenst work.');

config = require('./cz-config-EXAMPLE');
}
return config;
}


function buildCommit(answers) {
var maxLineWidth = 100;

Expand Down Expand Up @@ -52,12 +50,16 @@ function buildCommit(answers) {
var body = wrap(answers.body, wrapOptions) || '';
body = body.split('|').join('\n');

var breaking = wrap(answers.breaking, wrapOptions);
var footer = wrap(answers.footer, wrapOptions);

var result = head;
if (body) {
result += '\n\n' + body;
}
if (breaking) {
result += '\n\n' + 'BREAKING CHANGE:\n' + breaking;
}
if (footer) {
result += '\n\n' + footer;
}
Expand All @@ -69,7 +71,6 @@ var isNotWip = function(answers) {
return answers.type.toLowerCase() !== 'wip';
};


module.exports = {

prompter: function(cz, commit) {
Expand All @@ -85,7 +86,6 @@ module.exports = {
message: '\nSelect the type of change that you\'re committing:',
choices: config.types
},

{
type: 'list',
name: 'scope',
Expand All @@ -105,14 +105,28 @@ module.exports = {
validate: function(value) {
return !!value;
}
}, {
},
{
type: 'input',
name: 'body',
message: '\nProvide a LONGER description of the change (optional). Use "|" to break new line:\n'
}, {
},
{
type: 'input',
name: 'breaking',
message: '\nList any BREAKING CHANGES (optional):\n',
when: function(answers) {
if (config.allowBreakingChanges) {
return config.allowBreakingChanges.indexOf(answers.type.toLowerCase()) >= 0;
}

return true;
}
},
{
type: 'input',
name: 'footer',
message: '\nList any BREAKING CHANGES or ISSUES CLOSED by this change (optional):\n',
message: '\nList any ISSUES CLOSED by this change (optional):\n',
when: isNotWip
},
{
Expand Down
43 changes: 28 additions & 15 deletions spec/czCustomizableSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ describe('cz-customizable', function() {
scopes: [{name: 'myScope'}],
scopeOverrides: {
fix: [{name: 'fixOverride'}]
}
},
allowBreakingChanges: ['feat']
};
}
});
Expand Down Expand Up @@ -62,14 +63,20 @@ describe('cz-customizable', function() {
expect(getQuestion(4).type).toEqual('input');

//question 5
expect(getQuestion(5).name).toEqual('footer');
expect(getQuestion(5).name).toEqual('breaking');
expect(getQuestion(5).type).toEqual('input');
expect(getQuestion(5).when({type: 'fix'})).toEqual(true);
expect(getQuestion(5).when({type: 'WIP'})).toEqual(false);
expect(getQuestion(5).when({type: 'feat'})).toEqual(true);
expect(getQuestion(5).when({type: 'fix'})).toEqual(false);

//question 6, last one
expect(getQuestion(6).name).toEqual('confirmCommit');
expect(getQuestion(6).type).toEqual('confirm');
//question 6
expect(getQuestion(6).name).toEqual('footer');
expect(getQuestion(6).type).toEqual('input');
expect(getQuestion(6).when({type: 'fix'})).toEqual(true);
expect(getQuestion(6).when({type: 'WIP'})).toEqual(false);

//question 7, last one
expect(getQuestion(7).name).toEqual('confirmCommit');
expect(getQuestion(7).type).toEqual('confirm');


var answers = {
Expand All @@ -78,10 +85,10 @@ describe('cz-customizable', function() {
scope: 'myScope',
subject: 'create a new cool feature'
};
expect(getQuestion(6).message(answers)).toMatch('Are you sure you want to proceed with the commit above?');
expect(getQuestion(7).message(answers)).toMatch('Are you sure you want to proceed with the commit above?');
});

it('should call not call commit() function if there is no final confirmation', function() {
it('should not call commit() function if there is no final confirmation', function() {
module.prompter(cz, commit);
var commitAnswers = cz.prompt.mostRecentCall.args[1];
var res = commitAnswers({});
Expand All @@ -100,11 +107,12 @@ describe('cz-customizable', function() {
scope: 'myScope',
subject: 'create a new cool feature',
body: '-line1|-line2',
breaking: 'breaking',
footer: 'my footer'
};

commitAnswers(answers);
expect(commit).toHaveBeenCalledWith('feat(myScope): create a new cool feature\n\n-line1\n-line2\n\nmy footer');
expect(commit).toHaveBeenCalledWith('feat(myScope): create a new cool feature\n\n-line1\n-line2\n\nBREAKING CHANGE:\nbreaking\n\nmy footer');
});

it('should call commit() function with commit message with the minimal required fields', function() {
Expand All @@ -129,11 +137,11 @@ describe('cz-customizable', function() {
var answers = {
confirmCommit: true,
type: 'WIP',
subject: 'this is my worl-in-progress'
subject: 'this is my work-in-progress'
};

commitAnswers(answers);
expect(commit).toHaveBeenCalledWith('WIP: this is my worl-in-progress');
expect(commit).toHaveBeenCalledWith('WIP: this is my work-in-progress');
});

it('should truncate first line if number of characters is higher than 200', function() {
Expand Down Expand Up @@ -169,8 +177,7 @@ describe('cz-customizable', function() {
});



describe('optional fixOverride', function() {
describe('optional fixOverride and allowBreakingChanges', function() {

beforeEach(function() {
module.__set__({
Expand Down Expand Up @@ -207,13 +214,19 @@ describe('cz-customizable', function() {
expect(getQuestion(2).when({type: 'WIP'})).toEqual(false);
expect(getQuestion(2).when({type: 'wip'})).toEqual(false);

//question 5
expect(getQuestion(5).name).toEqual('breaking');
expect(getQuestion(5).when({type: 'feat'})).toEqual(true);
expect(getQuestion(5).when({type: 'fix'})).toEqual(true);
expect(getQuestion(5).when({type: 'FIX'})).toEqual(true);

var answers = {
confirmCommit: true,
type: 'feat',
scope: 'myScope',
subject: 'create a new cool feature'
};
expect(getQuestion(6).message(answers)).toMatch('Are you sure you want to proceed with the commit above?');
expect(getQuestion(7).message(answers)).toMatch('Are you sure you want to proceed with the commit above?');
});
});

Expand Down

0 comments on commit ad46da6

Please sign in to comment.