forked from conventional-changelog/commitlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.js
94 lines (81 loc) · 2.18 KB
/
input.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
import load from '@commitlint/load';
import throat from 'throat';
import format from './library/format';
import getHasName from './library/get-has-name';
import getPrompt from './library/get-prompt';
import settings from './settings';
export default input;
/**
* Get user input by interactive prompt based on
* conventional-changelog-lint rules.
* @param {function} prompter
* @return {Promise<string>} commit message
*/
async function input(prompter) {
const results = {
type: null,
scope: null,
subject: null,
body: null,
footer: null
};
const {rules} = await load();
await Promise.all(
['type', 'scope', 'subject', 'body', 'footer'].map(
throat(1, async input => {
const inputRules = getRules(input, rules);
const inputSettings = settings[input];
const isHeader = ['type', 'scope', 'subject'].indexOf(input) > -1;
const headerLengthRule = getRules('header', rules).filter(
getHasName('max-length')
)[0];
if (isHeader && headerLengthRule) {
const [, [severity, applicable, length]] = headerLengthRule;
if (severity > 0 && applicable === 'always') {
inputSettings.header = {
length
};
}
}
results[input] = await getPrompt(input, {
rules: inputRules,
settings: inputSettings,
results,
prompter
});
})
)
).catch(err => {
console.error(err);
return '';
});
// Return the results
return format(results);
}
/**
* Get prefix for a given rule id
* @param {string} id of the rule
* @return {string} prefix of the rule
*/
function getRulePrefix(id) {
const fragments = id.split('-');
const [prefix] = fragments;
return fragments.length > 1 ? prefix : null;
}
/**
* Get a predecate matching rule definitions with a given prefix
* @param {[type]} name [description]
* @return {[type]} [description]
*/
function getHasPrefix(name) {
return rule => getRulePrefix(rule[0]) === name;
}
/**
* Get rules for a given prefix
* @param {string} prefix to search in rule names
* @param {object} rules rules to search in
* @return {object} rules matching the prefix search
*/
function getRules(prefix, rules) {
return Object.entries(rules).filter(getHasPrefix(prefix));
}