UNPKG

@coat/cli

Version:

TODO: See #3

142 lines (141 loc) 6.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateCoatManifestSetup = validateCoatManifestSetup; 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* validateCoatManifestTask(task, index) { if (typeof task !== "object" || Array.isArray(task) || task === null) { yield { type: _validationIssue.ValidationIssueType.Error, message: `${(0, _formatPropertyPath.formatPropertyPath)(["setup", index])} must be an object.`, propertyPath: ["setup", index], shortMessage: "must be an object." }; return; } const { id, run, local, shouldRun, ...additionalProps } = task; const additionalPropKeys = new Set(Object.keys(additionalProps).filter(prop => prop !== "runOnCi")); if (typeof id === "undefined") { const potentialPropertyMatch = (0, _findPotentialPropertyMatch.findPotentialPropertyMatch)("id", [...additionalPropKeys]); if (potentialPropertyMatch) { additionalPropKeys.delete(potentialPropertyMatch); yield { type: _validationIssue.ValidationIssueType.Error, message: (0, _chalk.default)`${(0, _formatPropertyPath.formatPropertyPath)(["setup", index, "id"])} must be a non-empty string. Did you misspell {magenta ${potentialPropertyMatch}}?`, propertyPath: ["setup", index, potentialPropertyMatch], shortMessage: (0, _chalk.default)`did you mean to write {magenta id}?` }; } else { yield { type: _validationIssue.ValidationIssueType.Error, message: (0, _chalk.default)`${(0, _formatPropertyPath.formatPropertyPath)(["setup", index])} must have a {magenta id} property with a non-empty string.`, propertyPath: ["setup", index], shortMessage: (0, _chalk.default)`must have a {magenta id} property with a non-empty string.` }; } } else if (typeof id !== "string" || !id) { yield { type: _validationIssue.ValidationIssueType.Error, message: `${(0, _formatPropertyPath.formatPropertyPath)(["setup", index, "id"])} must be a non-empty string.`, propertyPath: ["setup", index, "id"], shortMessage: "must be a non-empty string." }; } if (typeof run === "undefined") { const potentialPropertyMatch = (0, _findPotentialPropertyMatch.findPotentialPropertyMatch)("run", [...additionalPropKeys]); if (potentialPropertyMatch) { additionalPropKeys.delete(potentialPropertyMatch); yield { type: _validationIssue.ValidationIssueType.Error, message: (0, _chalk.default)`${(0, _formatPropertyPath.formatPropertyPath)(["setup", index, "run"])} must be a function. Did you misspell {magenta ${potentialPropertyMatch}}?`, propertyPath: ["setup", index, potentialPropertyMatch], shortMessage: (0, _chalk.default)`did you mean to write {magenta run}?` }; } else { yield { type: _validationIssue.ValidationIssueType.Error, message: (0, _chalk.default)`${(0, _formatPropertyPath.formatPropertyPath)(["setup", index])} must have a {magenta run} property with a function.`, propertyPath: ["setup", index], shortMessage: (0, _chalk.default)`must have a {magenta run} property with a function.` }; } } else if (typeof run !== "function") { yield { type: _validationIssue.ValidationIssueType.Error, message: `${(0, _formatPropertyPath.formatPropertyPath)(["setup", index, "run"])} must be a function.`, propertyPath: ["setup", index, "run"], shortMessage: "must be a function." }; } if (typeof local !== "undefined" && typeof local !== "boolean") { yield { type: _validationIssue.ValidationIssueType.Error, message: `${(0, _formatPropertyPath.formatPropertyPath)(["setup", index, "local"])} must be a boolean.`, propertyPath: ["setup", index, "local"], shortMessage: "must be a boolean." }; } if (task.local) { if (typeof task.runOnCi !== "undefined" && typeof task.runOnCi !== "boolean") { yield { type: _validationIssue.ValidationIssueType.Error, message: `${(0, _formatPropertyPath.formatPropertyPath)(["setup", index, "runOnCi"])} must be a boolean.`, propertyPath: ["setup", index, "runOnCi"], shortMessage: "must be a boolean." }; } } else if ("runOnCi" in task) { yield { type: _validationIssue.ValidationIssueType.Warning, message: `${(0, _formatPropertyPath.formatPropertyPath)(["setup", index, "runOnCi"])} is only used for local tasks, since global tasks are never run on CI.`, propertyPath: ["setup", index, "runOnCi"] }; } if (typeof shouldRun !== "undefined" && typeof shouldRun !== "function") { yield { type: _validationIssue.ValidationIssueType.Error, message: `${(0, _formatPropertyPath.formatPropertyPath)(["setup", index, "shouldRun"])} must be a function.`, propertyPath: ["setup", index, "shouldRun"], shortMessage: "must be a function." }; } const allOptionalProps = ["local", "shouldRun"]; if (task.local) { allOptionalProps.push("runOnCi"); } yield* (0, _handleUnknownProperties.handleUnknownProperties)({ allOptionalProps, declaredProps: Object.keys(task), unknownProps: [...additionalPropKeys], propertyPrefixPath: ["setup", index] }); } function* validateCoatManifestSetup(setup) { if (typeof setup === "undefined") { return; } if (!Array.isArray(setup)) { yield { type: _validationIssue.ValidationIssueType.Error, message: `${(0, _formatPropertyPath.formatPropertyPath)(["setup"])} must be an array.`, propertyPath: ["setup"], shortMessage: "must be an array." }; return; } for (const [index, task] of setup.entries()) { yield* validateCoatManifestTask(task, index); } }