Skip to content

Commit

Permalink
feat: allow to edit commit message in $EDITOR
Browse files Browse the repository at this point in the history
  • Loading branch information
Guria committed Jan 11, 2016
1 parent 0c3971a commit baf473d
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 15 deletions.
35 changes: 29 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
var wrap = require('word-wrap');
var SYMLINK_CONFIG_NAME = 'cz-config';
var log = require('winston');
var editor = require('editor');
var temp = require('temp').track();
var fs = require('fs');

/* istanbul ignore next */
function readConfigFile() {
Expand Down Expand Up @@ -164,22 +167,42 @@ module.exports = {
when: isNotWip
},
{
type: 'confirm',
type: 'expand',
name: 'confirmCommit',
choices: [
{ key: 'y', name: 'Yes', value: 'yes' },
{ key: 'n', name: 'Abort commit', value: 'no' },
{ key: 'e', name: 'Edit message', value: 'edit' }
],
message: function(answers) {
var SEP = '###--------------------------------------------------------###';
log.info('\n' + SEP + '\n' + buildCommit(answers) + '\n' + SEP + '\n');
return 'Are you sure you want to proceed with the commit above?';
}
}
], function(answers) {
if (!answers.confirmCommit) {
if (answers.confirmCommit === 'edit') {
temp.open(null, function(err, info) {
/* istanbul ignore else */
if (!err) {
fs.write(info.fd, buildCommit(answers));
fs.close(info.fd, function(err) {
editor(info.path, function (code, sig) {
if (code === 0) {
var commitStr = fs.readFileSync(info.path, { encoding: 'utf8' });
commit(commitStr);
} else {
log.info('Editor returned non zero value. Commit message was:\n' + buildCommit(answers));
}
});
});
}
});
} else if (answers.confirmCommit === 'yes') {
commit(buildCommit(answers));
} else {
log.info('Commit has been canceled.');
return;
}

var commitStr = buildCommit(answers);
commit(commitStr);
});
}
};
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
],
"license": "MIT",
"dependencies": {
"word-wrap": "1.1.0",
"winston": "2.1.0"
"editor": "^1.0.0",
"temp": "^0.8.3",
"winston": "2.1.0",
"word-wrap": "1.1.0"
},
"devDependencies": {
"codecov.io": "0.1.6",
Expand Down
50 changes: 43 additions & 7 deletions spec/czCustomizableSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ describe('cz-customizable', function() {

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


var answers = {
confirmCommit: true,
confirmCommit: 'yes',
type: 'feat',
scope: 'myScope',
subject: 'create a new cool feature'
Expand All @@ -110,7 +110,7 @@ describe('cz-customizable', function() {
var commitAnswers = cz.prompt.mostRecentCall.args[1];

var answers = {
confirmCommit: true,
confirmCommit: 'yes',
type: 'feat',
scope: 'myScope',
subject: 'create a new cool feature',
Expand All @@ -128,7 +128,7 @@ describe('cz-customizable', function() {
var commitAnswers = cz.prompt.mostRecentCall.args[1];

var answers = {
confirmCommit: true,
confirmCommit: 'yes',
type: 'feat',
scope: 'myScope',
subject: 'create a new cool feature'
Expand All @@ -143,7 +143,7 @@ describe('cz-customizable', function() {
var commitAnswers = cz.prompt.mostRecentCall.args[1];

var answers = {
confirmCommit: true,
confirmCommit: 'yes',
type: 'WIP',
subject: 'this is my work-in-progress'
};
Expand All @@ -152,14 +152,50 @@ describe('cz-customizable', function() {
expect(commit).toHaveBeenCalledWith('WIP: this is my work-in-progress');
});

it('should allow edit message before commit', function(done) {
module.prompter(cz, commit);
var commitAnswers = cz.prompt.mostRecentCall.args[1];
process.env.EDITOR = 'true';

var answers = {
confirmCommit: 'edit',
type: 'feat',
subject: 'create a new cool feature'
};

commitAnswers(answers);
setTimeout(function() {
expect(commit).toHaveBeenCalledWith('feat: create a new cool feature');
done();
}, 100);
});

it('should not commit if editor returned non-zero value', function(done) {
module.prompter(cz, commit);
var commitAnswers = cz.prompt.mostRecentCall.args[1];
process.env.EDITOR = 'false';

var answers = {
confirmCommit: 'edit',
type: 'feat',
subject: 'create a new cool feature'
};

commitAnswers(answers);
setTimeout(function() {
expect(commit.wasCalled).toEqual(false);
done();
}, 100);
});

it('should truncate first line if number of characters is higher than 200', function() {
module.prompter(cz, commit);
var commitAnswers = cz.prompt.mostRecentCall.args[1];

var chars_100 = '0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789-0123456789';

var answers = {
confirmCommit: true,
confirmCommit: 'yes',
type: 'feat',
scope: 'myScope',
subject: chars_100,
Expand Down Expand Up @@ -229,7 +265,7 @@ describe('cz-customizable', function() {
expect(getQuestion(6).when({type: 'FIX'})).toEqual(true);

var answers = {
confirmCommit: true,
confirmCommit: 'yes',
type: 'feat',
scope: 'myScope',
subject: 'create a new cool feature'
Expand Down

0 comments on commit baf473d

Please sign in to comment.