liveperson-functions-cli
Version:
LivePerson Functions CLI
111 lines (109 loc) • 5.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.UndeployView = void 0;
const dynamic_1 = require("set-interval-async/dynamic");
const faasFactory_service_1 = require("../service/faasFactory.service");
const printer_1 = require("./printer");
const prompt_1 = require("./printer/prompt");
class UndeployView {
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 UndeployView
*/
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 undeploy.
* Checks every 3sec if the undeployment is finished.
* @param {ILambda[]} confirmedFunctionsToUndeploy - Functions to undeploy
* @param {boolean} [noWatch] - Changes the renderer of the tasklist, so no rendering is displayed in the console
* @returns
* @memberof UndeployView
*/
async showDeployments({ confirmedFunctionsToUndeploy, noWatch = false, }) {
if (noWatch) {
this.tasklist = new printer_1.TaskList({ renderer: 'silent', concurrent: true });
}
else {
this.log.print('\nUndeploying following functions:\n');
}
confirmedFunctionsToUndeploy.forEach(async (entry) => {
this.tasklist.addTask({
title: `Undeploying ${entry.name}`,
// eslint-disable-next-line consistent-return
task: async (_, task) => {
const faasService = await faasFactory_service_1.factory.get();
const response = await faasService.undeploy(entry.uuid);
if (response.uuid) {
return task.skip(`${response.message} (${entry.uuid})`);
}
if (!noWatch) {
return new Promise(async (resolve) => {
function waitUntilLambdaIsUndeployed() {
return new Promise(async (resolve) => {
const lambdaInformation = await faasService.getLambdaByUUID(entry.uuid);
if (lambdaInformation.state === 'Draft') {
resolve(true);
}
else {
resolve(false);
}
});
}
function watchUndeployment() {
let deployed = false;
return new Promise((resolve) => {
const timer = (0, dynamic_1.setIntervalAsync)(async () => {
deployed = await waitUntilLambdaIsUndeployed();
/* 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);
});
}
watchUndeployment().then(() => resolve());
});
}
},
});
});
return this.tasklist.run();
}
/**
* Shows an error message
* @param {string|PrettyPrintableError} message - message
* @memberof UndeployView
*/
showErrorMessage(message) {
this.error.print(message);
}
preparePromptMessage(lambda) {
const message = `Do you really want to undeploy the function ${this.chalk.yellow(lambda.name)} for Account ${this.chalk.green(lambda.accountId)}?
Once you confirm, the function will be undeployed within a few moments.
${this.chalk.red('Caution')} You cannot undo this action!
`;
return message;
}
}
exports.UndeployView = UndeployView;
//# sourceMappingURL=undeploy.view.js.map