Browse Source

pass flags to subcommands (#98)

* rework args parsing

* test args parsing
master
Cody Zuschlag 8 years ago
committed by Leo Lamprecht
parent
commit
50db54fd83
  1. 30
      bin/now.js
  2. 1
      package.json
  3. 88
      test/args-parsing.js

30
bin/now.js

@ -29,6 +29,7 @@ const defaultCommand = 'deploy'
const commands = new Set([
defaultCommand,
'help',
'list',
'ls',
'rm',
@ -55,25 +56,26 @@ const aliases = new Map([
['secret', 'secrets']
])
let cmd = argv._[0]
let args = []
let cmd = defaultCommand
const args = process.argv.slice(2)
const index = args.findIndex(a => commands.has(a))
if (cmd === 'help') {
cmd = argv._[1]
if (index > -1) {
cmd = args[index]
args.splice(index, 1)
if (!commands.has(cmd)) {
cmd = defaultCommand
}
if (cmd === 'help') {
if (index < args.length && commands.has(args[index])) {
cmd = args[index]
args.splice(index, 1)
} else {
cmd = defaultCommand
}
args.push('--help')
}
args.unshift('--help')
}
if (commands.has(cmd)) {
cmd = aliases.get(cmd) || cmd
args = args.concat(process.argv.slice(3))
} else {
cmd = defaultCommand
args = args.concat(process.argv.slice(2))
}
let bin = resolve(__dirname, 'now-' + cmd)

1
package.json

@ -11,6 +11,7 @@
"scripts": {
"start": "gulp",
"test": "xo && ava",
"pretest": "gulp compile",
"prepublish": "gulp compile",
"pkg": "pkg . --out-dir out"
},

88
test/args-parsing.js

@ -0,0 +1,88 @@
import path from 'path'
import test from 'ava'
import {spawn} from 'cross-spawn'
const deployHelpMessage = '𝚫 now [options] <command | path>'
const aliasHelpMessage = '𝚫 now alias <ls | set | rm> <deployment> <alias>'
test('"now help" prints deploy help message', async t => {
const result = await now('help')
t.is(result.code, 0)
const stdout = result.stdout.split('\n')
t.true(stdout.length > 1)
t.true(stdout[1].includes(deployHelpMessage))
})
test('"now --help" prints deploy help message', async t => {
const result = await now('--help')
t.is(result.code, 0)
const stdout = result.stdout.split('\n')
t.true(stdout.length > 1)
t.true(stdout[1].includes(deployHelpMessage))
})
test('"now deploy --help" prints deploy help message', async t => {
const result = await now('deploy', '--help')
t.is(result.code, 0)
const stdout = result.stdout.split('\n')
t.true(stdout.length > 1)
t.true(stdout[1].includes(deployHelpMessage))
})
test('"now --help deploy" prints deploy help message', async t => {
const result = await now('--help', 'deploy')
t.is(result.code, 0)
const stdout = result.stdout.split('\n')
t.true(stdout.length > 1)
t.true(stdout[1].includes(deployHelpMessage))
})
test('"now help alias" prints alias help message', async t => {
const result = await now('help', 'alias')
t.is(result.code, 0)
const stdout = result.stdout.split('\n')
t.true(stdout.length > 1)
t.true(stdout[1].includes(aliasHelpMessage))
})
test('"now alias --help" is the same as "now --help alias"', async t => {
const [result1, result2] = await Promise.all([now('alias', '--help'), now('--help', 'alias')])
t.is(result1.code, 0)
t.is(result1.code, result2.code)
t.is(result1.stdout, result2.stdout)
})
/**
* Run the built now binary with given arguments
*
* @param {String} args string arguements
* @return {Promise} promise that resolves to an object {code, stdout}
*/
function now(...args) {
return new Promise((resolve, reject) => {
const command = path.resolve(__dirname, '../build/bin/now')
const now = spawn(command, args)
let stdout = ''
now.stdout.on('data', data => {
stdout += data
})
now.on('error', err => {
reject(err)
})
now.on('close', code => {
resolve({
code,
stdout
})
})
})
}
Loading…
Cancel
Save