-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
engine.js
168 lines (155 loc) · 5.26 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
/* eslint no-nested-ternary: 0 */
const wrap = require('word-wrap');
const map = require('lodash.map');
const longest = require('longest');
const rightPad = require('right-pad');
const chalk = require('chalk');
const helpers = require('./lib/helpers');
const config = require('./config/default.json');
module.exports = (options) => {
const types = options.types;
const length = longest(Object.keys(types)).length + 1;
const choices = map(types, (type, key) => ({
name: `${config.emojis[key]} ${rightPad(`${key}`, length)} ${type.description}`,
value: key,
}));
return {
prompter(cz, commit) {
cz.prompt([
{
type: 'list',
name: 'type',
message: 'Type of change that you\'re committing:',
choices,
default: options.defaultType,
}, {
type: 'input',
name: 'scope',
message: 'Scope of this change, component or folder name: (enter to skip)',
default: options.defaultScope,
filter(value) {
return value.trim().toLowerCase();
},
}, {
type: 'input',
name: 'subject',
message(answers) {
return (
`Short, imperative tense description of the change (max ${
helpers.maxSummaryLength(options, answers)
} chars):`
);
},
default: options.defaultSubject,
validate(subject, answers) {
const filteredSubject = helpers.filterSubject(subject);
return filteredSubject.length === 0 ? 'subject is required' : filteredSubject.length <= helpers.maxSummaryLength(options, answers) ? true
: `Subject length must be less than or equal to ${
helpers.maxSummaryLength(options, answers)
} characters. Current length is ${
filteredSubject.length
} characters.`;
},
transformer(subject, answers) {
const filteredSubject = helpers.filterSubject(subject);
const color = filteredSubject.length <= helpers.maxSummaryLength(options, answers)
? chalk.green
: chalk.red;
return color(`(${filteredSubject.length}) ${subject}`);
},
filter(subject) {
return helpers.filterSubject(subject);
},
},
{
type: 'input',
name: 'body',
message:
'Longer description of the change: (enter to skip):',
default: options.defaultBody,
},
{
type: 'confirm',
name: 'isBreaking',
message: 'Breaking changes?',
default: false,
},
{
type: 'input',
name: 'breakingBody',
default: '-',
message:
'Breaking change requires a body :',
when(answers) {
return answers.isBreaking && !answers.body;
},
validate(breakingBody) {
return (
breakingBody.trim().length > 0
|| 'Body is required for BREAKING CHANGE'
);
},
},
{
type: 'input',
name: 'breaking',
message: 'Describe the breaking changes:',
when(answers) {
return answers.isBreaking;
},
},
{
type: 'confirm',
name: 'isIssueAffected',
message: 'Affect any open issues?',
default: !!options.defaultIssues,
},
{
type: 'input',
name: 'issuesBody',
default: '-',
message:
'If issues are closed, the commit requires a body:',
when(answers) {
return (
answers.isIssueAffected && !answers.body && !answers.breakingBody
);
},
},
{
type: 'input',
name: 'issues',
message: 'Issue references, "fix #123", "close #123":',
when(answers) {
return answers.isIssueAffected;
},
default: options.defaultIssues ? options.defaultIssues : undefined,
},
]).then((answers) => {
const wrapOptions = {
trim: true,
cut: false,
newline: '\n',
indent: '',
width: options.maxLineWidth,
};
// parentheses are only needed when a scope is present
const scope = answers.scope ? `(${answers.scope})` : '';
// add emoji
let emoji = ` ${config.emojis[answers.type]}`;
// Hard limit this line in the validate
const head = `${answers.type + scope}: ${answers.subject}${emoji}`;
// Wrap these lines at options.maxLineWidth characters
const body = answers.body ? wrap(answers.body, wrapOptions) : false;
// Apply breaking change prefix, removing it if already present
let breaking = answers.breaking ? answers.breaking.trim() : '';
breaking = breaking
? `BREAKING CHANGE: ${breaking.replace(/^BREAKING CHANGE: /, '')}`
: '';
breaking = breaking ? wrap(breaking, wrapOptions) : false;
const issues = answers.issues ? wrap(answers.issues, wrapOptions) : false;
commit(helpers.filter([head, body, breaking, issues]).join('\n\n'));
});
},
};
};