Skip to content

Commit 2bb52cd

Browse files
addaleaxbcoe
authored andcommitted
feat: implicitly assume node when the command starts with an option (#350)
Assume that commands starting with `-`, e.g. `--expose-gc script.js`, implicitly are Node.js commands. Fixes: #337
1 parent e7b5d81 commit 2bb52cd

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

lib/process-args.js

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ module.exports = {
1111
hideInstrumenterArgs: function (yargv) {
1212
var argv = process.argv.slice(1)
1313
argv = argv.slice(argv.indexOf(yargv._[0]))
14+
if (argv[0][0] === '-') {
15+
argv.unshift(process.execPath)
16+
}
1417
return argv
1518
},
1619
// don't pass arguments for the bin being

test/fixtures/cli/gc.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
gc();
3+
console.log('I’m still running');

test/src/nyc-bin.js

+21
Original file line numberDiff line numberDiff line change
@@ -453,5 +453,26 @@ describe('the nyc cli', function () {
453453
done()
454454
})
455455
})
456+
457+
it('interprets first args after -- as Node.js execArgv', function (done) {
458+
var args = [bin, '--', '--expose-gc', path.resolve(fixturesCLI, 'gc.js')]
459+
460+
var proc = spawn(process.execPath, args, {
461+
cwd: fixturesCLI,
462+
env: env
463+
})
464+
465+
var stdout = ''
466+
proc.stdout.setEncoding('utf8')
467+
proc.stdout.on('data', function (chunk) {
468+
stdout += chunk
469+
})
470+
471+
proc.on('close', function (code) {
472+
code.should.equal(0)
473+
stdout.should.include('still running')
474+
done()
475+
})
476+
})
456477
})
457478
})

test/src/process-args.js

+15
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,21 @@ describe('process-args', function () {
2222

2323
munged.should.eql(['node', 'test/nyc-test.js'])
2424
})
25+
26+
it('parses extra args directly after -- as Node execArgv', function () {
27+
process.argv = ['/Users/benjamincoe/bin/iojs',
28+
'/Users/benjamincoe/bin/nyc.js',
29+
'--',
30+
'--expose-gc',
31+
'index.js'
32+
]
33+
34+
var yargv = require('yargs/yargs')(process.argv.slice(2)).argv
35+
36+
var munged = processArgs.hideInstrumenterArgs(yargv)
37+
38+
munged.should.eql([process.execPath, '--expose-gc', 'index.js'])
39+
})
2540
})
2641

2742
describe('hideInstrumenteeArgs', function () {

0 commit comments

Comments
 (0)