forked from leoforfree/cz-customizable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
questions.js
206 lines (193 loc) · 6.49 KB
/
questions.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
const fs = require('fs');
const _ = require('lodash');
const buildCommit = require('./buildCommit');
const log = require('./logger');
const isNotWip = answers => answers.type.toLowerCase() !== 'wip';
const isValidateTicketNo = (value, config) => {
if (!value) {
return !config.isTicketNumberRequired;
}
if (!config.ticketNumberRegExp) {
return true;
}
const reg = new RegExp(config.ticketNumberRegExp);
if (value.replace(reg, '') !== '') {
return false;
}
return true;
};
const getPreparedCommit = context => {
let message = null;
if (fs.existsSync('./.git/COMMIT_EDITMSG')) {
let preparedCommit = fs.readFileSync('./.git/COMMIT_EDITMSG', 'utf-8');
preparedCommit = preparedCommit
.replace(/^#.*/gm, '')
.replace(/^\s*[\r\n]/gm, '')
.replace(/[\r\n]$/, '')
.split(/\r\n|\r|\n/);
if (preparedCommit.length && preparedCommit[0]) {
if (context === 'subject') [message] = preparedCommit;
else if (context === 'body' && preparedCommit.length > 1) {
preparedCommit.shift();
message = preparedCommit.join('|');
}
}
}
return message;
};
module.exports = {
getQuestions(config, cz) {
// normalize config optional options
const scopeOverrides = config.scopeOverrides || {};
const messages = config.messages || {};
const skipQuestions = config.skipQuestions || [];
messages.type = messages.type || "Select the type of change that you're committing:";
messages.scope = messages.scope || '\nDenote the SCOPE of this change (optional):';
messages.customScope = messages.customScope || 'Denote the SCOPE of this change:';
if (!messages.ticketNumber) {
if (config.ticketNumberRegExp) {
messages.ticketNumber =
messages.ticketNumberPattern ||
`Enter the ticket number following this pattern (${config.ticketNumberRegExp})\n`;
} else {
messages.ticketNumber = 'Enter the ticket number:\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?';
let questions = [
{
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);
}
if (config.allowCustomScopes || scopes.length === 0) {
scopes = scopes.concat([
new cz.Separator(),
{ name: 'empty', value: false },
{ name: 'custom', value: 'custom' },
]);
}
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) {
// TODO: Fix when possible
// eslint-disable-next-line no-param-reassign
answers.scope = 'custom';
return false;
}
return isNotWip(answers);
},
},
{
type: 'input',
name: 'scope',
message: messages.customScope,
when(answers) {
return answers.scope === 'custom';
},
},
{
type: 'input',
name: 'ticketNumber',
message: messages.ticketNumber,
when() {
return !!config.allowTicketNumber; // no ticket numbers allowed unless specifed
},
validate(value) {
return isValidateTicketNo(value, config);
},
},
{
type: 'input',
name: 'subject',
message: messages.subject,
default: getPreparedCommit('subject'),
validate(value) {
const limit = config.subjectLimit || 100;
if (value.length > limit) {
return `Exceed limit: ${limit}`;
}
return true;
},
filter(value) {
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,
default: getPreparedCommit('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;
},
};