diff --git a/bin/now-alias.js b/bin/now-alias.js index d08544a..eefa81f 100755 --- a/bin/now-alias.js +++ b/bin/now-alias.js @@ -16,6 +16,7 @@ const toHost = require('../lib/to-host') const {reAlias} = require('../lib/re-alias') const exit = require('../lib/utils/exit') const logo = require('../lib/utils/output/logo') +const promptBool = require('../lib/utils/input/prompt-bool') const argv = minimist(process.argv.slice(2), { string: ['config', 'token', 'rules'], @@ -223,8 +224,8 @@ async function run(token) { } try { - const confirmation = (await readConfirmation(alias, _alias, _aliases)).toLowerCase() - if (confirmation !== 'y' && confirmation !== 'yes') { + const confirmation = await confirmDeploymentRemoval(alias, _alias) + if (!confirmation) { console.log('\n> Aborted') process.exit(0) } @@ -278,27 +279,20 @@ async function run(token) { alias.close() } -async function readConfirmation(alias, _alias) { +async function confirmDeploymentRemoval(alias, _alias) { const deploymentsList = await alias.list() const urls = new Map(deploymentsList.map(l => [l.uid, l.url])) - return new Promise(resolve => { - const time = chalk.gray(ms(new Date() - new Date(_alias.created)) + ' ago') - const _sourceUrl = chalk.underline(`https://${urls.get(_alias.deploymentId)}`) - const tbl = table( - [[_alias.uid, _sourceUrl, chalk.underline(`https://${_alias.alias}`), time]], - {align: ['l', 'r', 'l'], hsep: ' '.repeat(6)} - ) - - process.stdout.write('> The following alias will be removed permanently\n') - process.stdout.write(' ' + tbl + '\n') - process.stdout.write(` ${chalk.bold.red('> Are you sure?')} ${chalk.gray('[y/N] ')}`) - - process.stdin.on('data', d => { - process.stdin.pause() - resolve(d.toString().trim()) - }).resume() - }) + const time = chalk.gray(ms(new Date() - new Date(_alias.created)) + ' ago') + const _sourceUrl = chalk.underline(`https://${urls.get(_alias.deploymentId)}`) + const tbl = table( + [[_alias.uid, _sourceUrl, chalk.underline(`https://${_alias.alias}`), time]], + {align: ['l', 'r', 'l'], hsep: ' '.repeat(6)} + ) + + const msg = '> The following alias will be removed permanently\n' + + ` ${tbl} \nAre you sure?` + return await promptBool(msg) } function findAlias(alias, list) { @@ -341,7 +335,11 @@ async function updatePathAlias(alias, aliasName, rules) { const start = new Date() const res = await alias.updatePathBasedroutes(String(aliasName), rules) const elapsed = ms(new Date() - start) - if (!res.error) { + if (res.error) { + const err = new Error(res.error.message) + err.userError = true + throw err + } else { console.log(`${chalk.cyan('> Success!')} ${res.ruleCount} rules configured for ${chalk.underline(res.alias)} [${elapsed}]`) } } diff --git a/bin/now-billing.js b/bin/now-billing.js index d32280b..0cb9119 100644 --- a/bin/now-billing.js +++ b/bin/now-billing.js @@ -16,7 +16,7 @@ const NowCreditCards = require('../lib/credit-cards') const indent = require('../lib/indent') const listInput = require('../lib/utils/input/list') const success = require('../lib/utils/output/success') -const promptBool = require('../lib/utils/output/prompt-bool') +const promptBool = require('../lib/utils/input/prompt-bool') const logo = require('../lib/utils/output/logo') const argv = minimist(process.argv.slice(2), { @@ -210,7 +210,6 @@ async function run(token) { if (cardId) { const label = `Are you sure that you to set this card as the default?` const confirmation = await promptBool(label) - console.log('') // new line if (!confirmation) { console.log('Aborted') break @@ -263,7 +262,6 @@ async function run(token) { if (cardId) { const label = `Are you sure that you want to remove this card?` const confirmation = await promptBool(label) - console.log('') // new line if (!confirmation) { console.log('Aborted') break diff --git a/lib/alias.js b/lib/alias.js index 490c38f..4dd021f 100644 --- a/lib/alias.js +++ b/lib/alias.js @@ -5,6 +5,7 @@ const minimist = require('minimist') const chalk = require('chalk') // Ours +const promptBool = require('../lib/utils/input/prompt-bool') const exit = require('./utils/exit') const copy = require('./copy') const toHost = require('./to-host') @@ -125,8 +126,7 @@ module.exports = class Alias extends Now { return {uid: body.error.uid} } if (res.status === 422) { - console.log(body.error.message) - return new Error(body.error.message) + return body } // no retry on authorization problems @@ -218,17 +218,12 @@ module.exports = class Alias extends Now { if (aliasDepl && aliasDepl.rules) { if (isTTY) { try { - const confirmation = (await new Promise(resolve => { - process.stdout.write(`Path alias excists with ${aliasDepl.rules.length} rule${aliasDepl.rules.length > 1 ? 's' : ''}.\n`) - process.stdout.write(`Are you sure you want to update ${alias} to be a normal alias?\n`) + const msg = `> Path alias excists with ${aliasDepl.rules.length} rule${aliasDepl.rules.length > 1 ? 's' : ''}.\n` + + `> Are you sure you want to update ${alias} to be a normal alias?\n` - process.stdin.on('data', d => { - process.stdin.pause() - resolve(d.toString().trim()) - }).resume() - })).toLowerCase() + const confirmation = await promptBool(msg) - if (confirmation !== 'y' && confirmation !== 'yes') { + if (!confirmation) { console.log('\n> Aborted') return exit(1) } diff --git a/lib/utils/output/prompt-bool.js b/lib/utils/input/prompt-bool.js similarity index 94% rename from lib/utils/output/prompt-bool.js rename to lib/utils/input/prompt-bool.js index 812109f..486a5e5 100644 --- a/lib/utils/output/prompt-bool.js +++ b/lib/utils/input/prompt-bool.js @@ -7,7 +7,8 @@ module.exports = (label, { yesChar = 'y', noChar = 'n', stdin = process.stdin, - stdout = process.stdout + stdout = process.stdout, + trailing = '\n' } = {}) => { return new Promise((resolve, reject) => { const isRaw = process.stdin.isRaw @@ -16,6 +17,7 @@ module.exports = (label, { stdin.resume() function restore() { + console.log(trailing) stdin.setRawMode(isRaw) stdin.pause() stdin.removeListener('data', onData)