forked from leoforfree/cz-customizable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestions.js
More file actions
363 lines (345 loc) · 12.2 KB
/
Copy pathquestions.js
File metadata and controls
363 lines (345 loc) · 12.2 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/* eslint-disable no-control-regex */
const _ = require('lodash');
const fuzzy = require('fuzzy');
const chalk = require('chalk');
const fs = require('fs');
const { FilteredChoice } = require('inquirer-better-prompts');
const Choices = require('inquirer/lib/objects/choices');
const { Separator } = require('inquirer');
const buildCommit = require('./buildCommit');
const log = require('./logger');
const fuzzyOptions = {
pre: '<<',
post: '>>',
};
function fuzzyResultToDisplay(fuzzyString, originalString) {
const str = originalString.split('');
let currentMatch = -1;
const matchLength = fuzzyOptions.pre.length + fuzzyOptions.post.length;
// eslint-disable-next-line no-restricted-syntax
for (const match of fuzzyString.matchAll(/<<([^<>]*)>>/g)) {
let offset = 0;
let i = 0;
currentMatch += 1;
while (i < match.index) {
const exec = new RegExp(/^\u001b\[([0-9];?)+m/i).exec(originalString.slice(i + offset));
if (exec) {
offset += exec[0].length;
} else {
i += 1;
}
}
str.splice(match.index + offset + currentMatch * 2 - currentMatch * matchLength, 0, fuzzyOptions.pre);
str.splice(
match.index + offset + currentMatch * 2 - currentMatch * matchLength + match[1].length + 1,
0,
fuzzyOptions.post
);
}
return str.join('').replace(/<<([^<>]*)>>/g, chalk.bold.cyanBright('$1'));
}
function searchIssues(choices, answers, input) {
if ((input || '') === '') {
return null;
}
const realChoices = choices.realChoices.map((c, i) => new FilteredChoice(c, answers, i));
const lastCheckedIndex = realChoices.reduce((acc, choice, index) => (choice.checked ? index : acc), -1);
const fuzzyResult = fuzzy.filter(input, realChoices.slice(lastCheckedIndex + 1), {
...fuzzyOptions,
extract: choice => choice.name.replace(/\u001b\[([0-9];?)+m/gi, ''),
});
return new Choices(
realChoices
.slice(0, lastCheckedIndex + 1)
.map(c => {
const choice = c;
const match = fuzzy.match(input, choice.name, {
...fuzzyOptions,
extract: s => s.replace(/\u001b\[([0-9];?)+m/gi, ''),
});
choice.name = match ? fuzzyResultToDisplay(match.rendered, choice.name) : choice.name;
return choice;
})
.concat(
fuzzyResult.length && lastCheckedIndex >= 0 ? new Separator() : [],
fuzzyResult.map(el => {
const choice = el.original;
choice.name = fuzzyResultToDisplay(el.string, choice.name);
return choice;
})
)
);
}
const isNotWip = answers => answers.type.toLowerCase() !== 'wip';
const validateTicketNo = (type, value, config) => {
const required = config.isTicketNumberRequired;
if (!value.length && (required === true || (Array.isArray(required) && required.includes(type)))) {
return 'You must specify at least one ticket.';
}
if (!config.ticketNumberRegExp) {
return true;
}
if (
value
.join('')
.replace(new RegExp(config.ticketNumberRegExp, 'g'), '')
.replace(new RegExp(config.ticketNumberPrefix || '', 'g'), '') !== ''
) {
return `Tickets do not match format ${config.ticketNumberPrefix || ''}${config.ticketNumberRegExp}.`;
}
return true;
};
module.exports = {
getQuestions(config, cz) {
// normalize config optional options
const scopeOverrides = config.scopeOverrides || {};
const messages = config.messages || {};
const skipQuestions = config.skipQuestions || [];
messages.wip = messages.wip || 'Are you done with this change ?';
messages.type = messages.type || "Select the type of change that you're committing:";
messages.scope = messages.scope || 'Denote the SCOPE of this change (optional):\n';
messages.customScope = messages.customScope || 'Denote the SCOPE of this change:';
if (!messages.ticketNumber) {
messages.ticketNumber = 'Enter the ticket number:\n';
if (config.ticketNumberRegExp) {
messages.ticketNumber =
messages.ticketNumberPattern ||
`Enter the ticket number following this pattern (${config.ticketNumberRegExp}):\n`;
}
}
messages.subject = messages.subject || 'Write a SHORT, IMPERATIVE tense description of the change:\n';
messages.body =
messages.body || 'Provide a LONGER description of the change (optional). Use "|" to break new line:\n';
messages.breaking = messages.breaking || 'List any BREAKING CHANGES (optional):\n';
messages.footer = messages.footer || 'List any ISSUES CLOSED by this change (optional). E.g.: #31, #34:\n';
messages.confirmCommit = messages.confirmCommit || 'Are you sure you want to proceed with the commit above?';
const filePath = _.get(config, 'pathToJiraIssues', './.jira-issues-cache.json');
let issuesString = '[]';
try {
issuesString = fs.readFileSync(filePath, 'utf-8');
} catch (e) {
//
}
const issues = JSON.parse(issuesString).map(issue => {
const name = `${issue.key} - ${issue.summary}`.split('\n').flatMap(el =>
// el.match(/[^\s]+\s+/g).reduce((acc, word, index) => {
// if (acc[index % 10]) {
// acc[index % 10].push(word);
// return acc;
// }
// acc[index % 10] = [word];
// return acc;
// }, [])
el.match(/([^\s]+\s?){1,20}/g)
);
const parent =
issue.parent && issue.parent.type.name === 'Epic' ? chalk.bgBlackBright.white(` ${issue.parent.summary} `) : '';
if (name[name.length - 1].length + parent.length > 149) {
name.push(parent);
} else {
name[name.length - 1] = `${name[name.length - 1]} ${parent}`;
}
return {
name: name.map((l, i) => (i > 0 ? ` ${l}` : l)).join('\n'),
value: issue.key,
short: issue.key,
};
});
const wipChoices = [{ key: 'n', name: 'No', value: true }];
if (_.get(config, 'wipDefaultChoice', true)) {
wipChoices.push({ key: 'y', name: 'Yes', value: false });
} else {
wipChoices.unshift({ key: 'y', name: 'Yes', value: false });
}
let questions = [
{
type: 'expand',
name: 'wip',
message: messages.wip,
choices: wipChoices,
default: 0,
},
{
type: 'list',
name: 'type',
message: messages.type,
choices: config.types,
},
{
type: 'list',
name: 'scope',
message: messages.scope,
choices(answers) {
let scopes = [];
if (scopeOverrides[answers.type]) {
scopes = scopes.concat(scopeOverrides[answers.type]);
} else {
scopes = scopes.concat(config.scopes);
}
const defaultChoices = [];
if (config.allowEmptyScopes) {
defaultChoices.push({ name: 'empty', value: false });
}
if (config.allowCustomScopes) {
defaultChoices.push({ name: 'custom', value: 'custom' });
}
if (defaultChoices.length > 0) {
if (config.defaultScopeInFirst) {
scopes.unshift(...defaultChoices, new cz.Separator());
} else {
scopes.push(new cz.Separator(), ...defaultChoices);
}
}
return scopes;
},
when(answers) {
let hasScope = false;
if (scopeOverrides[answers.type]) {
hasScope = !!(scopeOverrides[answers.type].length > 0);
} else {
hasScope = !!(config.scopes && config.scopes.length > 0);
}
if (!hasScope) {
if (config.allowCustomScopes && config.allowEmptyScopes) {
return true;
}
if ((!config.allowCustomScopes && !config.allowEmptyScopes) || config.allowCustomScopes) {
// TODO: Fix when possible
// eslint-disable-next-line no-param-reassign
answers.scope = 'custom';
}
if (config.allowEmptyScopes) {
// TODO: Fix when possible
// eslint-disable-next-line no-param-reassign
answers.scope = false;
}
return false;
}
if (config.allowScopeWithWip) {
return true;
}
return isNotWip(answers);
},
},
{
type: 'input',
name: 'scope',
message: messages.customScope,
when(answers) {
return answers.scope === 'custom';
},
},
{
type: 'multiple-select-search',
name: 'ticketNumbers',
allowCustom: true,
message: answers => {
if (config.isTicketNumberRequired === true) {
return messages.ticketNumber + chalk.reset(' ') + chalk.dim('(Ticket number is required)\n');
}
if (Array.isArray(config.isTicketNumberRequired) && config.isTicketNumberRequired.includes(answers.type)) {
return (
messages.ticketNumber +
chalk.reset(' ') +
chalk.dim(`(Ticket number is required with type${config.isTicketNumberRequired.length > 1 ? 's' : ''}`) +
config.isTicketNumberRequired
.map(type =>
type === answers.type
? chalk.reset(' ') + chalk.cyan.bold(type) + chalk.reset('*')
: chalk.reset(' ') + chalk`{dim ${type}}`
)
.join(chalk.dim(',')) +
chalk.dim(')\n')
);
}
return messages.ticketNumber;
},
searchText: 'Searching...',
emptyText: 'No issue found',
filterMethod: searchIssues,
choices: issues,
pageSize: 7,
loop: false,
reorderOnSelect: true,
shouldDimNotSelected: true,
when() {
return !!config.allowTicketNumber; // no ticket numbers allowed unless specifed
},
validate(value, answers) {
return validateTicketNo(answers.type, value, config);
},
},
{
type: 'input',
name: 'subject',
message: messages.subject,
validate(value) {
if (!value) {
return 'Subject is required';
}
const limit = config.subjectLimit || 100;
if (value.length > limit) {
return `Exceed limit: ${limit}`;
}
return true;
},
filter(value) {
if (config.lowerCaseSubject) {
return value.toLowerCase();
}
const upperCaseSubject = config.upperCaseSubject || false;
return (upperCaseSubject ? value.charAt(0).toUpperCase() : value.charAt(0).toLowerCase()) + value.slice(1);
},
},
{
type: 'input',
name: 'body',
message: messages.body,
},
{
type: 'input',
name: 'breaking',
message: messages.breaking,
when(answers) {
// eslint-disable-next-line max-len
if (
config.askForBreakingChangeFirst ||
(config.allowBreakingChanges && config.allowBreakingChanges.indexOf(answers.type.toLowerCase()) >= 0)
) {
return true;
}
return false; // no breaking changes allowed unless specifed
},
},
{
type: 'input',
name: 'footer',
message: messages.footer,
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' },
],
default: 0,
message(answers) {
const SEP = '###--------------------------------------------------------###';
log.info(`\n${SEP}\n${buildCommit(answers, config)}\n${SEP}\n`);
return messages.confirmCommit;
},
},
];
questions = questions.filter(item => !skipQuestions.includes(item.name));
if (config.askForBreakingChangeFirst) {
const isBreaking = oneQuestion => oneQuestion.name === 'breaking';
const breakingQuestion = _.filter(questions, isBreaking);
const questionWithoutBreaking = _.reject(questions, isBreaking);
questions = _.concat(breakingQuestion, questionWithoutBreaking);
}
return questions;
},
};