UNPKG

5.37 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const sdk_1 = require("@cto.ai/sdk");
5const debug_1 = tslib_1.__importDefault(require("debug"));
6const json = tslib_1.__importStar(require("JSONStream"));
7const through = tslib_1.__importStar(require("through2"));
8const CustomErrors_1 = require("../errors/CustomErrors");
9const get_docker_1 = tslib_1.__importDefault(require("../utils/get-docker"));
10const debug = debug_1.default('ops:ImageService');
11class Publish {
12 constructor() {
13 this.publishOpToAPI = async (op, version, teamID, accessToken, api, isGlueCode = false) => {
14 try {
15 const res = await api.create('ops', Object.assign({}, op, { version, teamID, isGlueCode, isPublic: op.isPublic }), {
16 headers: {
17 Authorization: accessToken,
18 },
19 });
20 return res;
21 }
22 catch (err) {
23 throw new CustomErrors_1.CouldNotCreateOp(err.message);
24 }
25 };
26 this.publishOpToRegistry = async (apiOp, registryAuth, teamName, accessToken, registryAuthService, version) => {
27 const imageUniqueId = `${registryAuth.projectFullName}/${apiOp.id.toLowerCase()}`;
28 const imageName = `${registryAuth.projectFullName}/${apiOp.name}`;
29 const self = this;
30 const docker = await get_docker_1.default(self, 'publish');
31 try {
32 if (!docker) {
33 throw new Error('Could not initialize Docker.');
34 }
35 // getImage always returns an image. Must listImages
36 const image = docker.getImage(imageName);
37 if (!image) {
38 throw new CustomErrors_1.DockerPublishNoImageFound(apiOp.name, teamName);
39 }
40 console.log(`šŸ”‹ Creating release ${sdk_1.ux.colors.callOutCyan(imageUniqueId)}... \n`);
41 const all = [];
42 let size = 0;
43 const seenChunks = {};
44 const parser = through.obj(function (chunk, _enc, cb) {
45 this.push(chunk.status);
46 if (chunk.aux) {
47 console.log(`\nšŸš€ ${sdk_1.ux.colors.white('Publishing...')}\n`);
48 console.log(`${sdk_1.ux.colors.green('>')} Tag: ${sdk_1.ux.colors.multiBlue(chunk.aux.Tag)}`);
49 console.log(`${sdk_1.ux.colors.green('>')} Size: ${sdk_1.ux.colors.multiBlue(chunk.aux.Size)}`);
50 console.log(`${sdk_1.ux.colors.green('>')} Digest: ${sdk_1.ux.colors.multiBlue(chunk.aux.Digest)}\n`);
51 size = chunk.aux.Size;
52 }
53 else if (chunk.id) {
54 const chunkString = `${chunk.status}: ${sdk_1.ux.colors.white(chunk.id)}`;
55 if (!seenChunks[chunkString]) {
56 console.log(`${chunk.status}: ${sdk_1.ux.colors.white(chunk.id)}`);
57 seenChunks[chunkString] = true;
58 }
59 }
60 cb();
61 });
62 const _pipe = parser.pipe;
63 parser.pipe = function (dest) {
64 return _pipe(dest);
65 };
66 await new Promise((res, rej) => {
67 image.tag({ repo: imageUniqueId }, (err, _data) => {
68 if (err) {
69 rej(new CustomErrors_1.ImageTagError(err));
70 }
71 const image = docker.getImage(imageUniqueId);
72 image.push({
73 tag: 'latest',
74 authconfig: registryAuth.authconfig,
75 }, (err, stream) => {
76 if (err) {
77 rej(new CustomErrors_1.ImagePushError(err));
78 }
79 stream
80 .pipe(json.parse())
81 .pipe(parser)
82 .on('data', (d) => {
83 all.push(d);
84 })
85 .on('end', async function () {
86 const bar = sdk_1.ux.progress.init();
87 bar.start(100, 0);
88 for (let i = 0; i < size; i++) {
89 bar.update(100 - size / i);
90 await sdk_1.ux.wait(5);
91 }
92 bar.update(100);
93 bar.stop();
94 console.log(`\nšŸ™Œ ${sdk_1.ux.colors.callOutCyan(imageUniqueId)} has been published! \n`);
95 await registryAuthService.delete(accessToken, registryAuth.robotID, teamName, apiOp.name, version);
96 res();
97 });
98 });
99 });
100 });
101 }
102 catch (err) {
103 debug('%O', err);
104 process.exit(1);
105 }
106 };
107 }
108}
109exports.Publish = Publish;