@commitlint/prompt
Version:
commitizen prompt using commitlint.config.js
92 lines • 3.69 kB
JavaScript
import chalk from "chalk";
import format from "./format.js";
import getForcedCaseFn from "./get-forced-case-fn.js";
import getForcedLeadingFn from "./get-forced-leading-fn.js";
import meta from "./meta.js";
import { enumRuleIsActive, getHasName, getMaxLength, ruleIsActive, ruleIsApplicable, ruleIsNotApplicable, } from "./utils.js";
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, rules = [], settings = {}) {
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) => {
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) {
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) {
return forceCaseFn(value);
},
forceLeadingBlankFn,
};
}
//# sourceMappingURL=get-prompt.js.map