Skip to content

Commit 12d9156

Browse files
committed
feat: implement subject feedback, scope filtering, subject filtering and config through package.json
cz-conventional-changelog can now have its values (specifically all the defaults and maxLineWidth) through the config.commitizen key in the package.json file. The scope now automatically becomes lowercase (as is required for conventional changelogs) and is prompted on the same line (as it is always short and doesn't need an additional line). The subject question now indicates the total number of characters that are allowed based upon the maxLineWidth configuration (or 100 as it is now by default), the length of the type, and scope. Validation prevents entering more than the allowed number of characters with feedback of the number of characters entered. Subject will always have a lowercase first letter and strip any trailing dots (as is required by the conventional changelog standard). 'commitizen' and 'semantic-release' have been updated to the most recent versions (because of current vunderabilites) and the .travisci file has been updated to reflect newer node versions.
1 parent 65d6a9f commit 12d9156

File tree

5 files changed

+7934
-34
lines changed

5 files changed

+7934
-34
lines changed

.travis.yml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
sudo: false
22
language: node_js
3-
cache:
4-
directories:
5-
- node_modules
63
notifications:
74
email: false
85
node_js:
9-
- '4'
10-
before_install:
11-
- npm i -g npm@^2.0.0
6+
- '6'
7+
- '8'
8+
- '10'
129
before_script:
1310
- npm prune
1411
after_success:

engine.js

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,32 @@ var wrap = require('word-wrap');
44
var map = require('lodash.map');
55
var longest = require('longest');
66
var rightPad = require('right-pad');
7+
var chalk = require('chalk')
78

89
var filter = function(array) {
910
return array.filter(function(x) {
1011
return x;
1112
});
1213
};
1314

15+
var headerLength = function(answers) {
16+
return answers.type.length + 2 + (answers.scope ? answers.scope.length + 2 : 0)
17+
}
18+
19+
var maxSummaryLength = function(options, answers) {
20+
return options.maxHeaderWidth - headerLength(answers)
21+
}
22+
var filterSubject = function(subject) {
23+
subject = subject.trim();
24+
if (subject.charAt(0).toLowerCase() !== subject.charAt(0)) {
25+
subject = subject.charAt(0).toLowerCase() + subject.slice(1, subject.length);
26+
}
27+
while (subject.endsWith('.')) {
28+
subject = subject.slice(0, subject.length - 1)
29+
}
30+
return subject
31+
}
32+
1433
// This can be any kind of SystemJS compatible module.
1534
// We use Commonjs here, but ES6 or AMD would do just
1635
// fine.
@@ -26,6 +45,7 @@ module.exports = function (options) {
2645
};
2746
});
2847

48+
2949
return {
3050
// When a user runs `git cz`, prompter will
3151
// be executed. We pass you cz, which currently
@@ -39,7 +59,7 @@ module.exports = function (options) {
3959
// By default, we'll de-indent your commit
4060
// template and will keep empty lines.
4161
prompter: function(cz, commit) {
42-
console.log('\nLine 1 will be cropped at 100 characters. All other lines will be wrapped after 100 characters.\n');
62+
console.log('\nLine 1 will have the maximum length of ' + options.maxHeaderWidth + ' characters (enforced). All other lines will be wrapped after ' + options.maxLineWidth + ' characters.\n');
4363

4464
// Let's ask some questions of the user
4565
// so that we can populate our commit
@@ -58,13 +78,32 @@ module.exports = function (options) {
5878
}, {
5979
type: 'input',
6080
name: 'scope',
61-
message: 'What is the scope of this change (e.g. component or file name)? (press enter to skip)\n',
62-
default: options.defaultScope
81+
message: 'What is the scope of this change (e.g. component or file name): (press enter to skip)',
82+
default: options.defaultScope,
83+
filter: function(value) {
84+
return value.trim().toLowerCase();
85+
}
6386
}, {
6487
type: 'input',
6588
name: 'subject',
66-
message: 'Write a short, imperative tense description of the change:\n',
67-
default: options.defaultSubject
89+
message: function (answers) {
90+
return 'Write a short, imperative tense description of the change (max ' + maxSummaryLength(options, answers) + ' chars):\n'
91+
},
92+
default: options.defaultSubject,
93+
validate: function(subject, answers) {
94+
var filteredSubject = filterSubject(subject)
95+
return filteredSubject.length == 0 ? 'Subject is required' :
96+
filteredSubject.length <= maxSummaryLength(options, answers) ? true :
97+
'Subject length must be less than or equal to ' + maxSummaryLength(options, answers) + ' characters. Current length is ' + filteredSubject.length + ' characters.';
98+
},
99+
transformer: function(subject, answers) {
100+
var filteredSubject = filterSubject(subject)
101+
var color = filteredSubject.length <= maxSummaryLength(options, answers) ? chalk.green : chalk.red
102+
return color('(' + filteredSubject.length + ') ' + subject)
103+
},
104+
filter: function(subject) {
105+
return filterSubject(subject)
106+
}
68107
}, {
69108
type: 'input',
70109
name: 'body',
@@ -98,35 +137,30 @@ module.exports = function (options) {
98137
}
99138
]).then(function(answers) {
100139

101-
var maxLineWidth = 100;
102-
103140
var wrapOptions = {
104141
trim: true,
105142
newline: '\n',
106143
indent:'',
107-
width: maxLineWidth
144+
width: options.maxLineWidth
108145
};
109146

110147
// parentheses are only needed when a scope is present
111-
var scope = answers.scope.trim();
112-
scope = scope ? '(' + answers.scope.trim() + ')' : '';
148+
var scope = answers.scope ? '(' + answers.scope + ')' : '';
113149

114-
// Hard limit this line
115-
var head = (answers.type + scope + ': ' + answers.subject.trim()).slice(0, maxLineWidth);
150+
// Hard limit this line in the validate
151+
var head = answers.type + scope + ': ' + answers.subject;
116152

117-
// Wrap these lines at 100 characters
118-
var body = wrap(answers.body, wrapOptions);
153+
// Wrap these lines at 72 characters
154+
var body = answers.body ? wrap(answers.body, wrapOptions) : false;
119155

120156
// Apply breaking change prefix, removing it if already present
121157
var breaking = answers.breaking ? answers.breaking.trim() : '';
122158
breaking = breaking ? 'BREAKING CHANGE: ' + breaking.replace(/^BREAKING CHANGE: /, '') : '';
123-
breaking = wrap(breaking, wrapOptions);
124-
125-
var issues = answers.issues ? wrap(answers.issues, wrapOptions) : '';
159+
breaking = breaking ? wrap(breaking, wrapOptions) : false;
126160

127-
var footer = filter([ breaking, issues ]).join('\n\n');
161+
var issues = answers.issues ? wrap(answers.issues, wrapOptions) : false;
128162

129-
commit(head + '\n\n' + body + '\n\n' + footer);
163+
commit(filter([ head, body, breaking, issues ]).join('\n\n'));
130164
});
131165
}
132166
};

index.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
var engine = require('./engine');
44
var conventionalCommitTypes = require('conventional-commit-types');
5+
var configLoader = require('commitizen').configLoader
6+
7+
var config = configLoader.load()
58

69
module.exports = engine({
710
types: conventionalCommitTypes.types,
8-
defaultType: process.env.CZ_TYPE,
9-
defaultScope: process.env.CZ_SCOPE,
10-
defaultSubject: process.env.CZ_SUBJECT,
11-
defaultBody: process.env.CZ_BODY,
12-
defaultIssues: process.env.CZ_ISSUES
11+
defaultType: process.env.CZ_TYPE || config.defaultType,
12+
defaultScope: process.env.CZ_SCOPE || config.defaultScope,
13+
defaultSubject: process.env.CZ_SUBJECT || config.defaultSubject,
14+
defaultBody: process.env.CZ_BODY || config.defaultBody,
15+
defaultIssues: process.env.CZ_ISSUES || config.defaultIssues,
16+
maxHeaderWidth: process.env.CZ_MAX_LINE_WIDTH || config.maxHeaderWidth || 100,
17+
maxLineWidth: process.env.CZ_MAX_BODY_WIDTH || config.maxLineWidth || 100,
1318
});

0 commit comments

Comments
 (0)