-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.js
298 lines (279 loc) · 9.45 KB
/
engine.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
'format cjs';
var wrap = require('word-wrap');
var map = require('lodash.map');
var longest = require('longest');
var chalk = require('chalk');
const { writeFileSync } = require('fs');
const { resolve } = require('path');
var filter = function (array) {
return array.filter(function (x) {
return x;
});
};
var headerLength = function (answers) {
return (
answers.type.length + 2 + (answers.scope ? answers.scope.length + 2 : 0)
);
};
var maxSummaryLength = function (options, answers) {
return options.maxHeaderWidth - headerLength(answers);
};
var filterSubject = function (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;
};
// This can be any kind of SystemJS compatible module.
// We use Commonjs here, but ES6 or AMD would do just
// fine.
module.exports = function (options) {
var types = options.types;
var length = longest(Object.keys(types)).length + 1;
writeFileSync(
resolve(__dirname, './src/shared-config.json'),
JSON.stringify(options),
{ encoding: 'utf-8' }
);
var choices = map(types, function (type) {
return {
name: (type.title + ':').padEnd(length) + ' ' + type.description,
value: type.title
};
});
return {
// 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: function (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: chalk.blue('本次提交在哪方面做出变动?'),
choices: choices,
default: options.defaultType
},
{
type: 'input',
name: 'scope',
message: '本次提交的变更的范围是?(按回车跳过)',
default: options.defaultScope,
filter: function (value) {
return options.disableScopeLowerCase
? value.trim()
: value.trim().toLowerCase();
}
},
{
type: 'input',
name: 'subject',
message: function (answers) {
return (
'编写简要的变更描述(最多 ' +
maxSummaryLength(options, answers) +
' 字符):\n'
);
},
default: options.defaultSubject,
validate: function (subject, answers) {
var filteredSubject = filterSubject(
subject,
options.disableSubjectLowerCase
);
return filteredSubject.length == 0
? '必须填写简要描述'
: filteredSubject.length <= maxSummaryLength(options, answers)
? true
: '简要描述的长度必须小于等于 ' +
maxSummaryLength(options, answers) +
' 字符,当前长度为 ' +
filteredSubject.length +
' 字符。';
},
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: '提供更详细的变更描述正文:(按回车跳过)',
default: options.defaultBody
},
{
type: 'list',
name: 'isBreaking',
message: chalk.red('是否存在破坏性变更?'),
choices: ['否', '是'],
filter: function (input) {
return input.includes('是') ? true : false;
}
},
{
type: 'input',
name: 'breakingBody',
default: '-',
message: '破坏性变更的提交需要提供更详细的描述正文:\n',
when: function (answers) {
return answers.isBreaking && !answers.body;
},
validate: function (breakingBody) {
return (
breakingBody.trim().length > 0 ||
chalk.red('必须填写破坏性变更的描述正文')
);
}
},
{
type: 'input',
name: 'breaking',
message: '请描述破坏性变更的内容:\n',
when: function (answers) {
return answers.isBreaking;
}
},
{
type: 'list',
name: 'isIssueAffected',
message: '本次变更是否影响到未关闭的问题(issue)?',
default: options.defaultIssues ? true : false,
choices: ['否', '是'],
filter: function (input) {
return input === '是' ? true : false;
}
},
{
type: 'input',
name: 'issuesBody',
default: '-',
message:
'如果问题(issue)已关闭,则本次提交需要提供更详细的描述正文:\n',
when: function (answers) {
return (
answers.isIssueAffected && !answers.body && !answers.breakingBody
);
}
},
{
type: 'input',
name: 'issues',
message: '添加对问题(issue)的引用(例如“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 + ')' : '';
// Add a expolaration mark if this commit has marked as BREAKING CHANGE
var colon = answers.isBreaking ? '!:' : ':';
// Hard limit this line in the validate
var head = answers.type + scope + colon + answers.subject;
// Wrap these lines at options.maxLineWidth characters.
// Also, fill the body with breakingBody only if the body is missing.
var body = answers.body
? wrap(answers.body, wrapOptions)
: wrap(answers.breakingBody, wrapOptions);
// Apply breaking change prefix, removing it if already present
var breaking = answers.breaking ? answers.breaking.trim() : '';
breaking = breaking
? '破坏性更新:' + breaking.replace(/^破坏性更新:/, '')
: '';
breaking = breaking ? wrap(breaking, wrapOptions) : false;
var issues = answers.issues ? wrap(answers.issues, wrapOptions) : false;
if (options.showConfirmPrompt)
confirmPrompt({ head, body, breaking, issues }, cz, commit);
else commit(filter([head, body, breaking, issues]).join('\n\n'));
});
function confirmPrompt(parts, cz, commit) {
const { head, body, breaking, issues } = parts;
const message = filter([head, body, breaking, issues]).join('\n\n');
const TITLE = ' 提交消息如下 👇 ';
const header = insertStr(
divider(message.length),
message.length / 2,
TITLE
);
const footer = divider(message.length + TITLE.length + 6);
console.clear();
console.log(chalk.green(header));
newLine();
console.log(`${message}`);
newLine();
console.log(chalk.green(footer));
cz.prompt([
{
type: 'list',
name: 'continue',
message: chalk.yellow('确认提交本次更改?'),
choices: ['是', '否'],
filter: (input) => (input === '是' ? true : false)
}
])
.then((answer) => {
if (answer.continue) return commit(message);
else return process.exit(1);
})
.catch(() => {
console.warn(
chalk.yellow('[cz-translated-changelog-zh-cn]:取消提交')
);
process.exit(1);
});
}
function divider(length, str = '') {
if (str.length < length) return divider(length, str.concat('-'));
else return str;
}
function insertStr(source, at, plugin) {
return source.slice(0, at).concat(plugin).concat(source.slice(at));
}
function newLine() {
return console.log('\n');
}
}
};
};