-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
110 lines (103 loc) · 2.87 KB
/
index.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
const chalk = require('chalk');
const branch = require('git-branch');
const types = require('./types');
const getLongInput = ({ required = false, maxLength = 80 }) => ({
validate: value => {
const length = value.trim().length;
return required && length <= 0
? 'required'
: length > maxLength
? 'max ' + maxLength + ' chars'
: true;
},
transformer: value => '(' + value.trim().length + ') ' + value,
filter: value => value.trim()
});
const longest = array =>
array.reduce(
(acc, current) => (current.length > acc ? current.length : acc),
0
);
const keys = Object.keys(types);
const choices = keys.map(key => ({
name:
(types[key].title + ':').padEnd(
longest(keys.map(key => types[key].title)) + 2
) + types[key].description,
value: types[key].title
}));
const formatCommit = ({ issue, type, scope, subject }) =>
`${issue ? issue : '(NOTASK)'} ${type}${
scope.length >= 1 ? `(${scope})` : ''
}: ${subject}`;
module.exports = {
prompter: (cz, commit, ...args) => {
const match = branch.sync().match(/[A-Z]+-[0-9]+/);
const issueId = match && match[0];
cz.prompt([
{
type: 'confirm',
name: 'isIssueAffected',
message: 'Does this change affect an issue?',
default: true
},
{
...getLongInput({
required: true,
maxLength: 20
}),
type: 'input',
name: 'issue',
message: 'Issue reference:',
when: answers => answers.isIssueAffected,
default: issueId,
transformer: value =>
'(' + value.trim().length + ') ' + value.toUpperCase(),
filter: value => value.trim().toUpperCase()
},
{
type: 'list',
name: 'type',
message: "Select the type of change that you're committing:",
choices,
default: choices[0]
},
{
...getLongInput({
required: false,
maxLength: 20
}),
type: 'input',
name: 'scope',
message: 'What is the scope of this change: (press enter to skip)',
default: undefined,
transformer: value =>
'(' + value.trim().length + ') ' + value.toLowerCase(),
filter: value => value.trim().toLowerCase()
},
{
...getLongInput({
required: true,
maxLength: 60
}),
type: 'input',
name: 'subject',
message: 'Write a short, imperative tense description of the change:\n',
default: undefined
},
{
type: 'confirm',
name: 'commit',
message: answers =>
` Commit with this message? ${chalk.cyan(formatCommit(answers))}`,
default: true
}
]).then(answers => {
if (answers.commit) {
commit(formatCommit(answers));
} else {
console.log(chalk.red('Aborted ...'));
}
});
}
};