UNPKG

2.67 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const fs = tslib_1.__importStar(require("fs-extra"));
5const path = tslib_1.__importStar(require("path"));
6const debug_1 = tslib_1.__importDefault(require("debug"));
7const CustomErrors_1 = require("../errors/CustomErrors");
8const debug = debug_1.default('ops:BuildStepsService');
9class BuildSteps {
10 constructor() {
11 this.isGlueCode = async (step) => {
12 if (step === '') {
13 return false;
14 }
15 return !this.isOpRun(step);
16 };
17 this.buildAndPublishGlueCode = async (step, teamID, teamName, accessToken, opPath, user, publishService, opService, featherClient, registryAuthService, config, isPublic, version) => {
18 const indexJs = path.resolve(opPath, 'index.js');
19 fs.writeFileSync(indexJs, `const { sdk } = require('@cto.ai/sdk')
20
21 async function main() {
22 sdk.log('${step}')
23 }
24
25 main()`);
26 const rand = Math.random()
27 .toString(36)
28 .substring(7);
29 const opName = `gluecode-${rand}`;
30 const glueCodeOp = {
31 bind: ['/tmp:/tmp', 'Dockerfile'],
32 description: 'glue code',
33 mountCwd: false,
34 mountHome: false,
35 name: opName,
36 network: 'host',
37 run: '/bin/sdk-daemon node /ops/index.js',
38 src: ['Dockerfile', 'index.js', 'package.json', '.dockerignore'],
39 isPublic: isPublic,
40 };
41 const glueCodeClone = JSON.parse(JSON.stringify(glueCodeOp));
42 await opService.opsBuildLoop([glueCodeClone], path.resolve(__dirname, '../templates/workflowsteps/js'), config);
43 const { data: apiOp } = await publishService.publishOpToAPI(glueCodeOp, version, teamID, accessToken, featherClient, true);
44 const registryAuth = await registryAuthService
45 .create(accessToken, teamName, glueCodeOp.name, version, false, true)
46 .catch(err => {
47 throw new CustomErrors_1.CouldNotGetRegistryToken(err);
48 });
49 await publishService.publishOpToRegistry(apiOp, registryAuth, teamName, accessToken, registryAuthService, version);
50 return `ops run ${opName}`;
51 };
52 }
53 isOpRun(step) {
54 const processedStep = step
55 .toLowerCase()
56 .replace(/\s\s+/g, ' ')
57 .trim();
58 const opsRunPrefix = 'ops run';
59 return processedStep.lastIndexOf(opsRunPrefix, 0) === 0;
60 }
61}
62exports.BuildSteps = BuildSteps;