forked from parachvte/Luban
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.cz-conventional-changelog.js
113 lines (104 loc) · 3.14 KB
/
.cz-conventional-changelog.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
const _ = require('lodash');
const wrap = require('word-wrap');
const commitizen = require('commitizen');
function createQuestions(config) {
config.maxSubjectLength = config.maxSubjectLength || 80;
const types = [
{
name: 'Feature',
description: 'A new feature'
},
{
name: 'Improvement',
description: 'An improvement to a current feature'
},
{
name: 'Fix',
description: 'A bug fix'
},
{
name: 'Refactor',
description: 'A code change that neither fixes a bug nor adds a feature'
},
{
name: 'Perf',
description: 'A code change that improves performance'
},
{
name: 'Test',
description: 'Adding missing tests or correcting existing tests'
},
{
name: 'Build',
description: 'Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)'
},
{
name: 'Chore',
description: 'Changes that do not affect the meaning of the code (package.json, white-space, formatting, etc)'
},
{
name: 'Docs',
description: 'Documentation only changes'
}
];
const length = 15;
const choices = _.map(types, (type) => ({
name: `${_.padEnd(`${type.name}:`, length)} ${type.description}`,
value: type.name
}));
return [
{
type: 'list',
name: 'type',
message: "Select the type of change that you're committing:",
choices
},
{
type: 'input',
name: 'subject',
message: `Write a short, imperative tense description of the change (max header length: ${config.maxSubjectLength}):\n`
},
{
type: 'input',
name: 'body',
message: 'Provide a longer description of the change:\n'
},
{
type: 'input',
name: 'issues',
message: 'List any issues closed by this change:\n'
}
];
}
/**
* Format the git commit message from given answers.
*
* @param {Object} answers Answers provide user
* @param {Object} config Config specified in package.json
*
* @return {String} Formatted git commit message
*/
function format(answers, config) {
config.bodyLineLength = config.bodyLineLength || 80;
const wrapOptions = {
trim: true,
newline: '\n',
indent: '',
cut: true,
width: config.bodyLineLength,
};
const head = answers.type + ': ' + answers.subject.trim();
const body = wrap(answers.body, wrapOptions);
const footer = answers.issues
? 'Closes ' + (answers.issues.match(/#\d+/g) || []).join(', closes')
: '';
return [head, body, footer].filter(x => x).join('\n\n');
}
module.exports = {
prompter: (cz, commit) => {
const config = commitizen.configLoader.load();
cz.prompt(createQuestions(config))
.then(answers => format(answers, config))
.then(commit);
}
};