1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const tslib_1 = require("tslib");
|
4 | const archiver = require("archiver");
|
5 | const axios_1 = require("axios");
|
6 | const fs = require("fs-extra");
|
7 | const globby = require("globby");
|
8 | const Listr = require("listr");
|
9 | const base_command_1 = require("../base-command");
|
10 | const decorators_1 = require("../utils/decorators");
|
11 | const helpers_1 = require("../utils/helpers");
|
12 | class Push extends base_command_1.default {
|
13 | async run() {
|
14 | helpers_1.ensureFolderExists(this.locator.buildArtifactDir, true);
|
15 | const archivePath = this.locator.buildArtifactResourcePath('integration.zip');
|
16 | const tasks = [
|
17 | {
|
18 | title: 'Generate bundle',
|
19 | task: async (_ctx) => this.archive(archivePath)
|
20 | },
|
21 | {
|
22 | title: 'Transfer bundle',
|
23 | task: async (_ctx) => this.transfer(archivePath)
|
24 | }
|
25 | ];
|
26 | try {
|
27 | await new Listr(tasks).run();
|
28 | this.success(`🐻 Integration successfully pushed.\n`);
|
29 | this.log(
|
30 |
|
31 | `Your Integration will be available shortly here: ` +
|
32 | this.colors.bold(`${this.bearerConfig.DeveloperPortalUrl}integrations/${this.bearerConfig.integrationUuid}/preview`));
|
33 | this.log(
|
34 |
|
35 | `\nIn the meantime you can follow the deployment here: ` +
|
36 | this.colors.bold(`${this.bearerConfig.DeveloperPortalUrl}integrations/${this.bearerConfig.integrationUuid}/logs`));
|
37 | }
|
38 | catch (e) {
|
39 | this.error(e);
|
40 | }
|
41 | }
|
42 | async archive(archivePath) {
|
43 | return new Promise(async (resolve, reject) => {
|
44 | const output = fs.createWriteStream(archivePath);
|
45 | const archive = archiver('zip', {
|
46 | zlib: { level: 9 }
|
47 | });
|
48 |
|
49 | archive.pipe(output);
|
50 | const files = await globby([
|
51 | 'views/**/*',
|
52 | 'functions/**/*.ts',
|
53 | 'functions/tsconfig.json',
|
54 | 'yarn.lock',
|
55 | 'package-json.lock',
|
56 | 'spec.ts',
|
57 | 'package.json',
|
58 | 'auth.config.json'
|
59 | ]);
|
60 | this.debug('Files to upload', files.join('\n'));
|
61 | if (files.length >= 100) {
|
62 | return reject(new Error('Too many files to bundle. Please re-run this command this DEBUG=*'));
|
63 | }
|
64 | output.on('close', () => {
|
65 | this.debug(`Archive created: ${archive.pointer() / 1024} Kb / ${archivePath}`);
|
66 | resolve(archivePath);
|
67 | });
|
68 | archive.on('error', (err) => {
|
69 | reject(err);
|
70 | });
|
71 | archive.on('warning', (err) => {
|
72 | if (err.code === 'ENOENT') {
|
73 | reject(err);
|
74 | }
|
75 | else {
|
76 | this.debug(err);
|
77 | }
|
78 | });
|
79 | files.map(file => {
|
80 | archive.file(file, { name: file });
|
81 | });
|
82 | archive.finalize();
|
83 | });
|
84 | }
|
85 | async getSignedUrl() {
|
86 | return this.integrationClient.getIntegrationArchiveUploadUrl(this.bearerConfig.orgId, this.bearerConfig.integrationId);
|
87 | }
|
88 | async transfer(archivePath) {
|
89 | try {
|
90 | const url = await this.getSignedUrl();
|
91 | this.debug(url);
|
92 | const file = fs.readFileSync(archivePath);
|
93 | await axios_1.default.put(url, file, { headers: { 'Content-Type': 'application/zip' } });
|
94 | return true;
|
95 | }
|
96 | catch (e) {
|
97 | if (e.response) {
|
98 | this.debug(e.response);
|
99 | switch (e.response.status) {
|
100 | case 401: {
|
101 | this.error(`Unauthorized to push, please visit ${this.bearerConfig.DeveloperPortalUrl}integrations/${this.bearerConfig.integrationUuid} to confirm you have access to ${this.colors.bold(this.bearerConfig.integrationUuid)} integration.`);
|
102 | }
|
103 | default: {
|
104 | this.log(e.response.data);
|
105 | }
|
106 | }
|
107 | }
|
108 | this.error(e);
|
109 | return false;
|
110 | }
|
111 | }
|
112 | }
|
113 | Push.description = 'Deploy Integration to Bearer Platform';
|
114 | Push.flags = Object.assign({}, base_command_1.default.flags);
|
115 | Push.args = [];
|
116 | tslib_1.__decorate([
|
117 | decorators_1.RequireIntegrationFolder(),
|
118 | decorators_1.RequireLinkedIntegration(),
|
119 | decorators_1.ensureFreshToken()
|
120 | ], Push.prototype, "run", null);
|
121 | exports.default = Push;
|