forked from sourcefuse/loopback4-microservice-catalog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.czferc.js
194 lines (186 loc) · 5.79 KB
/
.czferc.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
const types = [
{value: 'feat', name: 'feat: A new feature'},
{value: 'fix', name: 'fix: A bug fix'},
{value: 'docs', name: 'docs: Documentation only changes'},
{
value: 'style',
name: 'style: Changes that do not affect the meaning of the code\n (white-space, formatting, missing semi-colons, etc)',
},
{
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\n and libraries such as documentation generation',
},
{value: 'revert', name: 'revert: Revert to a commit'},
{value: 'WIP', name: 'WIP: Work in progress'},
];
const scopes = [
{name: 'chore'},
{name: 'deps'},
{name: 'core'},
{name: 'cli'},
{name: 'cache'},
{name: 'authentication-service'},
{name: 'in-mail-service'},
{name: 'notification-service'},
{name: 'scheduler-service'},
{name: 'video-conferencing-service'},
{name: 'audit-service'},
{name: 'bpmn-service'},
{name: 'search-service'},
{name: 'chat-service'},
{name: 'feature-toggle'},
{name: 'payment-service'},
{name: 'reporting-service'},
{name: 'sandbox'},
{name: 'ocr-parser'},
{name: 'user-tenant-service'},
{name: 'all-services'},
{name: 'typedoc'},
{name: 'mkdocs'},
{name: 'workflows'},
{name: 'task-service'},
];
/**
* @typedef {{type: string; scope: string; subject: string; body: string; isBreaking: boolean; breakingBody: string; breaking: string; isIssueAffected: boolean; issuesBody: string; issues: string;}} Answers
*/
/** @type import('cz-format-extension').Config<Answers> */
module.exports = {
questions({inquirer, gitInfo}) {
let migrationQuestions = getMigrationChanges(gitInfo.staged);
return [
{
type: 'list',
name: 'type',
message: 'Select type',
choices: types,
},
{
type: 'list',
name: 'scope',
message: 'Denote the SCOPE of this change (optional):\n',
choices: scopes,
},
{
type: 'input',
name: 'subject',
message: 'Write a SHORT, IMPERATIVE tense description of the change:\n',
validate: subject =>
subject.length === 0 ? 'subject is required' : true,
},
...migrationQuestions,
{
type: 'input',
name: 'body',
message:
'Provide a LONGER description of the change (optional). Use "|" to break new line:\n',
},
{
type: 'input',
name: 'breaking',
message: 'List any BREAKING CHANGES (optional):\n',
},
{
type: 'input',
name: 'issues',
message:
'List any ISSUES CLOSED by this change (optional). E.g.: #31, #34:\n',
filter: (input, answers) => {
return input.replace(/^\#/g, 'gh-');
},
},
{
type: 'expand',
name: 'confirmCommit',
choices: [
{key: 'y', name: 'Yes', value: 'yes'},
{key: 'n', name: 'Abort commit', value: 'no'},
],
default: 0,
message(answers) {
const SEP =
'###--------------------------------------------------------###';
console.log(`\n${SEP}\n${buildCommit({answers, gitInfo})}\n${SEP}\n`);
return 'Are you sure you want to proceed with the commit above?';
},
},
];
},
commitMessage({answers, gitInfo}) {
if (answers.confirmCommit === 'yes') {
return buildCommit({answers, gitInfo});
} else {
throw Error('Commit cancelled.');
}
},
};
function buildCommit({answers, gitInfo}) {
const migrationKeys = Object.keys(answers).filter(q =>
q.includes('migration-'),
);
const migrationAnswers = migrationKeys
.map(key => `${key}- ${answers[key]}`)
.join('\n');
const migrations =
migrationAnswers && migrationAnswers.length > 0
? `MIGRATION CHANGE:\n${migrationAnswers}`
: false;
const scope = answers.scope ? `(${answers.scope})` : false;
const head = `${answers.type}${scope}: ${answers.subject}`;
const body = answers.body ? answers.body : false;
const breaking = answers.breaking
? `BREAKING CHANGE:\n${answers.breaking}`
: false;
const issues = answers.issues
? answers.issues.split(', ').join('\n').valueOf()
: false;
return escapeSpecialChars(
[head, body, migrations, breaking, issues].filter(p => p).join('\n\n'),
);
}
function getMigrationChanges(staged) {
let migrationPaths = staged
.map(
filePath =>
filePath.includes('migrations/') && filePath.split(/\//).pop(),
)
.filter(f => f && f.length > 0);
let timestamps = {};
for (let item of migrationPaths) {
let match = item.match(/[0-9]{14}/g);
if (match && match[0]) {
timestamps[match[0]] = [...(timestamps[match[0]] || []), item];
}
}
let questions = [];
Object.keys(timestamps).forEach(timestamp => {
questions.push({
type: 'input',
name: `migration-${timestamp}`,
message: `Write a description of the changes in migrations for timestamp - ${timestamp}:\n`,
validate: description =>
description.length === 0 ? 'description is required' : true,
});
});
return questions;
}
const escapeSpecialChars = result => {
// eslint-disable-next-line no-useless-escape
const specialChars = ['`'];
let newResult = result;
// eslint-disable-next-line array-callback-return
specialChars.map(item => {
// If user types "feat: `string`", the commit preview should show "feat: `\string\`".
// Don't worry. The git log will be "feat: `string`"
newResult = result.replace(new RegExp(item, 'g'), '\\`');
});
return newResult;
};