Browse Source

Improve path alias ux (#376)

* Improved UX for showing errors on path based aliases

*   Refactor our read confirmation

* Cleanup now-alias to use read-confirmation

* `read-bool` is an input util, not an output one

* Use `prompt-bool` instead of `read-confimation`

* Make `prompt-bool` print a new line before resolving/rejecting
master
Jarmo Isotalo 8 years ago
committed by Matheus Fernandes
parent
commit
a3bb086e5a
  1. 40
      bin/now-alias.js
  2. 4
      bin/now-billing.js
  3. 17
      lib/alias.js
  4. 4
      lib/utils/input/prompt-bool.js

40
bin/now-alias.js

@ -16,6 +16,7 @@ const toHost = require('../lib/to-host')
const {reAlias} = require('../lib/re-alias') const {reAlias} = require('../lib/re-alias')
const exit = require('../lib/utils/exit') const exit = require('../lib/utils/exit')
const logo = require('../lib/utils/output/logo') const logo = require('../lib/utils/output/logo')
const promptBool = require('../lib/utils/input/prompt-bool')
const argv = minimist(process.argv.slice(2), { const argv = minimist(process.argv.slice(2), {
string: ['config', 'token', 'rules'], string: ['config', 'token', 'rules'],
@ -223,8 +224,8 @@ async function run(token) {
} }
try { try {
const confirmation = (await readConfirmation(alias, _alias, _aliases)).toLowerCase() const confirmation = await confirmDeploymentRemoval(alias, _alias)
if (confirmation !== 'y' && confirmation !== 'yes') { if (!confirmation) {
console.log('\n> Aborted') console.log('\n> Aborted')
process.exit(0) process.exit(0)
} }
@ -278,27 +279,20 @@ async function run(token) {
alias.close() alias.close()
} }
async function readConfirmation(alias, _alias) { async function confirmDeploymentRemoval(alias, _alias) {
const deploymentsList = await alias.list() const deploymentsList = await alias.list()
const urls = new Map(deploymentsList.map(l => [l.uid, l.url])) 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 time = chalk.gray(ms(new Date() - new Date(_alias.created)) + ' ago') const _sourceUrl = chalk.underline(`https://${urls.get(_alias.deploymentId)}`)
const _sourceUrl = chalk.underline(`https://${urls.get(_alias.deploymentId)}`) const tbl = table(
const tbl = table( [[_alias.uid, _sourceUrl, chalk.underline(`https://${_alias.alias}`), time]],
[[_alias.uid, _sourceUrl, chalk.underline(`https://${_alias.alias}`), time]], {align: ['l', 'r', 'l'], hsep: ' '.repeat(6)}
{align: ['l', 'r', 'l'], hsep: ' '.repeat(6)} )
)
const msg = '> The following alias will be removed permanently\n' +
process.stdout.write('> The following alias will be removed permanently\n') ` ${tbl} \nAre you sure?`
process.stdout.write(' ' + tbl + '\n') return await promptBool(msg)
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()
})
} }
function findAlias(alias, list) { function findAlias(alias, list) {
@ -341,7 +335,11 @@ async function updatePathAlias(alias, aliasName, rules) {
const start = new Date() const start = new Date()
const res = await alias.updatePathBasedroutes(String(aliasName), rules) const res = await alias.updatePathBasedroutes(String(aliasName), rules)
const elapsed = ms(new Date() - start) 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}]`) console.log(`${chalk.cyan('> Success!')} ${res.ruleCount} rules configured for ${chalk.underline(res.alias)} [${elapsed}]`)
} }
} }

4
bin/now-billing.js

@ -16,7 +16,7 @@ const NowCreditCards = require('../lib/credit-cards')
const indent = require('../lib/indent') const indent = require('../lib/indent')
const listInput = require('../lib/utils/input/list') const listInput = require('../lib/utils/input/list')
const success = require('../lib/utils/output/success') 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 logo = require('../lib/utils/output/logo')
const argv = minimist(process.argv.slice(2), { const argv = minimist(process.argv.slice(2), {
@ -210,7 +210,6 @@ async function run(token) {
if (cardId) { if (cardId) {
const label = `Are you sure that you to set this card as the default?` const label = `Are you sure that you to set this card as the default?`
const confirmation = await promptBool(label) const confirmation = await promptBool(label)
console.log('') // new line
if (!confirmation) { if (!confirmation) {
console.log('Aborted') console.log('Aborted')
break break
@ -263,7 +262,6 @@ async function run(token) {
if (cardId) { if (cardId) {
const label = `Are you sure that you want to remove this card?` const label = `Are you sure that you want to remove this card?`
const confirmation = await promptBool(label) const confirmation = await promptBool(label)
console.log('') // new line
if (!confirmation) { if (!confirmation) {
console.log('Aborted') console.log('Aborted')
break break

17
lib/alias.js

@ -5,6 +5,7 @@ const minimist = require('minimist')
const chalk = require('chalk') const chalk = require('chalk')
// Ours // Ours
const promptBool = require('../lib/utils/input/prompt-bool')
const exit = require('./utils/exit') const exit = require('./utils/exit')
const copy = require('./copy') const copy = require('./copy')
const toHost = require('./to-host') const toHost = require('./to-host')
@ -125,8 +126,7 @@ module.exports = class Alias extends Now {
return {uid: body.error.uid} return {uid: body.error.uid}
} }
if (res.status === 422) { if (res.status === 422) {
console.log(body.error.message) return body
return new Error(body.error.message)
} }
// no retry on authorization problems // no retry on authorization problems
@ -218,17 +218,12 @@ module.exports = class Alias extends Now {
if (aliasDepl && aliasDepl.rules) { if (aliasDepl && aliasDepl.rules) {
if (isTTY) { if (isTTY) {
try { try {
const confirmation = (await new Promise(resolve => { const msg = `> Path alias excists with ${aliasDepl.rules.length} rule${aliasDepl.rules.length > 1 ? 's' : ''}.\n` +
process.stdout.write(`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.stdout.write(`Are you sure you want to update ${alias} to be a normal alias?\n`)
process.stdin.on('data', d => { const confirmation = await promptBool(msg)
process.stdin.pause()
resolve(d.toString().trim())
}).resume()
})).toLowerCase()
if (confirmation !== 'y' && confirmation !== 'yes') { if (!confirmation) {
console.log('\n> Aborted') console.log('\n> Aborted')
return exit(1) return exit(1)
} }

4
lib/utils/output/prompt-bool.js → lib/utils/input/prompt-bool.js

@ -7,7 +7,8 @@ module.exports = (label, {
yesChar = 'y', yesChar = 'y',
noChar = 'n', noChar = 'n',
stdin = process.stdin, stdin = process.stdin,
stdout = process.stdout stdout = process.stdout,
trailing = '\n'
} = {}) => { } = {}) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const isRaw = process.stdin.isRaw const isRaw = process.stdin.isRaw
@ -16,6 +17,7 @@ module.exports = (label, {
stdin.resume() stdin.resume()
function restore() { function restore() {
console.log(trailing)
stdin.setRawMode(isRaw) stdin.setRawMode(isRaw)
stdin.pause() stdin.pause()
stdin.removeListener('data', onData) stdin.removeListener('data', onData)
Loading…
Cancel
Save