@coat/cli
Version:
TODO: See #3
200 lines (199 loc) • 10.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.validateCoatManifestFiles = validateCoatManifestFiles;
var _chalk = _interopRequireDefault(require("chalk"));
var _coatManifestFile = require("../../types/coat-manifest-file");
var _validationIssue = require("../validation-issue");
var _findPotentialPropertyMatch = require("../find-potential-property-match");
var _handleUnknownProperties = require("../handle-unknown-properties");
var _formatPropertyPath = require("../format-property-path");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Dummy function to create an exhaustive switch statement that
* raises a compile time error when a new file type is added
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function exhaustiveTypeHelper(_type) {
// Empty function
}
function* validateCoatManifestFile(fileEntry, index) {
if (typeof fileEntry !== "object" || fileEntry === null || Array.isArray(fileEntry)) {
yield {
type: _validationIssue.ValidationIssueType.Error,
message: `${(0, _formatPropertyPath.formatPropertyPath)(["files", index])} must be an object.`,
propertyPath: ["files", index],
shortMessage: "must be an object."
};
return;
}
const {
file,
content,
type,
local,
once,
...additionalProps
} = fileEntry;
const additionalPropKeys = new Set(Object.keys(additionalProps));
if (typeof file !== "string" || !file.length) {
if (typeof file === "undefined") {
const potentialPropertyMatch = (0, _findPotentialPropertyMatch.findPotentialPropertyMatch)("file", [...additionalPropKeys]);
if (potentialPropertyMatch) {
additionalPropKeys.delete(potentialPropertyMatch);
yield {
type: _validationIssue.ValidationIssueType.Error,
message: (0, _chalk.default)`${(0, _formatPropertyPath.formatPropertyPath)(["files", index, "file"])} must be a relative path. Did you misspell {magenta ${potentialPropertyMatch}}?`,
propertyPath: ["files", index, potentialPropertyMatch],
shortMessage: (0, _chalk.default)`did you mean to write {magenta file}?`
};
} else {
yield {
type: _validationIssue.ValidationIssueType.Error,
message: (0, _chalk.default)`${(0, _formatPropertyPath.formatPropertyPath)(["files", index])} must have a {magenta file} property with the relative file path.`,
propertyPath: ["files", index],
shortMessage: (0, _chalk.default)`must have a {magenta file} property with a relative path.`
};
}
} else {
yield {
type: _validationIssue.ValidationIssueType.Error,
message: `${(0, _formatPropertyPath.formatPropertyPath)(["files", index, "file"])} must be a relative path.`,
propertyPath: ["files", index, "file"],
shortMessage: "must be a relative path."
};
}
}
switch (type) {
case _coatManifestFile.CoatManifestFileType.Json:
case _coatManifestFile.CoatManifestFileType.Yaml:
if (content !== null && typeof content !== "object" && typeof content !== "function" || Array.isArray(content)) {
if (typeof content === "undefined") {
const potentialPropertyMatch = (0, _findPotentialPropertyMatch.findPotentialPropertyMatch)("content", [...additionalPropKeys]);
if (potentialPropertyMatch) {
additionalPropKeys.delete(potentialPropertyMatch);
yield {
type: _validationIssue.ValidationIssueType.Error,
message: (0, _chalk.default)`${(0, _formatPropertyPath.formatPropertyPath)(["files", index, "content"])} must be null, a valid JSON object or a function that returns one, since the file.type is ${type}. Did you misspell {magenta ${potentialPropertyMatch}}?`,
propertyPath: ["files", index, potentialPropertyMatch],
shortMessage: (0, _chalk.default)`did you mean to write {magenta content}?`
};
} else {
yield {
type: _validationIssue.ValidationIssueType.Error,
message: (0, _chalk.default)`${(0, _formatPropertyPath.formatPropertyPath)(["files", index])} must have a {magenta content} property that is null, a valid JSON object or a function that returns one, since the file.type is ${type}.`,
propertyPath: ["files", index],
shortMessage: (0, _chalk.default)`must have a {magenta content} property that is null, a valid JSON object or a function that returns one.`
};
}
} else {
yield {
type: _validationIssue.ValidationIssueType.Error,
message: `${(0, _formatPropertyPath.formatPropertyPath)(["files", index, "content"])} must be null, a valid JSON object or a function that returns one, since the file.type is ${type}.`,
propertyPath: ["files", index, "content"],
shortMessage: "must be null, a valid JSON object or a function that returns one."
};
}
}
break;
case _coatManifestFile.CoatManifestFileType.Text:
if (content !== null && typeof content !== "string" && typeof content !== "function") {
if (typeof content === "undefined") {
const potentialPropertyMatch = (0, _findPotentialPropertyMatch.findPotentialPropertyMatch)("content", [...additionalPropKeys]);
if (potentialPropertyMatch) {
additionalPropKeys.delete(potentialPropertyMatch);
yield {
type: _validationIssue.ValidationIssueType.Error,
message: (0, _chalk.default)`${(0, _formatPropertyPath.formatPropertyPath)(["files", index, "content"])} must be null, a string or a function that returns one, since the file.type is TEXT. Did you misspell {magenta ${potentialPropertyMatch}}?`,
propertyPath: ["files", index, potentialPropertyMatch],
shortMessage: (0, _chalk.default)`did you mean to write {magenta content}?`
};
} else {
yield {
type: _validationIssue.ValidationIssueType.Error,
message: (0, _chalk.default)`${(0, _formatPropertyPath.formatPropertyPath)(["files", index])} must have a {magenta content} property that is null, a string or a function that returns one, since the file.type is TEXT.`,
propertyPath: ["files", index],
shortMessage: (0, _chalk.default)`must have a {magenta content} property that is null, a string or a function that returns one.`
};
}
} else {
yield {
type: _validationIssue.ValidationIssueType.Error,
message: `${(0, _formatPropertyPath.formatPropertyPath)(["files", index, "content"])} must be null, a string or a function that returns one, since the file.type is TEXT.`,
propertyPath: ["files", index, "content"],
shortMessage: "must be null, a string or a function that returns one."
};
}
}
break;
default:
exhaustiveTypeHelper(type);
if (typeof type === "undefined") {
const potentialPropertyMatch = (0, _findPotentialPropertyMatch.findPotentialPropertyMatch)("type", [...additionalPropKeys]);
if (potentialPropertyMatch) {
additionalPropKeys.delete(potentialPropertyMatch);
yield {
type: _validationIssue.ValidationIssueType.Error,
message: (0, _chalk.default)`${(0, _formatPropertyPath.formatPropertyPath)(["files", index, "type"])} must be either {green "JSON"}, {green "YAML"} or {green "TEXT"}. Did you misspell {magenta ${potentialPropertyMatch}}?`,
propertyPath: ["files", index, potentialPropertyMatch],
shortMessage: (0, _chalk.default)`did you mean to write {magenta type}?`
};
} else {
yield {
type: _validationIssue.ValidationIssueType.Error,
message: (0, _chalk.default)`${(0, _formatPropertyPath.formatPropertyPath)(["files", index])} must have a {magenta type} property that is either {green "JSON"}, {green "YAML"} or {green "TEXT"}.`,
propertyPath: ["files", index],
shortMessage: (0, _chalk.default)`must have a {magenta type} property that is either {green "JSON"}, {green "YAML"} or {green "TEXT"}.`
};
}
} else {
yield {
type: _validationIssue.ValidationIssueType.Error,
// NOTE: Update when new file types are added!
message: (0, _chalk.default)`${(0, _formatPropertyPath.formatPropertyPath)(["files", index, "type"])} must be either {green "JSON"}, {green "YAML"} or {green "TEXT"}.`,
propertyPath: ["files", index, "type"],
shortMessage: (0, _chalk.default)`must be either {green "JSON"}, {green "YAML"} or {green "TEXT"}.`
};
}
}
if (typeof once !== "undefined" && typeof once !== "boolean") {
yield {
type: _validationIssue.ValidationIssueType.Error,
message: `${(0, _formatPropertyPath.formatPropertyPath)(["files", index, "once"])} must be a boolean.`,
propertyPath: ["files", index, "once"],
shortMessage: "must be a boolean."
};
}
if (typeof local !== "undefined" && typeof local !== "boolean") {
yield {
type: _validationIssue.ValidationIssueType.Error,
message: `${(0, _formatPropertyPath.formatPropertyPath)(["files", index, "local"])} must be a boolean.`,
propertyPath: ["files", index, "local"],
shortMessage: "must be a boolean."
};
}
yield* (0, _handleUnknownProperties.handleUnknownProperties)({
allOptionalProps: ["local", "once"],
declaredProps: Object.keys(fileEntry),
unknownProps: [...additionalPropKeys],
propertyPrefixPath: ["files", index]
});
}
function* validateCoatManifestFiles(files) {
if (typeof files === "undefined") {
return;
}
if (!Array.isArray(files)) {
yield {
type: _validationIssue.ValidationIssueType.Error,
message: `${(0, _formatPropertyPath.formatPropertyPath)(["files"])} must be an array.`,
propertyPath: ["files"],
shortMessage: "must be an array."
};
return;
}
for (const [index, file] of files.entries()) {
yield* validateCoatManifestFile(file, index);
}
}