forked from conventional-changelog/commitlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-prompt.ts
123 lines (108 loc) Β· 3.24 KB
/
get-prompt.ts
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
import chalk from 'chalk';
import type {InputCustomOptions} from 'inquirer';
import type {InputSetting, RuleEntry, Result, ResultPart} from './types';
import format from './format';
import getForcedCaseFn from './get-forced-case-fn';
import getForcedLeadingFn from './get-forced-leading-fn';
import meta from './meta';
import {
enumRuleIsActive,
getHasName,
getMaxLength,
ruleIsActive,
ruleIsApplicable,
ruleIsNotApplicable,
} from './utils';
const EOL = '\n';
/**
* Get a cli prompt based on rule configuration
* @param type type of the data to gather
* @param rules
* @param settings
* @return prompt instance
*/
export default function getPrompt(
type: ResultPart,
rules: RuleEntry[] = [],
settings: InputSetting = {}
): InputCustomOptions<Result> | null {
const emptyRule = rules.filter(getHasName('empty')).find(ruleIsActive);
const mustBeEmpty = emptyRule ? ruleIsApplicable(emptyRule) : false;
if (mustBeEmpty) {
return null;
}
const required = emptyRule ? ruleIsNotApplicable(emptyRule) : false;
const forceCaseFn = getForcedCaseFn(rules.find(getHasName('case')));
const forceLeadingBlankFn = getForcedLeadingFn(
rules.find(getHasName('leading-blank'))
);
const maxLengthRule = rules.find(getHasName('max-length'));
const inputMaxLength = getMaxLength(maxLengthRule);
const enumRule = rules.filter(getHasName('enum')).find(enumRuleIsActive);
const tabCompletion = enumRule
? enumRule[1][2].map((enumerable) => {
const enumSettings = (settings.enumerables || {})[enumerable] || {};
return {
value: forceLeadingBlankFn(forceCaseFn(enumerable)),
description: enumSettings.description || '',
};
})
: [];
const maxLength = (res: Result) => {
let remainingHeaderLength = Infinity;
if (settings.header && settings.header.length) {
const header = format({
type: res.type,
scope: res.scope,
subject: res.subject,
});
remainingHeaderLength = settings.header.length - header.length;
}
return Math.min(inputMaxLength, remainingHeaderLength);
};
return {
type: 'input-custom',
name: type,
message: `${type}:`,
validate(input, answers) {
if (input.length > maxLength(answers || {})) {
return 'Input contains too many characters!';
}
if (required && input.trim().length === 0) {
// Show help if enum is defined and input may not be empty
return `β ${chalk.bold(type)} may not be empty.`;
}
const tabValues = tabCompletion.map((item) => item.value);
if (
input.length > 0 &&
tabValues.length > 0 &&
!tabValues.includes(input)
) {
return `β ${chalk.bold(type)} must be one of ${tabValues.join(', ')}.`;
}
return true;
},
tabCompletion,
log(answers?: Result) {
let prefix =
`${chalk.white('Please enter a')} ${chalk.bold(type)}: ${meta({
optional: !required,
required: required,
'tab-completion': typeof enumRule !== 'undefined',
header: typeof settings.header !== 'undefined',
'multi-line': settings.multiline,
})}` + EOL;
if (settings.description) {
prefix += chalk.grey(`${settings.description}`) + EOL;
}
if (answers) {
prefix += EOL + `${format(answers, true)}` + EOL;
}
return prefix + EOL;
},
maxLength,
transformer(value: string) {
return forceCaseFn(value);
},
};
}