From 037b1d02e33beebf2071fe68c235593cea8e2c42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?TZ=20=7C=20=E5=A4=A9=E7=8C=AA?= Date: Mon, 26 Mar 2018 13:37:34 +0800 Subject: [PATCH] fix: execArgv convert rule (#20) --- LICENSE | 2 +- lib/command.js | 26 +++++++++++--- test/fixtures/my-bin/command/parserDebug.js | 1 + test/fixtures/my-bin/command/parserRequire.js | 35 +++++++++++++++++++ test/my-bin.test.js | 15 +++++++- 5 files changed, 72 insertions(+), 7 deletions(-) create mode 100644 test/fixtures/my-bin/command/parserRequire.js diff --git a/LICENSE b/LICENSE index 065e597..af2d832 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2017 node-modules and other contributors +Copyright (c) 2017-present node-modules and other contributors Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/lib/command.js b/lib/command.js index e382fd7..25c75b3 100644 --- a/lib/command.js +++ b/lib/command.js @@ -309,12 +309,28 @@ class CommonBin { argv[changeCase.camel(key)] = undefined; } - // only exports execArgv when any match - if (Object.keys(execArgvObj).length) { - context.execArgv = this.helper.unparseArgv(execArgvObj); - context.execArgvObj = execArgvObj; - context.debugOptions = debugOptions; + // exports execArgv + const self = this; + context.execArgvObj = execArgvObj; + + // convert execArgvObj to execArgv + // `--require` should be `--require abc --require 123`, not allow `=` + // `--debug` should be `--debug=9999`, only allow `=` + Object.defineProperty(context, 'execArgv', { + get() { + const lazyExecArgvObj = context.execArgvObj; + const execArgv = self.helper.unparseArgv(lazyExecArgvObj, { excludes: [ 'require' ] }); + if (lazyExecArgvObj.require) { + execArgv.push(...self.helper.unparseArgv({ require: lazyExecArgvObj.require }, { useEquals: false })); + } + return execArgv; + }, + }); + + // only exports debugPort when any match + if (Object.keys(debugOptions).length) { context.debugPort = debugPort; + context.debugOptions = debugOptions; } } diff --git a/test/fixtures/my-bin/command/parserDebug.js b/test/fixtures/my-bin/command/parserDebug.js index a4c2501..3488ba0 100644 --- a/test/fixtures/my-bin/command/parserDebug.js +++ b/test/fixtures/my-bin/command/parserDebug.js @@ -22,6 +22,7 @@ class ContextCommand extends Command { * run({ argv, execArgv, debugPort, debugOptions }) { console.log('argv: %j', argv); console.log('execArgv: %s', execArgv); + console.log('execArgv.length: %s', execArgv.length); console.log('debugPort: %s', debugPort); console.log('debugOptions: %j', debugOptions); } diff --git a/test/fixtures/my-bin/command/parserRequire.js b/test/fixtures/my-bin/command/parserRequire.js new file mode 100644 index 0000000..f04115b --- /dev/null +++ b/test/fixtures/my-bin/command/parserRequire.js @@ -0,0 +1,35 @@ +'use strict'; + +const Command = require('../../../..'); + +class ContextCommand extends Command { + constructor(rawArgv) { + super(rawArgv); + this.parserOptions = { + execArgv: true, + removeAlias: true, + }; + + this.options = { + baseDir: { + description: 'target directory', + alias: 'b', + }, + }; + } + + + * run(context) { + context.execArgvObj = { require: 'abc' }; + console.log('execArgv: %j', context.execArgv); + + context.execArgvObj = { require: [ 'abc', '123' ] }; + console.log('execArgv: %j', context.execArgv); + } + + get description() { + return 'custom context'; + } +} + +module.exports = ContextCommand; diff --git a/test/my-bin.test.js b/test/my-bin.test.js index adadad5..950e94b 100644 --- a/test/my-bin.test.js +++ b/test/my-bin.test.js @@ -126,6 +126,19 @@ describe('test/my-bin.test.js', () => { .end(done); }); + it('my-bin parserRequire', done => { + const args = [ + 'parserRequire', + ]; + coffee.fork(myBin, args, { cwd }) + // .debug() + // .coverage(false) + .expect('stdout', /execArgv: \["--require","abc"]/) + .expect('stdout', /execArgv: \["--require","abc","123"]/) + .expect('code', 0) + .end(done); + }); + it('my-bin parserDebug without execArgv', done => { const args = [ 'parserDebug', @@ -136,7 +149,7 @@ describe('test/my-bin.test.js', () => { // .debug() // .coverage(false) .expect('stdout', /"debug-invalid":true,"debugInvalid":true/) - .expect('stdout', /execArgv: undefined/) + .expect('stdout', /execArgv.length: 0/) .expect('stdout', /debugPort: undefined/) .expect('stdout', /debugOptions: undefined/) .expect('code', 0)