-
-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(cli): Pull things out for testing and bug fixing
- Loading branch information
Kent C. Dodds
committed
Apr 26, 2016
1 parent
5ba2e0f
commit 2e41bd0
Showing
3 changed files
with
62 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import remove from 'lodash.remove' | ||
import contains from 'lodash.contains' | ||
import shellEscape from 'shell-escape' | ||
import isEmpty from 'lodash.isempty' | ||
|
||
export {getScriptsAndArgs} | ||
|
||
function getScriptsAndArgs(program) { | ||
let scripts, args, parallel | ||
if (!isEmpty(program.parallel)) { | ||
scripts = program.parallel.split(',') | ||
args = getArgs(program.args, program.rawArgs, scripts) | ||
parallel = true | ||
} else { | ||
scripts = program.args[0].split(',') | ||
args = getArgs(program.args.slice(1), program.rawArgs, scripts) | ||
parallel = false | ||
} | ||
return {scripts, args, parallel} | ||
} | ||
|
||
function getArgs(args, rawArgs, scripts) { | ||
const allArgs = rawArgs.slice(2) | ||
const psArgs = ['-p', '--parallel', '-c', '--config'] | ||
const psFlags = ['-s', '--silent'] | ||
const cleanedArgs = remove(allArgs, (item, index, arry) => { | ||
const isArgOrFlag = contains(psArgs, item) || contains(psFlags, item) | ||
const isArgValue = contains(psArgs, arry[index - 1]) | ||
const isScript = contains(scripts, item) | ||
return !isArgOrFlag && !isArgValue && !isScript | ||
}) | ||
return shellEscape(cleanedArgs) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import test from 'ava' | ||
import {getScriptsAndArgs} from './bin-utils' | ||
|
||
test('gets scripts', t => { | ||
const {scripts} = getScriptsAndArgs({ | ||
args: ['boo'], | ||
rawArgs: ['node', 'p-s', 'boo'], | ||
}) | ||
t.deepEqual(scripts, ['boo']) | ||
}) | ||
|
||
test('gets parallel scripts', t => { | ||
const {scripts} = getScriptsAndArgs({ | ||
parallel: 'boo,baz', | ||
rawArgs: ['node', 'p-s', '-p', 'boo,baz'], | ||
}) | ||
t.deepEqual(scripts, ['boo', 'baz']) | ||
}) | ||
|
||
test('passes args to scripts', t => { | ||
const {args, scripts} = getScriptsAndArgs({ | ||
args: ['boo'], | ||
rawArgs: ['node', 'p-s', 'boo', '--watch', '--verbose'], | ||
}) | ||
t.deepEqual(scripts, ['boo']) | ||
t.is(args, '--watch --verbose') | ||
}) |