forked from commitizen/cz-conventional-changelog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
243 lines (226 loc) · 8.07 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
const wrap = require('word-wrap');
const chalk = require('chalk');
const { getPackagesSync } = require("@lerna/project");
const shell = require('shelljs');
const path = require('path');
const filter = (array) => array.filter((x) => x);
const headerLength = (answers) => answers.type.length + 2 + (answers.scope ? answers.scope.length + 2 : 0);
const maxSummaryLength = (options, answers) => options.maxHeaderWidth - headerLength(answers);
const filterSubject = (subject, disableSubjectLowerCase) => {
subject = subject.trim();
if (!disableSubjectLowerCase && subject.charAt(0).toLowerCase() !== subject.charAt(0)) {
subject =
subject.charAt(0).toLowerCase() + subject.slice(1, subject.length);
}
while (subject.endsWith('.')) {
subject = subject.slice(0, subject.length - 1);
}
return subject;
};
const getChangedPackages = (allPackages) => {
const changedFiles = shell.exec('git diff --cached --name-only', { silent: true })
.stdout
.split('\n')
.map(path.normalize);
return allPackages
.filter(function (pkg) {
const packagePrefix = path.relative('.', pkg.location) + path.sep;
for (let changedFile of changedFiles) {
if (changedFile.indexOf(packagePrefix) === 0) {
return true;
}
}
})
.map(function (pkg) {
return pkg.name
});
}
function makeAffectsLine(answers) {
const selectedPackages = answers.packages;
if (selectedPackages && selectedPackages.length) {
return `affects: ${selectedPackages.join(', ')}`;
}
}
const packages = (() => {
const pkgs = getPackagesSync();
return {
all: pkgs.map(p => p.name),
changed: getChangedPackages(pkgs)
}
})();
const options = {
maxHeaderWidth: 100,
disableSubjectLowerCase: false
}
module.exports = {
// When a user runs `git cz`, prompter will
// be executed. We pass you cz, which currently
// is just an instance of inquirer.js. Using
// this you can ask questions and get answers.
//
// The commit callback should be executed when
// you're ready to send back a commit template
// to git.
//
// By default, we'll de-indent your commit
// template and will keep empty lines.
prompter(cz, commit) {
// Let's ask some questions of the user
// so that we can populate our commit
// template.
//
// See inquirer.js docs for specifics.
// You can also opt to use another input
// collection library if you prefer.
cz.prompt([
{
type: 'list',
name: 'type',
message: "Select the type of change that you're committing:",
choices: [
{value: 'fix', name: `fix: 🛠 Bug fix (note: indicate a ${chalk.green('minor')} release)`},
{value: 'feat', name: `feat: ✨ Feature (note: indicate a ${chalk.red('major')} release)`},
{value: 'docs', name: 'docs: Documentation only changes'},
{value: 'style', name: 'style: Changes that do not affect the meaning of the code'},
{value: 'refactor', name: 'refactor: A code change that neither fixes a bug nor adds a feature'},
{value: 'perf', name: 'perf: A code change that improves performance'},
{value: 'test', name: 'test: Adding missing tests'},
{value: 'chore', name: 'chore: Changes to the build process or auxiliary tools'},
{value: 'revert', name: 'revert: Revert to a commit'},
{value: 'WIP', name: 'WIP: Work in progress'}
],
},
{
type: 'checkbox',
name: 'scope',
message: `The packages that this commit has affected (${packages.all.length} detected)`,
default: packages.changed,
choices: packages.all
},
{
type: 'input',
name: 'subject',
message: function(answers) {
return (
'Write a short, imperative tense description of the change (max ' +
maxSummaryLength(options, answers) +
' chars):\n'
);
},
default: options.defaultSubject,
validate: function(subject, answers) {
var filteredSubject = filterSubject(subject, options.disableSubjectLowerCase);
return filteredSubject.length == 0
? 'subject is required'
: filteredSubject.length <= maxSummaryLength(options, answers)
? true
: 'Subject length must be less than or equal to ' +
maxSummaryLength(options, answers) +
' characters. Current length is ' +
filteredSubject.length +
' characters.';
},
transformer: function(subject, answers) {
var filteredSubject = filterSubject(subject, options.disableSubjectLowerCase);
var color =
filteredSubject.length <= maxSummaryLength(options, answers)
? chalk.green
: chalk.red;
return color('(' + filteredSubject.length + ') ' + subject);
},
filter: function(subject) {
return filterSubject(subject, options.disableSubjectLowerCase);
}
},
{
type: 'input',
name: 'body',
message: 'Provide a longer description of the change: (press enter to skip)\n',
default: options.defaultBody
},
{
type: 'confirm',
name: 'isBreaking',
message: 'Are there any breaking changes?',
default: false
},
{
type: 'input',
name: 'breakingBody',
default: '-',
message:
'A BREAKING CHANGE commit requires a body. Please enter a longer description of the commit itself:\n',
when: function(answers) {
return answers.isBreaking && !answers.body;
},
validate: function(breakingBody, answers) {
return (
breakingBody.trim().length > 0 ||
'Body is required for BREAKING CHANGE'
);
}
},
{
type: 'input',
name: 'breaking',
message: 'Describe the breaking changes:\n',
when: function(answers) {
return answers.isBreaking;
}
},
{
type: 'confirm',
name: 'isIssueAffected',
message: 'Does this change affect any open issues?',
default: options.defaultIssues ? true : false
},
{
type: 'input',
name: 'issuesBody',
default: '-',
message:
'If issues are closed, the commit requires a body. Please enter a longer description of the commit itself:\n',
when: function(answers) {
return (
answers.isIssueAffected && !answers.body && !answers.breakingBody
);
}
},
{
type: 'input',
name: 'issues',
message: 'Add issue references (e.g. "fix #123", "re #123".):\n',
when: function(answers) {
return answers.isIssueAffected;
},
default: options.defaultIssues ? options.defaultIssues : undefined
}
]).then(function(answers) {
var wrapOptions = {
trim: true,
cut: false,
newline: '\n',
indent: '',
width: options.maxLineWidth
};
// parentheses are only needed when a scope is present
var scope = answers.scope ? '(' + answers.scope + ')' : '';
// Hard limit this line in the validate
var head = answers.type + scope + ': ' + answers.subject;
const affectsLine = makeAffectsLine(answers.scope);
if (affectsLine) {
answers.body = `${affectsLine}\n` + answers.body;
}
// Wrap these lines at options.maxLineWidth characters
var body = answers.body ? wrap(answers.body, wrapOptions) : false;
// Apply breaking change prefix, removing it if already present
var breaking = answers.breaking ? answers.breaking.trim() : '';
breaking = breaking
? 'BREAKING CHANGE: ' + breaking.replace(/^BREAKING CHANGE: /, '')
: '';
breaking = breaking ? wrap(breaking, wrapOptions) : false;
var issues = answers.issues ? wrap(answers.issues, wrapOptions) : false;
commit(filter([head, body, breaking, issues]).join('\n\n'));
});
}
};