UNPKG

6.2 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const archiver = require("archiver");
5const axios_1 = require("axios");
6const fs = require("fs-extra");
7const globby = require("globby");
8const Listr = require("listr");
9const serviceClient_1 = require("@bearer/bearer-cli/lib/lib/serviceClient");
10const base_command_1 = require("../base-command");
11const decorators_1 = require("../utils/decorators");
12const helpers_1 = require("../utils/helpers");
13const integration_client_1 = require("../utils/integration-client");
14class Push extends base_command_1.default {
15 constructor() {
16 super(...arguments);
17 this.fetchLoginInformation = async () => {
18 const { BEARER_TOKEN, BEARER_EMAIL } = process.env;
19 if (BEARER_TOKEN && BEARER_EMAIL) {
20 return {
21 Username: BEARER_EMAIL,
22 Password: BEARER_TOKEN
23 };
24 }
25 const { data } = await this.devPortalClient.request({
26 query: QUERY
27 });
28 if (data.data) {
29 return { Username: data.data.currentUser.email, Password: data.data.currentUser.infrastructure.password };
30 }
31 throw 'Fetch credentials error';
32 };
33 }
34 async run() {
35 const { Username, Password } = await this.fetchLoginInformation();
36 this.serviceClient = serviceClient_1.default(this.constants.IntegrationServiceUrl);
37 const data = await this.serviceClient.login({ Username, Password });
38 this.debug('auth info : %j', data);
39 this.integrationClient = new integration_client_1.IntegrationClient(this.constants.DeploymentUrl, data.body.authorization.AuthenticationResult.IdToken, this.config.version);
40 helpers_1.ensureFolderExists(this.locator.buildArtifactDir, true);
41 const archivePath = this.locator.buildArtifactResourcePath('integration.zip');
42 const tasks = [
43 {
44 title: 'Generate bundle',
45 task: async (_ctx) => this.archive(archivePath)
46 },
47 {
48 title: 'Transfer bundle',
49 task: async (_ctx) => this.transfer(archivePath)
50 }
51 ];
52 try {
53 await new Listr(tasks).run();
54 this.success(`🐻 Integration successfully pushed.\n`);
55 this.log(
56 // tslint:disable-next-line:prefer-template
57 `Your Integration will be available shortly here: ` +
58 this.colors.bold(`${this.constants.DeveloperPortalUrl}integrations/${this.bearerConfig.integrationUuid}`));
59 this.log(
60 // tslint:disable-next-line:prefer-template
61 `\nIn the meantime you can follow the deployment here: ` +
62 this.colors.bold(`${this.constants.DeveloperPortalUrl}integrations/${this.bearerConfig.integrationUuid}/logs`));
63 }
64 catch (e) {
65 this.error(e);
66 }
67 }
68 async archive(archivePath) {
69 return new Promise(async (resolve, reject) => {
70 const output = fs.createWriteStream(archivePath);
71 const archive = archiver('zip', {
72 zlib: { level: 9 } // Sets the compression level.
73 });
74 // pipe archive data to the file
75 archive.pipe(output);
76 const files = await globby([
77 '.bearer/.keep',
78 'views/**/*',
79 'functions/**/*.ts',
80 'functions/tsconfig.json',
81 'yarn.lock',
82 'package-json.lock',
83 'spec.ts',
84 'package.json',
85 'auth.config.json'
86 ]);
87 this.debug('Files to upload', files.join('\n'));
88 if (files.length >= 100) {
89 return reject(new Error('Too many files to bundle. Please re-run this command this DEBUG=*'));
90 }
91 output.on('close', () => {
92 this.debug(`Archive created: ${archive.pointer() / 1024} Kb / ${archivePath}`);
93 resolve(archivePath);
94 });
95 archive.on('error', (err) => {
96 reject(err);
97 });
98 archive.on('warning', (err) => {
99 if (err.code === 'ENOENT') {
100 reject(err);
101 }
102 else {
103 this.debug(err);
104 }
105 });
106 files.map(file => {
107 archive.file(file, { name: file });
108 });
109 archive.finalize();
110 });
111 }
112 async getSignedUrl() {
113 return this.integrationClient.getIntegrationArchiveUploadUrl(this.bearerConfig.orgId, this.bearerConfig.integrationId);
114 }
115 async transfer(archivePath) {
116 try {
117 const url = await this.getSignedUrl();
118 this.debug(url);
119 const file = fs.readFileSync(archivePath);
120 await axios_1.default.put(url, file, { headers: { 'Content-Type': 'application/zip' } });
121 return true;
122 }
123 catch (e) {
124 if (e.response) {
125 this.debug(e.response);
126 switch (e.response.status) {
127 case 401: {
128 this.error(`Unauthorized to push, please visit ${this.constants.DeveloperPortalUrl}integrations/${this.bearerConfig.integrationUuid} to confirm you have access to ${this.colors.bold(this.bearerConfig.integrationUuid)} integration.`);
129 }
130 default: {
131 this.log(e.response.data);
132 }
133 }
134 }
135 this.error(e);
136 return false;
137 }
138 }
139}
140Push.description = 'deploy Integration to Bearer';
141Push.flags = Object.assign({}, base_command_1.default.flags);
142Push.args = [];
143tslib_1.__decorate([
144 decorators_1.RequireIntegrationFolder(),
145 decorators_1.ensureFreshToken(),
146 decorators_1.RequireLinkedIntegration()
147], Push.prototype, "run", null);
148exports.default = Push;
149const QUERY = `
150query CLIPush {
151 currentUser {
152 email
153 infrastructure {
154 password
155 }
156 }
157}
158`;