UNPKG

@adinsure-ops/ops-cli

Version:

Operations CLI for working with AdInsure

87 lines (86 loc) 3.36 kB
import { __awaiter } from "tslib"; import { Flags, ux } from '@oclif/core'; import axios from 'axios'; import chalk from 'chalk'; import fs from 'fs-extra'; import os from 'os'; import path from 'path'; import CommandBase from '../../command.base.js'; class DownloadPlatform extends CommandBase { constructor() { super(...arguments); this.url = 'https://ops.adinsure.com/api/storage/binaries/'; } /** * Get unique AdInsure Platform zip by specified version * @param {string} version The version of AdInsure Platform * @returns {Promise<AxiosResponse<any, any>>} */ // eslint-disable-next-line @typescript-eslint/no-explicit-any downloadFile(version) { return __awaiter(this, void 0, void 0, function* () { return axios.get(`${this.url}/AdInsure-${version}.zip`, { headers: { Authorization: 'Bearer ' + (yield this.security.getToken()), 'Content-Type': 'application/octet-stream', }, responseType: 'arraybuffer', }); }); } run() { const _super = Object.create(null, { run: { get: () => super.run } }); return __awaiter(this, void 0, void 0, function* () { _super.run.call(this); const { flags } = yield this.parse(DownloadPlatform); const basePath = flags.output.charAt(0) === '~' ? os.homedir() + flags.output.split('~')[1] : flags.output; const filename = `AdInsure-${flags.version}.zip`; const filePath = path.resolve(basePath, filename); ux.action.start(`Downloading AdInsure Platform version ${flags.version} to ${filePath}`, undefined, { stdout: true }); try { const file = yield this.downloadFile(flags.version); const writeStream = fs.createWriteStream(filePath); writeStream.write(file.data, 'binary'); writeStream.on('finish', () => { ux.action.stop(`${chalk.green('OK')}`); }); writeStream.end(); // eslint-disable-next-line @typescript-eslint/no-explicit-any } catch (error) { switch (error.statusCode) { case 404: { this.error('Does not exists.', { exit: 1 }); } case 400: { this.error('Try refreshing your login credentials with "ops login".', { exit: 1 }); } default: { this.error(error.message); } } } }); } } DownloadPlatform.aliases = ['download']; DownloadPlatform.description = 'download AdInsure Platform bundle containing zip of Client, Server and IdentityServer from azure storage'; DownloadPlatform.usage = 'download <flags>'; DownloadPlatform.examples = [ `$ ops download -v 6.0.0 -o ~/Downloads`, ]; DownloadPlatform.flags = { version: Flags.string({ char: 'v', description: 'version we want to download', required: true, }), output: Flags.string({ char: 'o', description: 'output directory', default: './', }), }; export default DownloadPlatform;