UNPKG

@cto.ai/ops

Version:

💻 CTO.ai Ops - The CLI built for Teams 🚀

93 lines (92 loc) 4.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const sdk_1 = require("@cto.ai/sdk"); const yaml = tslib_1.__importStar(require("yaml")); const CustomErrors_1 = require("../errors/CustomErrors"); const opConfig_1 = require("../constants/opConfig"); const { white, callOutCyan } = sdk_1.ux.colors; const checkCommandNecessryFields = (commandContents) => { if (!commandContents.run || typeof commandContents.run !== 'string') { throw new CustomErrors_1.IncompleteOpsYml('The run command must be included as a string'); } }; const checkWorkflowNecessryFields = (workflowContents) => { if (!workflowContents.steps || !workflowContents.steps.length) { throw new CustomErrors_1.IncompleteOpsYml('The run command must be included as a string'); } workflowContents.steps.forEach(step => { if (!step || typeof step !== 'string') { throw new CustomErrors_1.IncompleteOpsYml('Each step should be a non-empty string'); } }); }; const checkOpNecessaryFields = (opContents) => { if (!opContents.name || typeof opContents.name !== 'string') { throw new CustomErrors_1.IncompleteOpsYml('Op name must be a non-empty string'); } if (!opContents.description || typeof opContents.description !== 'string') { throw new CustomErrors_1.IncompleteOpsYml('Op description must be a non-empty string'); } if (!opContents.isPublic === undefined || typeof opContents.isPublic !== 'boolean') { throw new CustomErrors_1.IncompleteOpsYml('Your ops.yml file is missing the public field, please add `public:false` to publish your op as private'); } return true; }; const _splitOpNameAndVersion = (op) => { const splits = op.name.split(':'); return [splits[0], splits[1]]; }; exports.parseYaml = (manifest) => { const { version, ops = [], workflows = [], commands = [] } = manifest && yaml.parse(manifest); let yamlContents = { version, ops: [], workflows: [], }; if (ops.length > 0) { console.log(white(`It looks like your ops.yml is a little out of date.\nYou should replace the ${callOutCyan('ops')} field with ${callOutCyan('commands')}.\nLearn more here ${sdk_1.ux.url('https://cto.ai/docs/ops-reference', 'https://cto.ai/docs/ops-reference')}`)); const parsedOps = ops.map(op => { const newCommand = formatRequiredFields(op, opConfig_1.COMMAND_TYPE); checkOpNecessaryFields(newCommand); checkCommandNecessryFields(newCommand); return newCommand; }); yamlContents.ops = [...yamlContents.ops, ...parsedOps]; } if (commands.length > 0) { const parsedCommands = commands.map(op => { const newCommand = formatRequiredFields(op, opConfig_1.COMMAND_TYPE); checkOpNecessaryFields(newCommand); checkCommandNecessryFields(newCommand); return newCommand; }); yamlContents.ops = [...yamlContents.ops, ...parsedCommands]; } if (workflows.length > 0) { yamlContents.workflows = workflows.map(wf => { const newWf = formatRequiredFields(wf, opConfig_1.WORKFLOW_TYPE); checkOpNecessaryFields(newWf); checkWorkflowNecessryFields(newWf); return newWf; }); } return yamlContents; }; const formatRequiredFields = (opOrWorkflow, type) => { const defaultVersion = `0.1.0`; const defaultVersionLog = `\nℹ️ It looks like your ops.yml is a little out of date. It does not have a version, we are setting the default version to ${sdk_1.ux.colors.callOutCyan(defaultVersion)}. Learn more ${sdk_1.ux.url('here', 'https://cto.ai/docs/ops-reference')}.\n`; const newOp = Object.assign({}, opOrWorkflow); newOp.isPublic = newOp.public; delete newOp.public; let [opName, opVersion] = _splitOpNameAndVersion(opOrWorkflow); if (!opVersion) { opVersion = defaultVersion; console.log(defaultVersionLog); } newOp.name = opName; newOp.version = opVersion; newOp.type = type; return newOp; };