commitiquette
Version:
Plugin for commitizen to use commitLint config
107 lines (106 loc) • 3.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.scopeMaker = exports.filterFactory = exports.choicesFactory = exports.validatorFactory = void 0;
var tslib_1 = require("tslib");
var types_1 = require("@commitlint/types");
var when_1 = require("../when");
var validators_1 = require("../validators");
var filters_1 = require("../filters");
function validatorFactory(rules) {
return function (value) {
return (0, validators_1.validate)([
{
value: value,
rule: rules['scope-max-length'],
validator: validators_1.maxLengthValidator,
message: function (length) { return "Scope maximum length of " + length + " has been exceeded"; },
},
{
value: value,
rule: rules['scope-min-length'],
validator: validators_1.minLengthValidator,
message: function (length) { return "Scope minimum length of " + length + " has not been met"; },
},
{
value: value,
rule: rules['scope-empty'],
validator: validators_1.emptyValidator,
message: function () { return 'Scope cannot be empty'; },
},
{
value: value,
rule: rules['scope-case'],
validator: validators_1.caseValidator,
message: function (ruleValue, applicable) { return "Scope must " + (applicable == 'never' ? 'not ' : '') + "be in " + ruleValue; },
},
]);
};
}
exports.validatorFactory = validatorFactory;
function parseEmptyScopeRule(rule) {
var skipChoice = { name: ':skip', value: '' };
if (rule !== undefined) {
var level = rule[0], applicability = rule[1];
if (level === types_1.RuleConfigSeverity.Error) {
if (applicability === 'always') {
return [true, skipChoice];
}
return [false, undefined];
}
}
return [true, skipChoice];
}
function parseScopeEnumRule(rule) {
if (rule !== undefined) {
var scopeEnum = rule[2];
return [true, (scopeEnum !== null && scopeEnum !== void 0 ? scopeEnum : []).map(function (scope) { return ({ name: scope, value: scope }); })];
}
return [false, undefined];
}
function choicesFactory(rules) {
var choices = [];
var _a = parseEmptyScopeRule(rules['scope-empty']), containsSkipChoice = _a[0], skipChoice = _a[1];
if (containsSkipChoice) {
choices.push(skipChoice);
}
var _b = parseScopeEnumRule(rules['scope-enum']), containsScopeEnumChoices = _b[0], scopeEnumChoices = _b[1];
if (containsScopeEnumChoices) {
choices.unshift.apply(choices, scopeEnumChoices);
}
return choices;
}
exports.choicesFactory = choicesFactory;
function filterFactory(rules) {
return function (value) { return (0, filters_1.wordCaseFilter)(value, rules['scope-case']); };
}
exports.filterFactory = filterFactory;
function scopeMaker(questions, rules) {
var name = 'scope';
var message = 'What is the scope of this change:\n';
var when = (0, when_1.whenFactory)(rules['scope-enum'], rules['scope-empty']);
var choices = choicesFactory(rules);
var question;
if (choices) {
question = {
name: name,
message: message,
when: when,
validate: validatorFactory(rules),
filter: filterFactory(rules),
choices: choices,
type: 'list',
};
}
else {
question = {
name: name,
message: message,
when: when,
validate: validatorFactory(rules),
filter: filterFactory(rules),
type: 'input',
};
}
return (0, tslib_1.__spreadArray)((0, tslib_1.__spreadArray)([], questions, true), [question], false);
}
exports.scopeMaker = scopeMaker;