-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
292 lines (265 loc) · 10.2 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
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 types = require('./lib/types')
const readFile = util.promisify(fs.readFile)
function loadConfig(filename) {
return readFile(filename, 'utf8')
.then(JSON.parse)
.then(obj => obj && obj.config && obj.config['cz-gitmoji-adapter'])
.catch(() => null)
}
function loadConfigUpwards(filename) {
return findUp(filename).then(loadConfig)
}
async function getConfig() {
const defaultFormat = `{emoji} {type}{scope}: {subject}`
const conventionalFormat = `{type}{scope}: {emoji} {subject}`
const defaultConfig = {
types,
symbol: false,
skipQuestions: [''],
subjectMaxLength: 75,
conventional: false
}
const loadedConfig =
//check for config files locations in this order package.json > project directory > home directory
(await loadConfigUpwards('package.json')) ||
//check project directory for config files
(await loadConfigUpwards('.czrc')) ||
(await loadConfig(path.join(process.cwd(), '.czrc'))) ||
(await loadConfigUpwards('.czrc.js')) ||
(await loadConfig(path.join(process.cwd(), '.czrc.js'))) ||
(await loadConfigUpwards('.czrc.cjs')) ||
(await loadConfig(path.join(process.cwd(), '.czrc.cjs'))) ||
(await loadConfigUpwards('.czrc.mjs')) ||
(await loadConfig(path.join(process.cwd(), '.czrc.mjs'))) ||
(await loadConfigUpwards('.czrc.ts')) ||
(await loadConfig(path.join(process.cwd(), '.czrc.ts'))) ||
(await loadConfigUpwards('.czrc.cts')) ||
(await loadConfig(path.join(process.cwd(), '.czrc.cts'))) ||
(await loadConfigUpwards('.czrc.json')) ||
(await loadConfig(path.join(process.cwd(), '.czrc.json'))) ||
(await loadConfigUpwards('.czrc.yml')) ||
(await loadConfig(path.join(process.cwd(), '.czrc.yml'))) ||
(await loadConfigUpwards('.czrc.yaml')) ||
(await loadConfig(path.join(process.cwd(), '.czrc.yaml'))) ||
(await loadConfigUpwards('.cz.config.js')) ||
(await loadConfig(path.join(process.cwd(), '.cz.config.js'))) ||
(await loadConfigUpwards('.cz.config.cjs')) ||
(await loadConfig(path.join(process.cwd(), '.cz.config.cjs'))) ||
(await loadConfigUpwards('.cz.config.mjs')) ||
(await loadConfig(path.join(process.cwd(), '.cz.config.mjs'))) ||
(await loadConfigUpwards('.cz.config.ts')) ||
(await loadConfig(path.join(process.cwd(), '.cz.config.ts'))) ||
(await loadConfigUpwards('.cz.config.cts')) ||
(await loadConfig(path.join(process.cwd(), '.cz.config.cts'))) ||
(await loadConfigUpwards('.cz.config.json')) ||
(await loadConfig(path.join(process.cwd(), '.cz.config.json'))) ||
(await loadConfigUpwards('.cz.config.yml')) ||
(await loadConfig(path.join(process.cwd(), '.cz.config.yml'))) ||
(await loadConfigUpwards('.cz.config.yaml')) ||
(await loadConfig(path.join(process.cwd(), '.cz.config.yaml'))) ||
//check home directory ~/ for config files
(await loadConfigUpwards('.czrc')) ||
(await loadConfig(path.join(homedir(), '.czrc'))) ||
(await loadConfigUpwards('.czrc.js')) ||
(await loadConfig(path.join(homedir(), '.czrc.js'))) ||
(await loadConfigUpwards('.czrc.cjs')) ||
(await loadConfig(path.join(homedir(), '.czrc.cjs'))) ||
(await loadConfigUpwards('.czrc.mjs')) ||
(await loadConfig(path.join(homedir(), '.czrc.mjs'))) ||
(await loadConfigUpwards('.czrc.ts')) ||
(await loadConfig(path.join(homedir(), '.czrc.ts'))) ||
(await loadConfigUpwards('.czrc.cts')) ||
(await loadConfig(path.join(homedir(), '.czrc.cts'))) ||
(await loadConfigUpwards('.czrc.json')) ||
(await loadConfig(path.join(homedir(), '.czrc.json'))) ||
(await loadConfigUpwards('.czrc.yml')) ||
(await loadConfig(path.join(homedir(), '.czrc.yml'))) ||
(await loadConfigUpwards('.czrc.yaml')) ||
(await loadConfig(path.join(homedir(), '.czrc.yaml'))) ||
(await loadConfigUpwards('.cz.config.js')) ||
(await loadConfig(path.join(homedir(), '.cz.config.js'))) ||
(await loadConfigUpwards('.cz.config.cjs')) ||
(await loadConfig(path.join(homedir(), '.cz.config.cjs'))) ||
(await loadConfigUpwards('.cz.config.mjs')) ||
(await loadConfig(path.join(homedir(), '.cz.config.mjs'))) ||
(await loadConfigUpwards('.cz.config.ts')) ||
(await loadConfig(path.join(homedir(), '.cz.config.ts'))) ||
(await loadConfigUpwards('.cz.config.cts')) ||
(await loadConfig(path.join(homedir(), '.cz.config.cts'))) ||
(await loadConfigUpwards('.cz.config.json')) ||
(await loadConfig(path.join(homedir(), '.cz.config.json'))) ||
(await loadConfigUpwards('.cz.config.yml')) ||
(await loadConfig(path.join(homedir(), '.cz.config.yml'))) ||
(await loadConfigUpwards('.cz.config.yaml')) ||
(await loadConfig(path.join(homedir(), '.cz.config.yaml'))) ||
{}
const config = {
...defaultConfig,
...{
format: loadedConfig.conventional ? conventionalFormat : defaultFormat
},
...loadedConfig
}
return 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 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
: "Select the type of change you're committing:",
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 : 'Specify a 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
: 'Write a short description:',
maxLength: config.subjectMaxLength
},
{
type: 'input',
name: 'body',
message:
config.questions && config.questions.body
? config.questions.body
: 'Provide a longer description:',
when: !config.skipQuestions.includes('body')
},
{
type: 'input',
name: 'breakingBody',
message:
config.questions && config.questions.breaking
? config.questions.breaking
: 'A BREAKING CHANGE commit requires a body. Please enter a longer description of the commit itself (press ENTER to skip if there are no breaking changes):\n',
when: !config.skipQuestions.includes('breaking')
},
{
type: 'input',
name: 'issues',
message:
config.questions && config.questions.issues
? config.questions.issues
: 'List any issue closed (#1, #2, ...) (press ENTER to skip if this commit does NOT solve any issues):',
when: !config.skipQuestions.includes('issues')
}
]
return questions
}
/**
* Format the git commit message from given answers.
*
* @param {Object} answers Answers provide by `inquier.js`
* @param {Object} config Result of the `getConfig` returned promise
* @return {String} Formatted git commit message
*/
function formatCommitMessage(answers, config) {
const { columns } = process.stdout
const emoji = answers.type
const type = config.types.find(type => type.name === emoji.name).name
const scope = answers.scope ? '(' + answers.scope.trim() + ')' : ''
const subject = answers.subject.trim()
const commitMessage = config.format
.replace(/{emoji}/g, emoji.emoji)
.replace(/{type}/g, type)
.replace(/{scope}/g, scope)
.replace(/{subject}/g, subject)
// Only allow at most one whitespace.
// When an optional field (ie. `scope`) is not specified, it can leave several consecutive
// white spaces in the final message.
.replace(/\s+/g, ' ')
const head = truncate(commitMessage, 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()
}
/**
* Interactively prompts the git commit message to the user.
*
* @param {commitizen} cz Commitizen object
* @return {String} Git message provided by the user
*/
async function promptCommitMessage(cz) {
cz.prompt.registerPrompt('autocomplete', require('inquirer-autocomplete-prompt'))
cz.prompt.registerPrompt('maxlength-input', require('inquirer-maxlength-input-prompt'))
const config = await getConfig()
const questions = createQuestions(config)
const answers = await cz.prompt(questions)
const message = formatCommitMessage(answers, config)
return message
}
/**
* Export an object containing a `prompter` method. This object is used by `commitizen`.
*
* @type {Object}
*/
module.exports = {
prompter: (cz, commit) => {
promptCommitMessage(cz).then(commit)
}
}