Browse Source

make `promptBool()` return false upon Ctrl+C (#557)

* move `prompt-options.js` to utils/input dir

* make `promptBool()` return false upon Ctrl+C

Also make the `info('Aborted')` output more consistent
across the CLI.

* Revert "handle user abort in `now domains buy` (#554)"

This reverts commit 34151c8193.
master
Nathan Rajlich 8 years ago
committed by Matheus Fernandes
parent
commit
23abd2a826
  1. 5
      bin/now-alias.js
  2. 3
      bin/now-billing.js
  3. 17
      bin/now-deploy.js
  4. 22
      bin/now-domains.js
  5. 2
      lib/alias.js
  6. 20
      lib/utils/input/prompt-bool.js
  7. 0
      lib/utils/input/prompt-options.js

5
bin/now-alias.js

@ -19,6 +19,7 @@ const { error } = require('../lib/error')
const toHost = require('../lib/to-host') 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 info = require('../lib/utils/output/info')
const logo = require('../lib/utils/output/logo') const logo = require('../lib/utils/output/logo')
const promptBool = require('../lib/utils/input/prompt-bool') const promptBool = require('../lib/utils/input/prompt-bool')
@ -304,8 +305,8 @@ async function run({ token, config: { currentTeam, user } }) {
try { try {
const confirmation = await confirmDeploymentRemoval(alias, _alias) const confirmation = await confirmDeploymentRemoval(alias, _alias)
if (!confirmation) { if (!confirmation) {
console.log('\n> Aborted') info('Aborted')
process.exit(0) return process.exit(0)
} }
const start = new Date() const start = new Date()

3
bin/now-billing.js

@ -17,6 +17,7 @@ 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/input/prompt-bool') const promptBool = require('../lib/utils/input/prompt-bool')
const info = require('../lib/utils/output/info')
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), {
@ -236,7 +237,7 @@ async function run({ token, config: { currentTeam, user } }) {
trailing: '\n' trailing: '\n'
}) })
if (!confirmation) { if (!confirmation) {
console.log('Aborted') info('Aborted')
break break
} }
const start = new Date() const start = new Date()

17
bin/now-deploy.js

@ -22,7 +22,6 @@ const { version } = require('../lib/pkg')
const Logger = require('../lib/build-logger') const Logger = require('../lib/build-logger')
const Now = require('../lib') const Now = require('../lib')
const toHumanPath = require('../lib/utils/to-human-path') const toHumanPath = require('../lib/utils/to-human-path')
const promptOptions = require('../lib/utils/prompt-options')
const { handleError, error } = require('../lib/error') const { handleError, error } = require('../lib/error')
const { fromGit, isRepoPath, gitPathParts } = require('../lib/git') const { fromGit, isRepoPath, gitPathParts } = require('../lib/git')
const readMetaData = require('../lib/read-metadata') const readMetaData = require('../lib/read-metadata')
@ -35,6 +34,7 @@ const info = require('../lib/utils/output/info')
const wait = require('../lib/utils/output/wait') const wait = require('../lib/utils/output/wait')
const NowPlans = require('../lib/plans') const NowPlans = require('../lib/plans')
const promptBool = require('../lib/utils/input/prompt-bool') const promptBool = require('../lib/utils/input/prompt-bool')
const promptOptions = require('../lib/utils/input/prompt-options')
const note = require('../lib/utils/output/note') const note = require('../lib/utils/output/note')
const argv = minimist(process.argv.slice(2), { const argv = minimist(process.argv.slice(2), {
@ -550,17 +550,10 @@ async function sync({ token, config: { currentTeam, user } }) {
`${chalk.bold((currentTeam && `${currentTeam.slug} is`) || `You (${user.username || user.email}) are`)} on the OSS plan. Your code and logs will be made ${chalk.bold('public')}.` `${chalk.bold((currentTeam && `${currentTeam.slug} is`) || `You (${user.username || user.email}) are`)} on the OSS plan. Your code and logs will be made ${chalk.bold('public')}.`
) )
let proceed const proceed = await promptBool(
try { 'Are you sure you want to proceed with the deployment?',
const label = 'Are you sure you want to proceed with the deployment?' { trailing: eraseLines(1) }
proceed = await promptBool(label, { trailing: eraseLines(1) }) )
} catch (err) {
if (err.message === 'USER_ABORT') {
proceed = false
} else {
throw err
}
}
if (proceed) { if (proceed) {
note(`You can use ${cmd('now --public')} to skip this prompt`) note(`You can use ${cmd('now --public')} to skip this prompt`)

22
bin/now-domains.js

@ -272,22 +272,12 @@ async function run({ token, config: { currentTeam, user } }) {
break break
} }
case 'buy': { case 'buy': {
try { await require(resolve(__dirname, 'domains', 'buy.js'))({
await require(resolve(__dirname, 'domains', 'buy.js'))({ domains: domain,
domains: domain, args,
args, currentTeam,
currentTeam, user
user })
})
} catch (err) {
if (err.message === 'USER_ABORT') {
// Move the prompt to a newline before exiting
console.log()
process.exit(1)
} else {
throw err
}
}
break break
} }
default: default:

2
lib/alias.js

@ -255,7 +255,7 @@ module.exports = class Alias extends Now {
}) })
if (!confirmation) { if (!confirmation) {
console.log('\n> Aborted') info('Aborted')
return exit(1) return exit(1)
} }
} catch (err) { } catch (err) {

20
lib/utils/input/prompt-bool.js

@ -13,8 +13,8 @@ module.exports = (
trailing = '' trailing = ''
} = {} } = {}
) => { ) => {
return new Promise((resolve, reject) => { return new Promise(resolve => {
const isRaw = process.stdin.isRaw const isRaw = stdin.isRaw
stdin.setRawMode(true) stdin.setRawMode(true)
stdin.resume() stdin.resume()
@ -29,20 +29,18 @@ module.exports = (
function onData(buffer) { function onData(buffer) {
const data = buffer.toString() const data = buffer.toString()
if (abortSequences.has(data)) { if (data[0].toLowerCase() === yesChar) {
restore()
return reject(new Error('USER_ABORT'))
}
if (resolveChars.has(data[0])) {
restore()
resolve(defaultValue)
} else if (data[0].toLowerCase() === yesChar) {
restore() restore()
resolve(true) resolve(true)
} else if (data[0].toLowerCase() === noChar) { } else if (data[0].toLowerCase() === noChar) {
restore() restore()
resolve(false) resolve(false)
} else if (abortSequences.has(data)) {
restore()
resolve(false)
} else if (resolveChars.has(data[0])) {
restore()
resolve(defaultValue)
} else { } else {
// ignore extraneous input // ignore extraneous input
} }

0
lib/utils/prompt-options.js → lib/utils/input/prompt-options.js

Loading…
Cancel
Save