forked from ngryman/cz-emoji
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
228 lines (201 loc) · 5.86 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
const fs = require('fs')
const readPkg = require('read-pkg-up')
const truncate = require('cli-truncate')
const wrap = require('wrap-ansi')
const pad = require('pad')
const path = require('path')
const fuse = require('fuse.js')
const homeDir = require('home-dir')
const util = require('util')
const { exec } = require('child_process');
const execProm = util.promisify(exec);
const types = require('./lib/types')
const defaultConfig = {
types,
workflows:[],
projectDir: '~/workspace/hanger',
symbol: false,
skipQuestions: [''],
subjectMaxLength: 75,
conventional: false
}
let jiraTicket;
async function getGitBranch(){
const jiraMatcher = /((?!([A-Z0-9a-z]{1,10})-?$)[A-Z]{1}[A-Z0-9]+-\d+)/g;
const branch = await execProm('git rev-parse --abbrev-ref HEAD')
const jiraTickets = branch.stdout.match(jiraMatcher);
if(Array.isArray(jiraTicket)) {
return jiraTickets[jiraTickets.length -1];
} else {
return 'NO-TICKET'
}
}
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 loadConfig() {
jiraTicket = await getGitBranch()
const getConfig = obj => obj && obj.config
const readFromCzrc = dir =>
util
.promisify(fs.readFile)(dir, 'utf8')
.then(JSON.parse, () => null)
.then(getConfig)
const readFromLocalCzrc = () =>
readPkg().then(res =>
res && res.path ? readFromCzrc(`${path.dirname(res.path)}/.czrc`) : null
)
const config = (await readFromLocalCzrc())
return { ...defaultConfig, ...config }
}
function formatScope(scope) {
return scope ? `(${scope})` : ''
}
function formatHead({ type, scope, issues, workflow, subject, time, comment }, config) {
const prelude = config.conventional
? `${type.name}${formatScope(scope)}: ${type.emoji}`
: `${type.emoji} ${formatScope(scope)} `
return `${prelude} ${subject}`
}
function formatBody({ issues, workflow, body, time, comment }, config) {
const smartText = filter([
workflow && workflow != '[NONE]' ? '#' + workflow : undefined,
time ? '#time ' + time : undefined,
comment ? '#comment ' + comment : undefined,
]).join(` `);
return `${body} \n ${formatLineItem(issues, smartText)}`
}
function formatLineItem(issues, line) {
return `${issues} ${line}`
}
function filter(array) {
return array.filter(function(item) {
return !!item;
});
}
/**
* 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 `loadConfig` returned promise
* @return {Array} Return an array of `inquier.js` questions
* @private
*/
function createQuestions(config) {
const choices = getEmojiChoices(config)
const workflows = [{name:'[NONE]',value:'[NONE]'}, ...config.workflows]
const fuzzyOptions = {
shouldSort: true,
threshold: 0.4,
location: 0,
distance: 100,
maxPatternLength: 32,
minMatchCharLength: 1,
keys: ['name', 'code']
}
const fuzzyEmojis = new fuse(choices, fuzzyOptions)
const fuzzyWorkflows = new fuse(workflows, fuzzyOptions)
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 ? fuzzyEmojis.search(query) : choices)
},
{
type: config.scopes ? 'list' : 'input',
name: 'scope',
message: 'Specify a scope:',
when: !config.skipQuestions.includes('scope')
},
{
type: 'input',
name: 'issues',
message: 'Jira Issue ID(s):',
default: jiraTicket,
validate: function(input) {
if (!input) {
return 'Must specify issue IDs';
} else {
return true;
}
}
},
{
type: 'input',
name: 'time',
message: 'Time spent (i.e. 3h 15m):'
},
{
type: 'input',
name: 'comment',
message: 'Jira comment:'
},
{
type: 'autocomplete',
name: 'workflow',
message: 'Workflow command:',
source: (_, query) => Promise.resolve(query ? fuzzyWorkflows.search(query) : workflows)
},
{
type: 'maxlength-input',
name: 'subject',
message: 'Write a short description:',
maxLength: config.subjectMaxLength,
filter: (subject, answers) => formatHead({ ...answers, subject }, config)
},
{
type: 'input',
name: 'body',
message: 'Provide a longer description:',
when: !config.skipQuestions.includes('body'),
filter: (body, answers) => formatBody({ ...answers, body }, config)
}
]
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)
return [head, body]
.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('@matti-o7/inquirer-maxlength-input-prompt'))
loadConfig()
.then(createQuestions)
.then(cz.prompt)
.then(format)
.then(commit)
}
}