UNPKG

4.17 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6const chalk_1 = __importDefault(require("chalk"));
7const format_1 = __importDefault(require("./format"));
8const get_forced_case_fn_1 = __importDefault(require("./get-forced-case-fn"));
9const get_forced_leading_fn_1 = __importDefault(require("./get-forced-leading-fn"));
10const meta_1 = __importDefault(require("./meta"));
11const utils_1 = require("./utils");
12const EOL = '\n';
13/**
14 * Get a cli prompt based on rule configuration
15 * @param type type of the data to gather
16 * @param rules
17 * @param settings
18 * @return prompt instance
19 */
20function getPrompt(type, rules = [], settings = {}) {
21 const emptyRule = rules.filter((0, utils_1.getHasName)('empty')).find(utils_1.ruleIsActive);
22 const mustBeEmpty = emptyRule ? (0, utils_1.ruleIsApplicable)(emptyRule) : false;
23 if (mustBeEmpty) {
24 return null;
25 }
26 const required = emptyRule ? (0, utils_1.ruleIsNotApplicable)(emptyRule) : false;
27 const forceCaseFn = (0, get_forced_case_fn_1.default)(rules.find((0, utils_1.getHasName)('case')));
28 const forceLeadingBlankFn = (0, get_forced_leading_fn_1.default)(rules.find((0, utils_1.getHasName)('leading-blank')));
29 const maxLengthRule = rules.find((0, utils_1.getHasName)('max-length'));
30 const inputMaxLength = (0, utils_1.getMaxLength)(maxLengthRule);
31 const enumRule = rules.filter((0, utils_1.getHasName)('enum')).find(utils_1.enumRuleIsActive);
32 const tabCompletion = enumRule
33 ? enumRule[1][2].map((enumerable) => {
34 const enumSettings = (settings.enumerables || {})[enumerable] || {};
35 return {
36 value: forceLeadingBlankFn(forceCaseFn(enumerable)),
37 description: enumSettings.description || '',
38 };
39 })
40 : [];
41 const maxLength = (res) => {
42 let remainingHeaderLength = Infinity;
43 if (settings.header && settings.header.length) {
44 const header = (0, format_1.default)({
45 type: res.type,
46 scope: res.scope,
47 subject: res.subject,
48 });
49 remainingHeaderLength = settings.header.length - header.length;
50 }
51 return Math.min(inputMaxLength, remainingHeaderLength);
52 };
53 return {
54 type: 'input-custom',
55 name: type,
56 message: `${type}:`,
57 validate(input, answers) {
58 if (input.length > maxLength(answers || {})) {
59 return 'Input contains too many characters!';
60 }
61 if (required && input.trim().length === 0) {
62 // Show help if enum is defined and input may not be empty
63 return `⚠ ${chalk_1.default.bold(type)} may not be empty.`;
64 }
65 const tabValues = tabCompletion.map((item) => item.value);
66 if (input.length > 0 &&
67 tabValues.length > 0 &&
68 !tabValues.includes(input)) {
69 return `⚠ ${chalk_1.default.bold(type)} must be one of ${tabValues.join(', ')}.`;
70 }
71 return true;
72 },
73 tabCompletion,
74 log(answers) {
75 let prefix = `${chalk_1.default.white('Please enter a')} ${chalk_1.default.bold(type)}: ${(0, meta_1.default)({
76 optional: !required,
77 required: required,
78 'tab-completion': typeof enumRule !== 'undefined',
79 header: typeof settings.header !== 'undefined',
80 'multi-line': settings.multiline,
81 })}` + EOL;
82 if (settings.description) {
83 prefix += chalk_1.default.grey(`${settings.description}`) + EOL;
84 }
85 if (answers) {
86 prefix += EOL + `${(0, format_1.default)(answers, true)}` + EOL;
87 }
88 return prefix + EOL;
89 },
90 maxLength,
91 transformer(value) {
92 return forceCaseFn(value);
93 },
94 };
95}
96exports.default = getPrompt;
97//# sourceMappingURL=get-prompt.js.map
\No newline at end of file