forked from ngryman/cz-emoji
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
205 lines (178 loc) · 7.6 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
const findUp = require('find-up')
const fs = require('fs')
const homedir = require('homedir')
const truncate = require('cli-truncate')
const wrap = require('wrap-ansi')
const path = require('path')
const util = require('util')
const pad = require('pad')
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-frontboi'])
.catch(() => null)
}
function loadConfigUpwards(filename) {
return findUp(filename).then(loadConfig)
}
/**
* Read the configuration extracted from either the package.json, .czrc or global .czrc files
*
* @param {Object} config the configuration object if it exists, or an empty object
*/
async function getConfig() {
const defaultConfig = {
types,
symbol: false,
skipQuestions: [''],
subjectMaxLength: 75,
format: '{emoji} {type}{scope}: {subject}'
}
const loadedConfig =
(await loadConfigUpwards('package.json')) ||
(await loadConfigUpwards('.czrc')) ||
(await loadConfig(path.join(homedir(), '.czrc'))) ||
{}
const allTypes = loadedConfig.overrideNativeTypes
? loadedConfig.types ?? []
: [...defaultConfig.types, ...(loadedConfig.types ?? [])]
const filteredTypes = allTypes.filter(type =>
loadedConfig.skipTypes ? !loadedConfig.skipTypes.includes(type.name) : true
)
const config = {
...defaultConfig,
...loadedConfig,
types: filteredTypes,
scopes: loadedConfig.scopes
}
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
}))
}
async function requiredField(input) {
return input.length === 0 ? 'Il faut renseigner une valeur' : true
}
/**
* 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) {
return [
{
type: 'list',
name: 'type',
choices: getEmojiChoices(config),
message: config.questions && config.questions.type ? config.questions.type : 'Type de commit:'
},
{
type: config.scopes ? 'list' : 'input',
name: 'scope',
message: config.questions && config.questions.scope ? config.questions.scope : 'Cadre du commit (optionnel):',
choices: config.scopes && config.scopes.map(scope => scope.name),
when: !config.skipQuestions.includes('scope')
},
{
type: 'maxlength-input',
name: 'subject',
message: config.questions && config.questions.subject ? config.questions.subject : 'Sujet du commit:',
maxLength: config.subjectMaxLength,
validate: requiredField
},
{
type: 'input',
name: 'breaking_change',
message: config.questions && config.questions.body ? config.questions.body : 'Breaking change ? (y/N):',
when: !config.skipQuestions.includes('breaking_change')
}
]
}
/**
* 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 type = answers.type.name
const subject = answers.subject.trim()
const hasBreakingChange = answers.breaking_change === 'y'
const emoji = config.types.find(type => type.code === answers.type.emoji).emoji
let scope = ''
if (answers.scope) {
if (config.scopes) {
scope = `(${config.scopes.find(scope => scope.name === answers.scope).value.trim()})`
} else {
scope = `(${answers.scope})`
}
}
// ajout du breaking change si nécessaire
if (hasBreakingChange) {
scope += '!'
}
const commitMessage = config.format
.replace(/{emoji}/g, emoji)
.replace(/{type}/g, type)
.replace(/{scope}/g, scope)
.replace(/{subject}/g, subject)
.replace(/\s+/g, ' ')
const head = truncate(commitMessage, columns)
const body = '' // si tu veux ajouter le body, ça se fera par ici (et donc en posant une question supplémentaire)
return [head, body]
.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('maxlength-input', require('inquirer-maxlength-input-prompt'))
console.log(`
██╗ ███████╗ ██████╗ ██████╗ ███╗ ███╗███╗ ███╗██╗████████╗ ██████╗ ██████╗ ██████╗ ██████╗ ██████╗ ███████╗
██║ ██╔════╝ ██╔════╝██╔═══██╗████╗ ████║████╗ ████║██║╚══██╔══╝ ██╔══██╗██╔══██╗██╔═══██╗██╔══██╗██╔══██╗██╔════╝
██║ █████╗ ██║ ██║ ██║██╔████╔██║██╔████╔██║██║ ██║ ██████╔╝██████╔╝██║ ██║██████╔╝██████╔╝█████╗
██║ ██╔══╝ ██║ ██║ ██║██║╚██╔╝██║██║╚██╔╝██║██║ ██║ ██╔═══╝ ██╔══██╗██║ ██║██╔═══╝ ██╔══██╗██╔══╝
███████╗███████╗ ╚██████╗╚██████╔╝██║ ╚═╝ ██║██║ ╚═╝ ██║██║ ██║ ██║ ██║ ██║╚██████╔╝██║ ██║ ██║███████╗
╚══════╝╚══════╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝╚══════╝
⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜⚜
frontBOI - 1.2.0
`)
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)
}
}