UNPKG

@twilio/plugin-microvisor

Version:

Interact with your Twilio Microvisor devices

119 lines (118 loc) 5.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TwilioClientCommand = void 0; const tslib_1 = require("tslib"); const cliCore = require('@twilio/cli-core'); const { TwilioClientCommand } = cliCore.baseCommands; exports.TwilioClientCommand = TwilioClientCommand; const { TwilioCliError } = cliCore.services.error; const form_data_1 = tslib_1.__importDefault(require("form-data")); const fs = require('fs'); const axios = require('axios'); const adm_zip_1 = tslib_1.__importDefault(require("adm-zip")); const axios_1 = require("axios"); const core_1 = require("@oclif/core"); const NETWORK_ERROR_CODES = new Set(['ETIMEDOUT', 'ESOCKETTIMEDOUT', 'ECONNABORTED', 'ENOTFOUND']); function insertManifest(zip, manifest) { zip.deleteFile('manifest'); zip.addFile('manifest', manifest); } class CreateApp extends TwilioClientCommand { // Check configured tunnel domain ends in twilio.com. If it does, perform the same mangling as the http // client. If not, leave things alone (this is probably a Twilion trying to hit a test endpoint) mangleUrl(url) { if (/[^/]+\.twilio\.com/.test(url.hostname)) { url.hostname = this.twilioClient.getHostname(url.hostname, this.twilioClient.edge, this.twilioClient.region); } } async run() { await super.run(); const data = new form_data_1.default(); data.append('File', fs.createReadStream(this.args.File)); if (this.args.UniqueName) { data.append('UniqueName', this.args.UniqueName); } const baseUploadUrlString = this.flags['microvisor-upload-url']; let baseUploadUrl = new URL(baseUploadUrlString); this.mangleUrl(baseUploadUrl); const uploadUrl = new URL('v1/Apps', baseUploadUrl); const config = { method: 'post', url: uploadUrl.toString(), auth: { username: this.currentProfile.apiKey, password: this.currentProfile.apiSecret }, headers: Object.assign({}, data.getHeaders()), data: data }; try { let response = await axios(config); this.output(response.data, this.flags.properties); if (typeof this.flags['bundle-out'] !== 'undefined') { this.logger.info(`Saving signed bundle at ${this.flags['bundle-out']}`); const baseRequestUrlString = this.flags['microvisor-request-url']; let baseRequestUrl = new URL(baseRequestUrlString); this.mangleUrl(baseRequestUrl); const requestUrl = new URL('v1/Apps', baseRequestUrl); const manifestConfig = { method: 'get', url: requestUrl.toString() + '/' + response.data.sid + '/Manifest', auth: { username: this.currentProfile.apiKey, password: this.currentProfile.apiSecret }, }; let manifestResponse = await axios(manifestConfig); let zip = new adm_zip_1.default(this.args.File); insertManifest(zip, Buffer.from(manifestResponse.data.encoded_bytes, 'base64')); zip.writeZip(this.flags['bundle-out']); } } catch (error) { if (error instanceof axios_1.AxiosError) { if (error.response && (error.response.status < 200 || error.response.status >= 400)) { const { message, code } = this.httpClient.formatErrorMessage(error.response.data); throw new TwilioCliError(message, code, error.response.data); } else if (error.code && NETWORK_ERROR_CODES.has(error.code)) { throw new TwilioCliError('twilio-cli encountered a network connectivity error. ' + 'Please check your network connection and try your command again. ' + 'Check on Twilio service status at https://status.twilio.com/'); } } throw error; } } } exports.default = CreateApp; CreateApp.description = 'Create an App on the account'; CreateApp.args = [ { name: 'File', required: true, description: 'The App Bundle file' }, { name: 'UniqueName', required: false, description: 'Unique name for the App' } ]; CreateApp.flags = Object.assign({ properties: core_1.Flags.string({ default: 'sid, unique_name, hash', description: 'The app properties you would like to display (JSON output always shows all properties).' }), 'microvisor-upload-url': core_1.Flags.string({ description: 'First part of the url for microvisor api uploads', default: 'https://microvisor-upload.twilio.com/', hidden: true }), 'microvisor-request-url': core_1.Flags.string({ description: 'First part of the url of general microvisor api', default: 'https://microvisor.twilio.com/', hidden: true }), 'bundle-out': core_1.Flags.string({ char: 'b', required: false, description: 'Path to store the signed bundle to' }) }, TwilioClientCommand.flags); module.exports = CreateApp;