Skip to content

Commit 56c021a

Browse files
Merge pull request #31 from leonardoanalista/issue-30
fix: update this module to work with latest commitizen v2.8.1
2 parents cf6f8e8 + 35ab0b7 commit 56c021a

7 files changed

Lines changed: 389 additions & 358 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Suitable for large teams working with multiple projects with their own commit sc
1313

1414

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

buildCommit.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'use strict';
2+
3+
4+
var wrap = require('word-wrap');
5+
6+
7+
module.exports = function buildCommit(answers) {
8+
9+
var maxLineWidth = 100;
10+
11+
var wrapOptions = {
12+
trim: true,
13+
newline: '\n',
14+
indent:'',
15+
width: maxLineWidth
16+
};
17+
18+
function addScope(scope) {
19+
if (!scope) return ': '; //it could be type === WIP. So there is no scope
20+
21+
return '(' + scope.trim() + '): ';
22+
}
23+
24+
function addSubject(subject) {
25+
return subject.trim();
26+
}
27+
28+
function escapeSpecialChars(result) {
29+
var specialChars = ['\`'];
30+
31+
specialChars.map(function (item) {
32+
// For some strange reason, we have to pass additional '\' slash to commitizen. Total slashes are 4.
33+
// If user types "feat: `string`", the commit preview should show "feat: `\\string\\`".
34+
// Don't worry. The git log will be "feat: `string`"
35+
result = result.replace(new RegExp(item, 'g'), '\\\\`');
36+
});
37+
return result;
38+
}
39+
40+
// Hard limit this line
41+
var head = (answers.type + addScope(answers.scope) + addSubject(answers.subject)).slice(0, maxLineWidth);
42+
43+
// Wrap these lines at 100 characters
44+
var body = wrap(answers.body, wrapOptions) || '';
45+
body = body.split('|').join('\n');
46+
47+
var breaking = wrap(answers.breaking, wrapOptions);
48+
var footer = wrap(answers.footer, wrapOptions);
49+
50+
var result = head;
51+
if (body) {
52+
result += '\n\n' + body;
53+
}
54+
if (breaking) {
55+
result += '\n\n' + 'BREAKING CHANGE:\n' + breaking;
56+
}
57+
if (footer) {
58+
result += '\n\nISSUES CLOSED: ' + footer;
59+
}
60+
61+
return escapeSpecialChars(result);
62+
};

index.js

Lines changed: 6 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
var CZ_CONFIG_NAME = '.cz-config.js';
66
var CZ_CONFIG_EXAMPLE_LOCATION = './cz-config-EXAMPLE.js';
7-
var wrap = require('word-wrap');
87
var findConfig = require('find-config');
98
var log = require('winston');
109
var editor = require('editor');
1110
var temp = require('temp').track();
1211
var fs = require('fs');
1312
var path = require('path');
13+
var buildCommit = require('./buildCommit');
14+
1415

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

3435
if (config) {
35-
console.info('>>> Using cz-customizable file ".cz-config.js"');
36+
console.info('>>> cz-customizable config file has been found.');
3637
return config;
3738
}
3839

3940
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 "');
40-
41-
// if (!config) {
42-
// log.warn('Unable to find a config file "' + CZ_CONFIG_NAME + '". Default configuration would be used.');
43-
// log.warn('Copy and use it as template by running in a project root directory:\n "cp '
44-
// + path.resolve(CZ_CONFIG_EXAMPLE_LOCATION) + ' ' + path.join('.', CZ_CONFIG_NAME) + '"');
45-
46-
// config = require(CZ_CONFIG_EXAMPLE_LOCATION);
47-
// }
4841
}
4942

50-
function buildCommit(answers) {
51-
var maxLineWidth = 100;
52-
53-
var wrapOptions = {
54-
trim: true,
55-
newline: '\n',
56-
indent:'',
57-
width: maxLineWidth
58-
};
59-
60-
function addScope(scope) {
61-
if (!scope) return ': '; //it could be type === WIP. So there is no scope
62-
63-
return '(' + scope.trim() + '): ';
64-
}
65-
66-
function addSubject(subject) {
67-
return subject.trim();
68-
}
69-
70-
function escapeSpecialChars(result) {
71-
var specialChars = ['\`'];
72-
73-
specialChars.map(function (item) {
74-
// For some strange reason, we have to pass additional '\' slash to commitizen. Total slashes are 4.
75-
// If user types "feat: `string`", the commit preview should show "feat: `\\string\\`".
76-
// Don't worry. The git log will be "feat: `string`"
77-
result = result.replace(new RegExp(item, 'g'), '\\\\`');
78-
});
79-
return result;
80-
}
81-
82-
// Hard limit this line
83-
var head = (answers.type + addScope(answers.scope) + addSubject(answers.subject)).slice(0, maxLineWidth);
84-
85-
// Wrap these lines at 100 characters
86-
var body = wrap(answers.body, wrapOptions) || '';
87-
body = body.split('|').join('\n');
88-
89-
var breaking = wrap(answers.breaking, wrapOptions);
90-
var footer = wrap(answers.footer, wrapOptions);
91-
92-
var result = head;
93-
if (body) {
94-
result += '\n\n' + body;
95-
}
96-
if (breaking) {
97-
result += '\n\n' + 'BREAKING CHANGE:\n' + breaking;
98-
}
99-
if (footer) {
100-
result += '\n\nISSUES CLOSED: ' + footer;
101-
}
102-
103-
return escapeSpecialChars(result);
104-
}
105-
106-
var isNotWip = function(answers) {
107-
return answers.type.toLowerCase() !== 'wip';
108-
};
10943

11044
module.exports = {
11145

11246
prompter: function(cz, commit) {
11347
var config = readConfigFile();
114-
config.scopeOverrides = config.scopeOverrides || {};
11548

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

118-
cz.prompt([
119-
{
120-
type: 'list',
121-
name: 'type',
122-
message: 'Select the type of change that you\'re committing:',
123-
choices: config.types
124-
},
125-
{
126-
type: 'list',
127-
name: 'scope',
128-
message: '\nDenote the SCOPE of this change (optional):',
129-
choices: function(answers) {
130-
var scopes = [];
131-
if (config.scopeOverrides[answers.type]) {
132-
scopes = scopes.concat(config.scopeOverrides[answers.type]);
133-
} else {
134-
scopes = scopes.concat(config.scopes);
135-
}
136-
if (config.allowCustomScopes || scopes.length === 0) {
137-
scopes = scopes.concat([
138-
new cz.Separator(),
139-
{ name: 'empty', value: false },
140-
{ name: 'custom', value: 'custom' }
141-
]);
142-
}
143-
return scopes;
144-
},
145-
when: function(answers) {
146-
var hasScope = false;
147-
if (config.scopeOverrides[answers.type]) {
148-
hasScope = !!(config.scopeOverrides[answers.type].length > 0);
149-
} else {
150-
hasScope = !!(config.scopes && (config.scopes.length > 0));
151-
}
152-
if (!hasScope) {
153-
answers.scope = 'custom';
154-
return false;
155-
} else {
156-
return isNotWip(answers);
157-
}
158-
}
159-
},
160-
{
161-
type: 'input',
162-
name: 'scope',
163-
message: 'Denote the SCOPE of this change:',
164-
when: function(answers) {
165-
return answers.scope === 'custom';
166-
}
167-
},
168-
{
169-
type: 'input',
170-
name: 'subject',
171-
message: 'Write a SHORT, IMPERATIVE tense description of the change:\n',
172-
validate: function(value) {
173-
return !!value;
174-
},
175-
filter: function(value) {
176-
return value.charAt(0).toLowerCase() + value.slice(1);
177-
}
178-
},
179-
{
180-
type: 'input',
181-
name: 'body',
182-
message: 'Provide a LONGER description of the change (optional). Use "|" to break new line:\n'
183-
},
184-
{
185-
type: 'input',
186-
name: 'breaking',
187-
message: 'List any BREAKING CHANGES (optional):\n',
188-
when: function(answers) {
189-
if (config.allowBreakingChanges) {
190-
return config.allowBreakingChanges.indexOf(answers.type.toLowerCase()) >= 0;
191-
}
51+
var questions = require('./questions').getQuestions(config, cz);
52+
53+
cz.prompt(questions).then(function(answers) {
19254

193-
return true;
194-
}
195-
},
196-
{
197-
type: 'input',
198-
name: 'footer',
199-
message: 'List any ISSUES CLOSED by this change (optional). E.g.: #31, #34:\n',
200-
when: isNotWip
201-
},
202-
{
203-
type: 'expand',
204-
name: 'confirmCommit',
205-
choices: [
206-
{ key: 'y', name: 'Yes', value: 'yes' },
207-
{ key: 'n', name: 'Abort commit', value: 'no' },
208-
{ key: 'e', name: 'Edit message', value: 'edit' }
209-
],
210-
message: function(answers) {
211-
var SEP = '###--------------------------------------------------------###';
212-
log.info('\n' + SEP + '\n' + buildCommit(answers) + '\n' + SEP + '\n');
213-
return 'Are you sure you want to proceed with the commit above?';
214-
}
215-
}
216-
], function(answers) {
21755
if (answers.confirmCommit === 'edit') {
21856
temp.open(null, function(err, info) {
21957
/* istanbul ignore else */

package.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323
],
2424
"license": "MIT",
2525
"dependencies": {
26-
"editor": "^1.0.0",
27-
"find-config": "^0.3.0",
28-
"temp": "^0.8.3",
26+
"editor": "1.0.0",
27+
"find-config": "0.3.0",
28+
"temp": "0.8.3",
2929
"winston": "2.1.0",
3030
"word-wrap": "1.1.0"
3131
},
3232
"devDependencies": {
3333
"codecov.io": "0.1.6",
34-
"commitizen": "^2.4.6",
34+
"commitizen": "2.8.1",
3535
"eslint": "1.9.0",
3636
"ghooks": "1.0.0",
3737
"istanbul": "0.4.0",
@@ -41,7 +41,10 @@
4141
},
4242
"config": {
4343
"commitizen": {
44-
"path": "."
44+
"path": "./index.js"
45+
},
46+
"cz-customizable": {
47+
"config": "cz-config-EXAMPLE.js"
4548
},
4649
"ghooks": {
4750
"pre-commit": "npm run eslint && npm run test"

0 commit comments

Comments
 (0)