liveperson-functions-cli
Version:
LivePerson Functions CLI
122 lines (120 loc) • 5.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DeployView = void 0;
const dynamic_1 = require("set-interval-async/dynamic");
const faasFactory_service_1 = require("../service/faasFactory.service");
const utils_1 = require("../shared/utils");
const printer_1 = require("./printer");
const prompt_1 = require("./printer/prompt");
class DeployView {
constructor({ prompt = new prompt_1.Prompt(), log = new printer_1.LogMessage(), error = new printer_1.ErrorMessage(), tasklist = new printer_1.TaskList({ concurrent: true }), chalk = printer_1.chalk, } = {}) {
this.prompt = prompt;
this.log = log;
this.error = error;
this.tasklist = tasklist;
this.chalk = chalk;
}
/**
* Runs a prompt and asks the user for confirmation about the passed lambdas
* @param {*} lambdas - lambda functions
* @returns {Promise<Answers>} - prompt answers
* @memberof DeployView
*/
async askForConfirmation(lambdas) {
lambdas.forEach((lambda) => {
this.prompt.addQuestion({
name: `${lambda.name}`,
type: 'confirm',
message: this.preparePromptMessage(lambda),
});
});
return this.prompt.run();
}
/**
* Runs a tasklist with all functions to deploy.
* Checks every 3sec if the deployment is finished.
* @param {ILambda[]} confirmedFunctionsToDeploy - Functions to deploy
* @param {boolean} [noWatch] - Changes the renderer of the tasklist, so no rendering is displayed in the console
* @returns
* @memberof DeployView
*/
async showDeployments({ confirmedFunctionsToDeploy, noWatch = false, }) {
if (noWatch) {
this.tasklist = new printer_1.TaskList({ renderer: 'silent', concurrent: true });
}
else {
this.log.print('\nDeploying following functions:\n');
}
confirmedFunctionsToDeploy.forEach((entry) => {
this.tasklist.addTask({
title: `Deploying ${entry.name}`,
// eslint-disable-next-line consistent-return
task: async (_, task) => {
const faasService = await faasFactory_service_1.factory.get();
const response = await faasService.deploy(entry.uuid);
if (response.uuid) {
return task.skip(`${response.message} (${entry.uuid})`);
}
if (!noWatch) {
return new Promise(async (resolve) => {
function checkIfLambdaIsDeployed() {
return new Promise(async (resolve) => {
const lambdaInformation = (await faasService.getLambdaByUUID(entry.uuid));
if (lambdaInformation.state === 'Productive' &&
lambdaInformation.lastDeployment.deployedAt) {
resolve(true);
}
else {
resolve(false);
}
});
}
function watchDeployment() {
let deployed = false;
return new Promise((resolve) => {
const timer = (0, dynamic_1.setIntervalAsync)(async () => {
deployed = await checkIfLambdaIsDeployed();
/* istanbul ignore else */
if (deployed) {
// with the update to v2 the await clearIntervalAsync(timer) requires to be wrapped inside an immediately-invoke function expression or remove the await so that it resolves the timeout it causes because of not finishing the process
(0, dynamic_1.clearIntervalAsync)(timer);
resolve();
}
}, 3000);
});
}
watchDeployment().then(() => resolve());
});
}
},
});
});
return this.tasklist.run();
}
/**
* Shows an error message
* @param {string|PrettyPrintableError} message - message
* @memberof DeployView
*/
showErrorMessage(message) {
this.error.print(message);
}
preparePromptMessage(lambda) {
var _a, _b;
const message = `Do you want to approve and deploy?
AccountId: ${this.chalk.green(lambda.accountId)}
Description: ${lambda.description}
UUID: ${lambda.uuid}
Event: ${lambda.event || 'No Event'}
Last modified by: ${lambda.updatedBy}
Last modified at: ${(0, utils_1.formatDate)(lambda.updatedAt)}
Last deployed at: ${((_a = lambda.lastDeployment) === null || _a === void 0 ? void 0 : _a.createdAt)
? (0, utils_1.formatDate)((_b = lambda.lastDeployment) === null || _b === void 0 ? void 0 : _b.createdAt)
: '-'}
Runtime: ${lambda.runtime.name}
`;
return message;
}
}
exports.DeployView = DeployView;
//# sourceMappingURL=deploy.view.js.map