-
Notifications
You must be signed in to change notification settings - Fork 48
/
index.js
97 lines (92 loc) · 2.46 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
var inquirer = require('inquirer')
// This can be any kind of SystemJS compatible module.
// We use Commonjs here, but ES6 or AMD would do just
// fine.
module.exports = {
prompter: prompter,
formatCommit: formatCommit
};
// 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.
function prompter(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.
inquirer.prompt([
{
type: 'input',
name: 'message',
message: 'GitHub commit message (required):\n',
validate: function(input) {
if (!input) {
return 'empty commit message';
} else {
return true;
}
}
},
{
type: 'input',
name: 'issues',
message: 'Jira Issue ID(s) (required):\n',
validate: function(input) {
if (!input) {
return 'Must specify issue IDs, otherwise, just use a normal commit message';
} else {
return true;
}
}
},
{
type: 'input',
name: 'workflow',
message: 'Workflow command (testing, closed, etc.) (optional):\n',
validate: function(input) {
if (input && input.indexOf(' ') !== -1) {
return 'Workflows cannot have spaces in smart commits. If your workflow name has a space, use a dash (-)';
} else {
return true;
}
}
},
{
type: 'input',
name: 'time',
message: 'Time spent (i.e. 3h 15m) (optional):\n'
},
{
type: 'input',
name: 'comment',
message: 'Jira comment (optional):\n'
},
]).then((answers) => {
formatCommit(commit, answers);
});
}
function formatCommit(commit, answers) {
commit(filter([
answers.message,
answers.issues,
answers.workflow ? '#' + answers.workflow : undefined,
answers.time ? '#time ' + answers.time : undefined,
answers.comment ? '#comment ' + answers.comment : undefined,
]).join(' '));
}
function filter(array) {
return array.filter(function(item) {
return !!item;
});
}