@coat/cli
Version:
TODO: See #3
91 lines (90 loc) • 3.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.validateCoatManifestScripts = validateCoatManifestScripts;
var _chalk = _interopRequireDefault(require("chalk"));
var _findPotentialPropertyMatch = require("../find-potential-property-match");
var _formatPropertyPath = require("../format-property-path");
var _handleUnknownProperties = require("../handle-unknown-properties");
var _validationIssue = require("../validation-issue");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function* validateCoatManifestScript(script, index) {
if (typeof script !== "object" || Array.isArray(script) || script === null) {
yield {
type: _validationIssue.ValidationIssueType.Error,
message: `${(0, _formatPropertyPath.formatPropertyPath)(["scripts", index])} must be an object.`,
propertyPath: ["scripts", index],
shortMessage: "must be an object."
};
return;
}
const {
id,
run,
scriptName,
...additionalProps
} = script;
const additionalPropKeys = new Set(Object.keys(additionalProps));
const valuesToValidate = [{
value: id,
prop: "id"
}, {
value: run,
prop: "run"
}, {
value: scriptName,
prop: "scriptName"
}];
for (const valueToValidate of valuesToValidate) {
if (typeof valueToValidate.value === "undefined") {
const potentialPropertyMatch = (0, _findPotentialPropertyMatch.findPotentialPropertyMatch)(valueToValidate.prop, [...additionalPropKeys]);
if (potentialPropertyMatch) {
additionalPropKeys.delete(potentialPropertyMatch);
yield {
type: _validationIssue.ValidationIssueType.Error,
message: (0, _chalk.default)`${(0, _formatPropertyPath.formatPropertyPath)(["scripts", index, valueToValidate.prop])} must be a non-empty string. Did you misspell {magenta ${potentialPropertyMatch}}?`,
propertyPath: ["scripts", index, potentialPropertyMatch],
shortMessage: (0, _chalk.default)`did you mean to write {magenta ${valueToValidate.prop}}?`
};
} else {
yield {
type: _validationIssue.ValidationIssueType.Error,
message: (0, _chalk.default)`${(0, _formatPropertyPath.formatPropertyPath)(["scripts", index])} must have a {magenta ${valueToValidate.prop}} property with a non-empty string.`,
propertyPath: ["scripts", index],
shortMessage: (0, _chalk.default)`must have a {magenta ${valueToValidate.prop}} property with a non-empty string.`
};
}
} else if (typeof valueToValidate.value !== "string" || !valueToValidate.value) {
yield {
type: _validationIssue.ValidationIssueType.Error,
message: `${(0, _formatPropertyPath.formatPropertyPath)(["scripts", index, valueToValidate.prop])} must be a non-empty string.`,
propertyPath: ["scripts", index, valueToValidate.prop],
shortMessage: "must be a non-empty string."
};
}
}
yield* (0, _handleUnknownProperties.handleUnknownProperties)({
allOptionalProps: [],
declaredProps: Object.keys(script),
unknownProps: [...additionalPropKeys],
propertyPrefixPath: ["scripts", index]
});
}
function* validateCoatManifestScripts(scripts) {
if (typeof scripts === "undefined") {
return;
}
if (!Array.isArray(scripts)) {
yield {
type: _validationIssue.ValidationIssueType.Error,
message: `${(0, _formatPropertyPath.formatPropertyPath)(["scripts"])} must be an array.`,
propertyPath: ["scripts"],
shortMessage: "must be an array."
};
return;
}
for (const [index, script] of scripts.entries()) {
yield* validateCoatManifestScript(script, index);
}
}