UNPKG

@viewdo/dxp-story-cli

Version:
95 lines (77 loc) 2.25 kB
const inquirer = require('inquirer') /* https://github.com/SBoudrias/Inquirer.js */ const console = require('./console-service') module.exports = class PromptService { constructor() { Object.assign(this, { prompter: inquirer.createPromptModule(), prefix: console.prefix }) } validateEmail(email) { return /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(email) } validateFolder(folder) { return /^\.[a-zA-Z0-9_ \/-]*$/.test(folder) } validateKey(key) { return /^[A-Za-z0-9-_]{1,50}$/.test(key) } validateCode(code) { return /^[0-9]{6}/.test(code) } askIf(question, default_value = false) { return this.prompter([{ type : 'confirm', name : 'answer', message : question, default: default_value, prefix: this.prefix }]) .then(({answer}) => answer) } askFor(message, default_value, validate = () => {}, type = 'input') { return this.prompter([{ type, name : 'value', message, validate, default: default_value, prefix: this.prefix }]) .then(({value}) => value) } askWhich(question, choices, preselect) { return this.prompter({ type : 'list', name : 'option', message : question, choices, default: preselect, prefix: this.prefix }) .then(({option}) => option) } async selectFromKeys(model, configured_keys, action_name, allow_new, all) { if(configured_keys.length == 0) // no keys, ask for one return [await this.askFor(`${model} Key`, null, this.validateKey)] if(configured_keys.length > 1 && !all) { // multi key config let [all_text, new_text] = ['<all>','<new>'] let choices = [all_text, ...configured_keys] if(allow_new) choices.push(new_text) let option = await this.askWhich(`${action_name} `, choices, all_text) // new key if(option === new_text) return [await this.askFor(`${model} Key`, null, this.validateKey)] // selected key else if(option !== all_text) return [option] } return configured_keys } }