Skip to content
Permalink
Browse files

refactor: use common-bin parse execArgv

  • Loading branch information
atian25 authored and fengmk2 committed Sep 5, 2017
1 parent b6592e5 commit 831c77d3d0a269e1ab1243471ef34bb53df0fb80
Showing with 19 additions and 62 deletions.
  1. +11 −58 lib/command.js
  2. +4 −1 test/fixtures/my-egg-bin/lib/cmd/echo.js
  3. +4 −3 test/my-egg-bin.test.js
@@ -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;
@@ -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);
}
}

@@ -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);
});

0 comments on commit 831c77d

Please sign in to comment.
You can’t perform that action at this time.