UNPKG

liveperson-functions-cli

Version:
119 lines (118 loc) 5.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PushView = void 0; const faasFactory_service_1 = require("../service/faasFactory.service"); const printer_1 = require("./printer"); const file_service_1 = require("../service/file.service"); class PushView { constructor( /* istanbul ignore next */ { chalk = printer_1.chalk, log = new printer_1.LogMessage(), error = new printer_1.ErrorMessage(), tasklist = new printer_1.TaskList({ exitOnError: false, concurrent: true }), prompt = new printer_1.Prompt(), fileService = new file_service_1.FileService(), } = {}) { this.log = log; this.chalk = chalk; this.prompt = prompt; this.tasklist = tasklist; this.error = error; this.fileService = fileService; } /** * Shows an error message * @param {string|PrettyPrintableError} message - message * @memberof PushView */ showErrorMessage(message) { this.error.print(message); } /** * Prompts the user to confirm all of the lambdas he wants to push * @param {ILambda[]} lambdas Lambdas the user wants to push * @param {string} [accountId] The account ID to display in the prompt * @returns {Promise<Answers>} * @memberof PushView */ async askForConfirmation(lambdas, accountId) { lambdas.forEach((lambda) => { this.prompt.addQuestion({ name: `${lambda.name}`, type: 'confirm', message: this.preparePromptMessage(lambda, accountId), }); }); return this.prompt.run(); } /** * Creates and runs a Listr Task List. It creates one task for each request body * which triggers the push request for it. Depending on the noWatch param the * pushing is displayed or hidden in the console. * @param {{ pushRequestBodies: ILambda[]; noWatch?: boolean }} { pushRequestBodies, noWatch = false } * @memberof PushView */ async showPushProcess({ pushRequestBodies, noWatch = false, }) { if (noWatch) { this.tasklist = new printer_1.TaskList({ exitOnError: false, concurrent: true, renderer: 'silent', }); } else { this.log.print('\nPushing following functions:\n'); } pushRequestBodies.forEach((entry) => { this.tasklist.addTask({ title: `Pushing ${entry.name}`, task: async (_, task) => { if (!entry.description) { throw new Error('Push Error: Lambda description can not be null. Please add a description in the config.json'); } // tslint:disable-next-line:no-shadowed-variable const faasService = await faasFactory_service_1.factory.get(); const isNewLambda = entry.version === -1; const wasModified = await faasService.push({ method: isNewLambda ? 'POST' : 'PUT', body: entry, ...(!isNewLambda && { uuid: entry.uuid }), }); if (!wasModified) { return task.skip(`Push Skipped: The update contained no changes compared to the server version.`); } return wasModified; }, }); }); await this.tasklist.run(); } preparePromptMessage(pushBody, accountId) { const event = pushBody.eventId || 'No Event'; let eventHint = ''; if (pushBody.version !== -1) { const localConfig = this.fileService.getFunctionConfig(pushBody.name); /* istanbul ignore else */ if (localConfig.event !== event) { eventHint = `${this.chalk.yellow(`The remote and local event are different (${event} | ${localConfig.event}). Events cannot be changed after the creation of a function!`)} `; } } const message = `Do you want to approve and ${pushBody.version === -1 ? this.chalk.green('create') : this.chalk.red('overwrite')} the following lambda? ${pushBody.version === -1 ? '' : this.chalk.red('Caution: This action can NOT be reverted!\n')} ${eventHint} AccountId: ${this.chalk.green(accountId)} Name: ${pushBody.name} Description: ${pushBody.description} Event: ${event} Dependencies: ${pushBody.implementation.dependencies.length > 0 ? JSON.stringify(pushBody.implementation.dependencies) : '-'} Environment variables: ${pushBody.implementation.environmentVariables.length > 0 ? JSON.stringify(pushBody.implementation.environmentVariables) : '-'} `; return message; } } exports.PushView = PushView; //# sourceMappingURL=push.view.js.map