Skip to content

Commit 432d7f2

Browse files
committed
Merge pull request #100 from FiftyThree/cli-commander
Refactor CLI argument parsing
2 parents 18192c0 + 2ff5cbe commit 432d7f2

6 files changed

Lines changed: 50 additions & 57 deletions

File tree

bin/node-dev

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
#!/usr/bin/env node
2+
23
var dev = require('..')
3-
, args = process.argv.slice(2)
4+
var program = require('commander')
5+
var resolveMain = require('../lib/resolveMain')
6+
7+
8+
program
9+
.version(require('../package.json').version)
10+
.option('--all-deps', 'watch the whole dependency tree')
11+
.option('--no-deps', 'watch only the project’s own files and linked modules' +
12+
' (via `npm link`)')
13+
.option('--dedupe', 'dedupe modules dynamically')
14+
.arguments('<script> [arguments...]')
15+
.action(function (script, scriptArgs) {
16+
var wrapper = resolveMain(__dirname + '/../lib/wrap.js')
417

5-
if (!args.length) {
6-
console.log('Usage: node-dev [options] script [arguments]\n')
7-
process.exit(1)
8-
}
18+
dev(wrapper, script, scriptArgs || [], {
19+
allDeps: program.allDeps || false,
20+
noDeps: program.noDeps || false,
21+
dedupe: program.dedupe || false,
22+
})
23+
})
924

10-
dev(args)
25+
program.parse(process.argv)

lib/cli.js

Lines changed: 0 additions & 40 deletions
This file was deleted.

lib/index.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,26 @@ var fork = require('child_process').fork
22
var path = require('path')
33
var filewatcher = require('filewatcher')
44
var ipc = require('./ipc')
5-
var cli = require('./cli')
5+
var resolveMain = require('./resolveMain')
66

7-
module.exports = function(args) {
87

9-
// The child_process
10-
var child
8+
module.exports = function(wrapper, script, scriptArgs, opts) {
9+
if (typeof wrapper !== 'string' || wrapper.length === 0) {
10+
throw new TypeError('`wrapper` must be a string')
11+
}
12+
13+
if (typeof script !== 'string' || script.length === 0) {
14+
throw new TypeError('`script` must be a string')
15+
}
1116

12-
// Parse command line options
13-
var opts = cli.parseOpts(args)
17+
if (!Array.isArray(scriptArgs)) {
18+
throw new TypeError('`scriptArgs` must be an array')
19+
}
1420

15-
// Inject wrap.js into the args array
16-
var main = cli.injectScript(args, __dirname + '/wrap.js')
21+
// The child_process
22+
var child
1723

24+
var main = resolveMain(script)
1825
var cfg = require('./cfg')(main, opts)
1926
var log = require('./log')(cfg)
2027
var notify = require('./notify')(cfg, log)
@@ -50,7 +57,8 @@ module.exports = function(args) {
5057
* Run the wrapped script.
5158
*/
5259
function start() {
53-
child = fork(args[0], args.slice(1), {
60+
var wrapperArgs = [script].concat(scriptArgs)
61+
child = fork(wrapper, wrapperArgs, {
5462
cwd: process.cwd(),
5563
env: process.env
5664
})

lib/resolveMain.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var resolve = require('resolve').sync
2+
3+
4+
module.exports = function(main) {
5+
return resolve(main, {
6+
basedir: process.cwd(),
7+
paths: [process.cwd()]
8+
})
9+
}

lib/wrap.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ var resolve = require('resolve').sync
55
var hook = require('./hook')
66
var ipc = require('./ipc')
77
var cfg = require('./cfg')
8-
var cli = require('./cli')
8+
var resolveMain = require('./resolveMain')
99

1010
// Remove wrap.js from the argv array
1111
process.argv.splice(1, 1)
1212

1313
// Resolve the location of the main script relative to cwd
14-
var main = cli.resolveMain(process.argv[1])
14+
var main = resolveMain(process.argv[1])
1515

1616
var cfg = require('./cfg')(main)
1717

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"test": "node test"
2828
},
2929
"dependencies": {
30+
"commander": "^2.8.1",
3031
"dateformat": "~1.0.4-1.2.3",
3132
"dynamic-dedupe": "^0.2.0",
3233
"filewatcher": "~1.1.1",

0 commit comments

Comments
 (0)