@cto.ai/ops
Version:
💻 CTO.ai Ops - The CLI built for Teams 🚀
74 lines (71 loc) • 3.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const fs = tslib_1.__importStar(require("fs-extra"));
const path = tslib_1.__importStar(require("path"));
const debug_1 = tslib_1.__importDefault(require("debug"));
const CustomErrors_1 = require("../errors/CustomErrors");
const opConfig_1 = require("../constants/opConfig");
const debug = debug_1.default('ops:BuildStepsService');
class BuildSteps {
constructor() {
this.isGlueCode = async (step) => {
if (step === '') {
return false;
}
return !this.isOpRun(step);
};
this.buildAndPublishGlueCode = async (step, teamID, teamName, accessToken, opPath, user, publishService, opService, featherClient, registryAuthService, config, isPublic, version) => {
const indexJs = path.resolve(opPath, 'index.js');
fs.writeFileSync(indexJs, `const { sdk } = require('@cto.ai/sdk')
async function main() {
sdk.log('${step}')
}
main()`);
const rand = Math.random()
.toString(36)
.substring(7);
const opName = `gluecode-${rand}`;
const glueCodeOp = {
bind: ['/tmp:/tmp', 'Dockerfile'],
description: 'glue code',
publishDescription: 'glue code',
mountCwd: false,
mountHome: false,
name: opName,
network: 'host',
version: 'latest',
run: '/bin/sdk-daemon node /ops/index.js',
src: ['Dockerfile', 'index.js', 'package.json', '.dockerignore'],
isPublic: isPublic,
type: opConfig_1.GLUECODE_TYPE,
};
const glueCodeClone = JSON.parse(JSON.stringify(glueCodeOp));
await opService.opsBuildLoop([glueCodeClone], path.resolve(__dirname, '../templates/workflowsteps/js'), config);
if (!('run' in glueCodeOp)) {
throw new CustomErrors_1.InvalidGlueCode();
}
const { data: apiOp } = await publishService.publishOpToAPI(glueCodeOp, '1', teamName, accessToken, featherClient, true);
const registryAuth = await registryAuthService
.create(accessToken, teamName, glueCodeOp.name, 'latest', false, true)
.catch(err => {
throw new CustomErrors_1.CouldNotGetRegistryToken(err);
});
await publishService
.publishOpToRegistry(apiOp, registryAuth, teamName, accessToken, registryAuthService, featherClient, version)
.catch(err => {
throw new CustomErrors_1.CouldNotGetRegistryToken(err);
});
return `ops run @${teamName}/${opName}:${apiOp.version}`;
};
}
isOpRun(step) {
const processedStep = step
.toLowerCase()
.replace(/\s\s+/g, ' ')
.trim();
const opsRunPrefix = 'ops run';
return processedStep.lastIndexOf(opsRunPrefix, 0) === 0;
}
}
exports.BuildSteps = BuildSteps;