forked from commitizen/cz-conventional-changelog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.js
155 lines (141 loc) · 4.52 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
'format cjs';
const map = require('lodash.map');
const longest = require('longest');
const readConfigFile = require('./read-config-file.js');
const filter = function(array) {
return array.filter(function(x) {
return x;
});
};
const filterSubject = function(subject, disableSubjectLowerCase) {
subject = subject.trim();
if (
!disableSubjectLowerCase &&
subject.charAt(0).toLowerCase() !== subject.charAt(0)
) {
subject =
subject.charAt(0).toLowerCase() + subject.slice(1, subject.length);
}
while (subject.endsWith('.')) {
subject = subject.slice(0, subject.length - 1);
}
return subject;
};
// This can be any kind of SystemJS compatible module.
// We use Commonjs here, but ES6 or AMD would do just
// fine.
module.exports = function(options) {
const types = options.types;
const length = longest(Object.keys(types)).length + 1;
const choices = map(types, function(type, key) {
return {
name: (key + ':').padEnd(length) + ' ' + type.description,
value: key
};
});
return {
// When a user runs `git cz`, prompter will
// be executed. We pass you cz, which currently
// is just an instance of inquirer.js. Using
// this you can ask questions and get answers.
//
// The commit callback should be executed when
// you're ready to send back a commit template
// to git.
//
// By default, we'll de-indent your commit
// template and will keep empty lines.
prompter: function(cz, commit) {
// Let's ask some questions of the user
// so that we can populate our commit
// template.
//
// See inquirer.js docs for specifics.
// You can also opt to use another input
// collection library if you prefer.
const config = readConfigFile();
let jiraKey = 'OS';
if (config) {
if (config.jiraKey) jiraKey = config.jiraKey;
}
cz.prompt([
{
type: 'input',
name: 'storyKey',
message: `What is the story key? (e.g. ${jiraKey}-12345):`,
transformer: function(storyKey) {
return `${jiraKey}-${storyKey}`;
},
validate: function(storyKey) {
if (!storyKey || storyKey.length === 0) {
return 'Story key is required';
}
if (/\D/g.test(storyKey)) return 'Story key only supports numbers';
return true;
}
},
{
type: 'input',
name: 'subtaskKey',
message: `What is the sub-task key? (e.g. ${jiraKey}-12345):`,
transformer: function(subtaskKey) {
return `${jiraKey}-${subtaskKey}`;
},
validate: function(subtaskKey) {
if (!subtaskKey || subtaskKey.length === 0) {
return 'Sub-task key is required';
}
if (/\D/g.test(subtaskKey))
return 'Sub-task key only supports numbers';
return true;
}
},
{
type: 'list',
name: 'type',
message: "Select the type of change that you're committing:",
choices: choices,
default: options.defaultType
},
{
type: 'input',
name: 'scope',
message:
'What is the scope of this change (e.g. component or file name):',
validate: function(scope) {
return scope.length === 0 ? 'Scope is required' : true;
},
filter: function(value) {
return options.disableScopeLowerCase
? value.trim()
: value.trim().toLowerCase();
}
},
{
type: 'input',
name: 'subject',
message: 'Write a description of the change:',
default: options.defaultSubject,
validate: function(subject) {
const filteredSubject = filterSubject(
subject,
options.disableSubjectLowerCase
);
return filteredSubject.length == 0
? 'description is required'
: true;
}
}
]).then(function(answers) {
const storyKey = `[${jiraKey}-${answers.storyKey}]`;
const subtaskKey = `[${jiraKey}-${answers.subtaskKey}]`;
const type = `${answers.type}`;
const scope = `[${answers.scope}]`;
const subject = `${answers.subject}`;
// Hard limit this line in the validate
const head = `${storyKey}${subtaskKey} ${type} ${scope}: ${subject}`;
commit(filter([head]).join('\n\n'));
});
}
};
};