Skip to content

Commit

Permalink
fix: update this module to work with latest commitizen v2.8.1
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
you need to update commitizen to v2.8.1, which uses inquireJs v1.0.2. From now on commitizen uses
promise for the commit callback. As a user of this adapter, all you have to do is update commitizen.

ISSUES CLOSED: 30
  • Loading branch information
Leonardo Correa committed May 11, 2016
1 parent cf6f8e8 commit 35ab0b7
Show file tree
Hide file tree
Showing 7 changed files with 389 additions and 358 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Suitable for large teams working with multiple projects with their own commit sc


## Steps:
* install commitizen in case you don't have it: `npm install -g commitizen`
* install commitizen in case you don't have it: `npm install -g commitizen`. Make sure you have version `2.8.1`+.
* install the cz-customizable: `npm install cz-customizable --save-dev`
* configure `commitizen` to use `cz-customizable` as plugin. Add those lines to your `package.json`:

Expand Down
62 changes: 62 additions & 0 deletions buildCommit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'use strict';


var wrap = require('word-wrap');


module.exports = function buildCommit(answers) {

var maxLineWidth = 100;

var wrapOptions = {
trim: true,
newline: '\n',
indent:'',
width: maxLineWidth
};

function addScope(scope) {
if (!scope) return ': '; //it could be type === WIP. So there is no scope

return '(' + scope.trim() + '): ';
}

function addSubject(subject) {
return subject.trim();
}

function escapeSpecialChars(result) {
var specialChars = ['\`'];

specialChars.map(function (item) {
// For some strange reason, we have to pass additional '\' slash to commitizen. Total slashes are 4.
// If user types "feat: `string`", the commit preview should show "feat: `\\string\\`".
// Don't worry. The git log will be "feat: `string`"
result = result.replace(new RegExp(item, 'g'), '\\\\`');
});
return result;
}

// Hard limit this line
var head = (answers.type + addScope(answers.scope) + addSubject(answers.subject)).slice(0, maxLineWidth);

// Wrap these lines at 100 characters
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\nISSUES CLOSED: ' + footer;
}

return escapeSpecialChars(result);
};
174 changes: 6 additions & 168 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

var CZ_CONFIG_NAME = '.cz-config.js';
var CZ_CONFIG_EXAMPLE_LOCATION = './cz-config-EXAMPLE.js';
var wrap = require('word-wrap');
var findConfig = require('find-config');
var log = require('winston');
var editor = require('editor');
var temp = require('temp').track();
var fs = require('fs');
var path = require('path');
var buildCommit = require('./buildCommit');


/* istanbul ignore next */
function readConfigFile() {
Expand All @@ -32,188 +33,25 @@ function readConfigFile() {
var config = findConfig.require(CZ_CONFIG_NAME, {home: false});

if (config) {
console.info('>>> Using cz-customizable file ".cz-config.js"');
console.info('>>> cz-customizable config file has been found.');
return config;
}

log.warn('Unable to find a configuration file. Please refer to documentation to learn how to ser up: https://github.com/leonardoanalista/cz-customizable#steps "');

// if (!config) {
// log.warn('Unable to find a config file "' + CZ_CONFIG_NAME + '". Default configuration would be used.');
// log.warn('Copy and use it as template by running in a project root directory:\n "cp '
// + path.resolve(CZ_CONFIG_EXAMPLE_LOCATION) + ' ' + path.join('.', CZ_CONFIG_NAME) + '"');

// config = require(CZ_CONFIG_EXAMPLE_LOCATION);
// }
}

function buildCommit(answers) {
var maxLineWidth = 100;

var wrapOptions = {
trim: true,
newline: '\n',
indent:'',
width: maxLineWidth
};

function addScope(scope) {
if (!scope) return ': '; //it could be type === WIP. So there is no scope

return '(' + scope.trim() + '): ';
}

function addSubject(subject) {
return subject.trim();
}

function escapeSpecialChars(result) {
var specialChars = ['\`'];

specialChars.map(function (item) {
// For some strange reason, we have to pass additional '\' slash to commitizen. Total slashes are 4.
// If user types "feat: `string`", the commit preview should show "feat: `\\string\\`".
// Don't worry. The git log will be "feat: `string`"
result = result.replace(new RegExp(item, 'g'), '\\\\`');
});
return result;
}

// Hard limit this line
var head = (answers.type + addScope(answers.scope) + addSubject(answers.subject)).slice(0, maxLineWidth);

// Wrap these lines at 100 characters
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\nISSUES CLOSED: ' + footer;
}

return escapeSpecialChars(result);
}

var isNotWip = function(answers) {
return answers.type.toLowerCase() !== 'wip';
};

module.exports = {

prompter: function(cz, commit) {
var config = readConfigFile();
config.scopeOverrides = config.scopeOverrides || {};

log.info('\n\nLine 1 will be cropped at 100 characters. All other lines will be wrapped after 100 characters.\n');

cz.prompt([
{
type: 'list',
name: 'type',
message: 'Select the type of change that you\'re committing:',
choices: config.types
},
{
type: 'list',
name: 'scope',
message: '\nDenote the SCOPE of this change (optional):',
choices: function(answers) {
var scopes = [];
if (config.scopeOverrides[answers.type]) {
scopes = scopes.concat(config.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' }
]);
}
return scopes;
},
when: function(answers) {
var hasScope = false;
if (config.scopeOverrides[answers.type]) {
hasScope = !!(config.scopeOverrides[answers.type].length > 0);
} else {
hasScope = !!(config.scopes && (config.scopes.length > 0));
}
if (!hasScope) {
answers.scope = 'custom';
return false;
} else {
return isNotWip(answers);
}
}
},
{
type: 'input',
name: 'scope',
message: 'Denote the SCOPE of this change:',
when: function(answers) {
return answers.scope === 'custom';
}
},
{
type: 'input',
name: 'subject',
message: 'Write a SHORT, IMPERATIVE tense description of the change:\n',
validate: function(value) {
return !!value;
},
filter: function(value) {
return value.charAt(0).toLowerCase() + value.slice(1);
}
},
{
type: 'input',
name: 'body',
message: 'Provide a LONGER description of the change (optional). Use "|" to break new line:\n'
},
{
type: 'input',
name: 'breaking',
message: 'List any BREAKING CHANGES (optional):\n',
when: function(answers) {
if (config.allowBreakingChanges) {
return config.allowBreakingChanges.indexOf(answers.type.toLowerCase()) >= 0;
}
var questions = require('./questions').getQuestions(config, cz);

cz.prompt(questions).then(function(answers) {

return true;
}
},
{
type: 'input',
name: 'footer',
message: 'List any ISSUES CLOSED by this change (optional). E.g.: #31, #34:\n',
when: isNotWip
},
{
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 === 'edit') {
temp.open(null, function(err, info) {
/* istanbul ignore else */
Expand Down
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
],
"license": "MIT",
"dependencies": {
"editor": "^1.0.0",
"find-config": "^0.3.0",
"temp": "^0.8.3",
"editor": "1.0.0",
"find-config": "0.3.0",
"temp": "0.8.3",
"winston": "2.1.0",
"word-wrap": "1.1.0"
},
"devDependencies": {
"codecov.io": "0.1.6",
"commitizen": "^2.4.6",
"commitizen": "2.8.1",
"eslint": "1.9.0",
"ghooks": "1.0.0",
"istanbul": "0.4.0",
Expand All @@ -41,7 +41,10 @@
},
"config": {
"commitizen": {
"path": "."
"path": "./index.js"
},
"cz-customizable": {
"config": "cz-config-EXAMPLE.js"
},
"ghooks": {
"pre-commit": "npm run eslint && npm run test"
Expand Down
Loading

0 comments on commit 35ab0b7

Please sign in to comment.