-
Notifications
You must be signed in to change notification settings - Fork 0
/
.czferc.js
138 lines (131 loc) · 4.12 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
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: 'templates' },
{ name: 'actions' },
{ name: 'plugins' },
];
/**
* @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 }) {
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,
},
{
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.: ARC-12, ARC-34:\n',
filter: (input, answers) => {
return input.replace(/^\#/g, 'ARC-');
},
},
{
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 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, breaking, issues].filter(p => p).join('\n\n'),
);
}
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;
};