-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
197 lines (175 loc) · 5.37 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
const findUp = require('find-up')
const fs = require('fs')
const homedir = require('homedir')
const truncate = require('cli-truncate')
const wrap = require('wrap-ansi')
const pad = require('pad')
const path = require('path')
const fuse = require('fuse.js')
const util = require('util')
const readFile = util.promisify(fs.readFile)
const types = require('./lib/types')
function loadConfig(filename) {
return readFile(filename, 'utf8')
.then(JSON.parse)
.then(obj => obj && obj.config && obj.config['g-cz-emoji'])
.catch(() => null)
}
function loadConfigUpwards(filename) {
return findUp(filename).then(loadConfig)
}
async function getConfig() {
const defaultConfig = {
types,
symbol: true,
skipQuestions: [],
subjectMaxLength: 75,
subjectMinLength: 2,
conventional: true
}
const config =
(await loadConfigUpwards('package.json')) ||
(await loadConfigUpwards('.czrc')) ||
(await loadConfig(path.join(homedir(), '.czrc')))
return { ...defaultConfig, ...config }
}
function getEmojiChoices({ types, symbol }) {
const maxNameLength = types.reduce(
(maxLength, type) => (type.name.length > maxLength ? type.name.length : maxLength),
0
)
return types.map(choice => ({
name: `${pad(choice.name, maxNameLength)} ${choice.emoji} ${choice.description}`,
value: {
emoji: symbol ? `${choice.emoji}` : choice.code,
name: choice.name
},
code: choice.code
}))
}
function formatScope(scope) {
return scope ? `(${scope})` : ''
}
function formatHead({ type, scope, subject }, config) {
const prelude = config.conventional
? `${type.name}${formatScope(scope)}: ${type.emoji}`
: `${type.emoji} ${formatScope(scope)}`
return `${prelude} ${subject}`
}
function formatIssues(issues) {
return issues ? 'Closes ' + (issues.match(/#\d+/g) || []).join(', closes ') : ''
}
/**
* Create inquier.js questions object trying to read `types` and `scopes` from the current project
* `package.json` falling back to nice default :)
*
* @param {Object} config Result of the `getConfig` returned promise
* @return {Array} Return an array of `inquier.js` questions
* @private
*/
function createQuestions(config) {
const choices = getEmojiChoices(config)
const fuzzy = new fuse(choices, {
shouldSort: true,
threshold: 0.4,
location: 0,
distance: 100,
maxPatternLength: 32,
minMatchCharLength: 1,
keys: ['name', 'code']
})
const questions = [
{
type: 'autocomplete',
name: 'type',
message:
config.questions && config.questions.type
? config.questions.type
: "选择提交的更改类型:",
source: (_, query) => Promise.resolve(query ? fuzzy.search(query) : choices)
},
{
type: config.scopes ? 'list' : 'input',
name: 'scope',
message:
config.questions && config.questions.scope ? config.questions.scope : '指定一个范围:',
choices: config.scopes && [{ name: '[none]', value: '' }].concat(config.scopes),
when: !config.skipQuestions.includes('scope')
},
{
type: 'maxlength-input',
name: 'subject',
message:
config.questions && config.questions.subject
? config.questions.subject
: '写一个简短的描述:',
maxLength: config.subjectMaxLength,
validate: function(value) {
const [type, emoji,...args] = value.split(' ');
const content = args.join('')
const minLength = config.subjectMinLength;
if (content.length >= minLength) {
return true;
}
const message = minLength > 1 ? `,长度不少于${minLength}` : '';
return `须输入改动描述${message}`;
},
filter: (subject, answers) => formatHead({ ...answers, subject }, config)
},
{
type: 'input',
name: 'body',
message:
config.questions && config.questions.body
? config.questions.body
: '写一个更详细的描述:',
when: !config.skipQuestions.includes('body')
},
{
type: 'input',
name: 'issues',
message:
config.questions && config.questions.issues
? config.questions.issues
: '列出已解决的 issue (#1, #2, 无直接回车):',
when: !config.skipQuestions.includes('issues')
}
]
return questions
}
/**
* Format the git commit message from given answers.
*
* @param {Object} answers Answers provide by `inquier.js`
* @return {String} Formated git commit message
*/
function format(answers) {
const { columns } = process.stdout
const head = truncate(answers.subject, columns)
const body = wrap(answers.body || '', columns)
const breaking =
answers.breakingBody && answers.breakingBody.trim().length !== 0
? wrap(`BREAKING CHANGE: ${answers.breakingBody.trim()}`, columns)
: ''
const footer = formatIssues(answers.issues)
return [head, body, breaking, footer]
.filter(Boolean)
.join('\n\n')
.trim()
}
/**
* Export an object containing a `prompter` method. This object is used by `commitizen`.
*
* @type {Object}
*/
module.exports = {
prompter: function(cz, commit) {
cz.prompt.registerPrompt('autocomplete', require('inquirer-autocomplete-prompt'))
cz.prompt.registerPrompt('maxlength-input', require('inquirer-maxlength-input-prompt'))
getConfig()
.then(createQuestions)
.then(cz.prompt)
.then(format)
.then(commit)
}
}