Skip to content

Commit

Permalink
refactor: use common-bin parse execArgv
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 authored and fengmk2 committed Sep 7, 2017
1 parent b6592e5 commit 831c77d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 62 deletions.
69 changes: 11 additions & 58 deletions lib/command.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,27 @@
'use strict';

const BaseCommand = require('common-bin');
const changeCase = require('change-case');
const parser = require('yargs-parser');

class Command extends BaseCommand {
/**
* normalize context
* @param {Object} context - { cwd, argv, rawArgv }
* @return {Object} return with { cwd, argv, execArgv, rawArgv }
*/
constructor(rawArgv) {
super(rawArgv);
this.parserOptions = {
execArgv: true,
removeAlias: true,
};
}

get context() {
const context = super.context;
const argv = context.argv;

let execArgvObj = {};
let debugPort;

// extract from command argv
debugPort = findDebugPort(argv);
execArgvObj = extractExecArgv(argv);

// extract from WebStorm env `$NODE_DEBUG_OPTION`
if (context.env.NODE_DEBUG_OPTION) {
console.log('Use $NODE_DEBUG_OPTION: %s', context.env.NODE_DEBUG_OPTION);
const argvFromEnv = parser(context.env.NODE_DEBUG_OPTION);
debugPort = findDebugPort(argvFromEnv);
Object.assign(execArgvObj, extractExecArgv(argvFromEnv));
}

context.execArgv = this.helper.unparseArgv(execArgvObj);
context.execArgvObj = execArgvObj;
context.debug = debugPort;
// compatible
if (context.debugPort) context.debug = context.debugPort;

// remove unuse args
argv.$0 = undefined;
context.argv.$0 = undefined;

return context;
}
}

function match(key, arr) {
return arr.some(x => x instanceof RegExp ? x.test(key) : x === key); // eslint-disable-line no-confusing-arrow
}

function findDebugPort(argv) {
let debugPort;

for (const key of Object.keys(argv)) {
if (match(key, [ /^debug.*/, /^inspect.*/ ]) && typeof argv[key] === 'number') {
debugPort = argv[key];
}
}
return debugPort;
}

// pick and remove all execArgv from origin `argv`
function extractExecArgv(argv) {
const execArgvObj = {};
for (const key of Object.keys(argv)) {
// debug / debug-brk / debug-port / inspect / inspect-brk / inspect-port
if (match(key, [ /^debug.*/, /^inspect.*/, 'es_staging', 'expose_debug_as', /^harmony.*/ ])) {
execArgvObj[key] = argv[key];
// remove from origin obj
argv[key] = undefined;
// also remove `debugBrk`
argv[changeCase.camelCase(key)] = undefined;
}
}
return execArgvObj;
}

module.exports = Command;
5 changes: 4 additions & 1 deletion test/fixtures/my-egg-bin/lib/cmd/echo.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ class EchoCommand extends Command {
}

* run(context) {
console.log('%j', context);
console.log('argv: %j', context.argv);
console.log('debugPort: %s', context.debugPort);
console.log('debugOptions: %j', context.debugOptions);
console.log('execArgv: %j', context.execArgv);
}
}

Expand Down
7 changes: 4 additions & 3 deletions test/my-egg-bin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ describe('test/my-egg-bin.test.js', () => {
coffee.fork(eggBin, args, { cwd })
// .debug()
.expect('stdout', /"baseDir":".\/dist"/)
.expect('stdout', /"debug":6666/)
.notExpect('stdout', /"debugBrk":true/)
.expect('stdout', /"execArgv":\["--debug","--debug-brk=5555","--expose_debug_as=v8debug","--inspect=6666","--inspect-brk","--es_staging","--harmony","--harmony_default_parameters"]/)
.expect('stdout', /debugPort: 6666/)
.notExpect('stdout', /"argv: {.*debugBrk":true/)
.expect('debugOptions:', /{"debug":true,"debug-brk":5555,"inspect":6666,"inspect-brk":true}/)
.expect('stdout', /execArgv: \["--debug","--debug-brk=5555","--expose_debug_as=v8debug","--inspect=6666","--inspect-brk","--es_staging","--harmony","--harmony_default_parameters"]/)
.expect('code', 0)
.end(done);
});
Expand Down

0 comments on commit 831c77d

Please sign in to comment.