UNPKG

@grouparoo/core

Version:
75 lines (74 loc) 3.59 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Validate = void 0; const cli_1 = require("../modules/cli"); const actionhero_1 = require("actionhero"); const configLoaders_1 = require("../modules/configLoaders"); const pluginDetails_1 = require("../modules/pluginDetails"); const pluralize_1 = __importDefault(require("pluralize")); const setting_1 = require("../modules/ops/setting"); class Validate extends actionhero_1.CLI { constructor() { super(); this.name = "validate"; this.description = "Validate your code config"; this.inputs = { local: { description: "Disable external validation. You can optionally pass object IDs to only disable external validation for those specific config objects and their dependents.", letter: "l", variadic: true, placeholder: "object ids", }, }; this.preInitialize = () => { cli_1.GrouparooCLI.setGrouparooRunMode(this); cli_1.GrouparooCLI.setNextDevelopmentMode(); }; cli_1.GrouparooCLI.timestampOption(this); } async run({ params }) { var _a; cli_1.GrouparooCLI.logCLI(this.name); await setting_1.SettingOps.prepare(); const configDir = await (0, pluginDetails_1.getConfigDir)(true); let configObjects; // Can we read the config directory? Is the JSON/JS valid? try { const loadResponse = await (0, configLoaders_1.loadConfigObjects)(configDir); if (loadResponse.errors.length > 0) { throw new Error(loadResponse.errors.join("; ")); } configObjects = loadResponse.configObjects; } catch (error) { return cli_1.GrouparooCLI.logger.fatal(`Error loading config from ${configDir}: \r\n\r\n${error.stack}`); } cli_1.GrouparooCLI.logger.log(`Validating ${configObjects.length} ${(0, pluralize_1.default)("object", configObjects.length)}...`); const canExternallyValidate = ((_a = params === null || params === void 0 ? void 0 : params.local) === null || _a === void 0 ? void 0 : _a.toString()) !== "true"; const locallyValidateIds = Array.isArray(params.local) && new Set(params.local); try { await actionhero_1.api.sequelize.transaction(async () => { const { errors } = await (0, configLoaders_1.processConfigObjects)(configObjects, canExternallyValidate, locallyValidateIds, true); if (errors.length > 0) { cli_1.GrouparooCLI.logger.log(""); return cli_1.GrouparooCLI.logger.fatal(`Validation failed - ${errors.length} validation ${(0, pluralize_1.default)("errors", errors.length)}`); } else { cli_1.GrouparooCLI.logger.log(""); cli_1.GrouparooCLI.logger.log(`✅ Validation succeeded - ${configObjects.length} config ${(0, pluralize_1.default)("object", configObjects.length)} OK!`); } throw new Error("validate-rollback"); }); } catch (error) { if (error.message !== "validate-rollback") { return cli_1.GrouparooCLI.logger.fatal(`Validation failed - ${error.message}`); } } return true; } } exports.Validate = Validate;