-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
187 lines (164 loc) ยท 5.19 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
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 types = require('./lib/types');
const defaultConfig = {
types,
symbol: false,
skipQuestions: [''],
subjectMaxLength: 75,
subjectMinLength: 1
};
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: symbol ? choice.emoji : choice.code,
code: choice.code
}));
}
async function loadConfig() {
const getConfig = obj => obj && obj.config && obj.config['cz-emoji-chinese'];
const readFromPkg = () => readPkg().then(res => getConfig(res.pkg));
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 readFromGlobalCzrc = () => readFromCzrc(homeDir('.czrc'));
const config =
(await readFromPkg()) || (await readFromLocalCzrc()) || (await readFromGlobalCzrc());
return { ...defaultConfig, ...config };
}
function formatScope(scope) {
return scope ? `(${scope})` : '';
}
function formatHead({ type, scope, subject }) {
return [type, formatScope(scope), subject]
.filter(Boolean)
.map(s => s.trim())
.join(' ');
}
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 `loadConfig` 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: (answersSoFar, query) => {
return 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 arr = value.split(' ');
const minLength = config.subjectMinLength;
if (arr && arr.length > 1 && arr[0].length > 3 && arr[1].length > minLength) {
return true;
}
const message = minLength > 1 ? `๏ผ้ฟๅบฆไธๅฐไบ${minLength}` : '';
return `้กป่พๅ
ฅๆนๅจๆ่ฟฐ${message}`;
},
filter: (subject, answers) => formatHead({ ...answers, subject })
},
{
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 footer = formatIssues(answers.issues);
return [head, body, 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'));
loadConfig()
.then(createQuestions)
.then(cz.prompt)
.then(format)
.then(commit);
}
};