UNPKG

liveperson-functions-cli

Version:
120 lines 4.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LoginView = void 0; // tslint:disable:object-literal-shorthand const printer_1 = require("./printer"); /** * Adds the condition if a prompt should be displayed or not. * Will be executed during runtime. * @param {*} condition - condition for execution. * @param {boolean} compareWithAnswer - should the condition compared with the answer. * @returns {{ when: (answer: { accountId: string }) => boolean }} */ function checkIfQuestionShouldBeDisplayed({ condition, compareWithAnswer, }) { return { /* istanbul ignore next */ when(answer) { /* istanbul ignore next */ return compareWithAnswer ? answer.accountId === condition : condition; }, }; } class LoginView { constructor({ emoji = printer_1.emoji, log = new printer_1.LogMessage(), chalk = printer_1.chalk, prompt = new printer_1.Prompt(), } = {}) { this.log = log; this.emoji = emoji; this.chalk = chalk; this.prompt = prompt; } /** * Ask the user for selecting or enter an accountId * @param {string[]} accountIds - accountIds * @returns {Promise<IPromptAnswer[]>} - prompt answers * @memberof LoginView */ async chooseOrEnterAccountId(accountIds) { if (accountIds.length > 0) { this.prompt.addQuestion({ name: 'accountId', type: 'list', message: 'Choose accountId or select other', choices: accountIds.concat(['other']), }); this.prompt.addQuestion({ name: 'other', type: 'Input', message: 'AccountId', ...checkIfQuestionShouldBeDisplayed({ condition: 'other', compareWithAnswer: true, }), }); const answer = await this.prompt.run(); if (answer.accountId === 'other') { answer.accountId = answer.other; } return answer; } this.prompt.addQuestion({ name: 'accountId', type: 'Input', message: 'AccountId', }); return this.prompt.run(); } /** * Asks the user for username and password * @param {string} username - username * @param {string} password - password * @param {string} accountId - accountId * @param {boolean} displayAccountId - should the account be displayed in the prompt * @returns {Promise<IPromptAnswer[]>} * @memberof LoginView */ async askForUsernameAndPassword({ username, password, } = {}) { this.askForuserName(!username); this.askForPassword(!password); return this.prompt.run(); } /** * Shows the welcome banner * @param {boolean} showBanner - should show the welcome banner * @returns {void} * @memberof LoginView */ showWelcomeBanner(showBanner) { /* istanbul ignore else */ if (showBanner) { this.log.print('Welcome to'); this.log.print(printer_1.figlet.textSync('LP Functions')); this.log.print(`Use ${this.chalk.green('lpf help')} or ${this.chalk.green('lpf <command> --help')} for further informations.`); } } /** * Show error message during login. * @returns {void} * @memberof LoginView */ errorDuringLogin() { this.log.print(`${this.emoji.red_circle} Looks like something went wrong during the login`); this.log.print(`${this.emoji.red_circle} Check your credentials and try again ${this.chalk.green('lpf login')}`); } askForuserName(condition) { this.prompt.addQuestion({ name: 'username', type: 'input', message: 'username', ...checkIfQuestionShouldBeDisplayed({ condition }), }); } askForPassword(condition) { this.prompt.addQuestion({ name: 'password', type: 'password', message: 'password', ...checkIfQuestionShouldBeDisplayed({ condition }), }); } } exports.LoginView = LoginView; //# sourceMappingURL=login.view.js.map