Skip to content

Commit

Permalink
feat: extract debug options (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 authored and fengmk2 committed Sep 5, 2017
1 parent ab6de13 commit a90e63c
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 39 deletions.
50 changes: 21 additions & 29 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const debug = require('debug')('common-bin');
const co = require('co');
const yargs = require('yargs');
const parser = require('yargs-parser');
const helper = require('./helper');
const assert = require('assert');
const fs = require('fs');
Expand Down Expand Up @@ -238,7 +239,7 @@ class CommonBin {
completions.forEach(x => console.log(x));
});
} else {
// handle by self
// handle by self
yield this.helper.callFn(this.run, [ context ], this);
}
}
Expand Down Expand Up @@ -283,34 +284,31 @@ class CommonBin {

// extract execArgv
if (this.parserOptions.execArgv) {
const execArgvObj = {};
for (const key of Object.keys(argv)) {
const value = argv[key];

// skip undefined set uppon (camel etc.)
if (value === undefined) continue;

// debug / debug-brk / debug-port / inspect / inspect-brk / inspect-port
if (match(key, [ /^debug.*/, /^inspect.*/ ])) {
// extract debug port
if (typeof value === 'number') context.debugPort = value;
execArgvObj[key] = value;
argv[key] = undefined;
continue;
}
// extract from command argv
let { debugPort, debugOptions, execArgvObj } = this.helper.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);
const obj = this.helper.extractExecArgv(argvFromEnv);
debugPort = obj.debugPort || debugPort;
Object.assign(debugOptions, obj.debugOptions);
Object.assign(execArgvObj, obj.execArgvObj);
}

// others execArgv
if (match(key, [ 'es_staging', 'expose_debug_as', /^harmony.*/ ])) {
execArgvObj[key] = value;
argv[key] = undefined;
continue;
}
// remove from origin argv
for (const key of Object.keys(execArgvObj)) {
argv[key] = undefined;
argv[changeCase.camel(key)] = undefined;
}

// only exports execArgv when any match
if (Object.keys(execArgvObj).length) {
context.execArgvObj = execArgvObj;
context.execArgv = this.helper.unparseArgv(execArgvObj);
context.execArgvObj = execArgvObj;
context.debugOptions = debugOptions;
context.debugPort = debugPort;
}
}

Expand All @@ -328,10 +326,4 @@ class CommonBin {
}
}

function match(key, arr) {
return arr.some(x => {
return x instanceof RegExp ? x.test(key) : x === key;
});
}

module.exports = CommonBin;
31 changes: 31 additions & 0 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,34 @@ exports.unparseArgv = (argv, options = {}) => {
// yargs will paser `debug-brk` to `debug-brk` and `debugBrk`, so we need to filter
return [ ...new Set(unparse(argv, options)) ];
};

/**
* extract execArgv from argv
* @method helper#extractExecArgv
* @param {Object} argv - yargs style
* @return {Object} { debugPort, debugOptions: {}, execArgvObj: {} }
*/
exports.extractExecArgv = argv => {
const debugOptions = {};
const execArgvObj = {};
let debugPort;

for (const key of Object.keys(argv)) {
const value = argv[key];
// skip undefined set uppon (camel etc.)
if (value === undefined) continue;
// debug / debug-brk / debug-port / inspect / inspect-brk / inspect-port
if ([ 'debug', 'debug-brk', 'debug-port', 'inspect', 'inspect-brk', 'inspect-port' ].includes(key)) {
if (typeof value === 'number') debugPort = value;
debugOptions[key] = argv[key];
execArgvObj[key] = argv[key];
} else if (match(key, [ 'es_staging', 'expose_debug_as', /^harmony.*/ ])) {
execArgvObj[key] = argv[key];
}
}
return { debugPort, debugOptions, execArgvObj };
};

function match(key, arr) {
return arr.some(x => x instanceof RegExp ? x.test(key) : x === key); // eslint-disable-line no-confusing-arrow
}
19 changes: 10 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@
"description": "Abstraction bin tool",
"main": "index.js",
"dependencies": {
"chalk": "^1.1.3",
"chalk": "^2.1.0",
"change-case": "^3.0.1",
"co": "^4.6.0",
"dargs": "^5.1.0",
"debug": "^2.6.8",
"is-type-of": "^1.0.0",
"yargs": "^8.0.1"
"debug": "^3.0.1",
"is-type-of": "^1.2.0",
"yargs": "^8.0.2",
"yargs-parser": "^7.0.0"
},
"devDependencies": {
"autod": "^2.8.0",
"coffee": "^3.3.2",
"egg-bin": "^3.4.1",
"autod": "^2.9.0",
"coffee": "^4.1.0",
"egg-bin": "^4.2.0",
"egg-ci": "^1.5.0",
"eslint": "^3.19.0",
"eslint-config-egg": "^4.2.0",
"eslint": "^4.6.1",
"eslint-config-egg": "^5.1.1",
"mm": "^2.1.0",
"rimraf": "^2.6.1",
"webstorm-disable-index": "^1.1.2"
Expand Down
34 changes: 34 additions & 0 deletions test/fixtures/my-bin/command/parserDebug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'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({ argv, execArgv, debugPort, debugOptions }) {
console.log('argv: %j', argv);
console.log('execArgv: %s', execArgv);
console.log('debugPort: %s', debugPort);
console.log('debugOptions: %j', debugOptions);
}

get description() {
return 'custom context';
}
}

module.exports = ContextCommand;
2 changes: 1 addition & 1 deletion test/fixtures/test-files/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "test-files",
"dependencies": {
"egg-init-config": "~1.2.0"
"egg-init-config": "^1.2.0"
}
}
95 changes: 95 additions & 0 deletions test/my-bin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,101 @@ describe('test/my-bin.test.js', () => {
.end(done);
});

it('my-bin parserDebug', done => {
const args = [
'parserDebug',
'--baseDir=./dist',
'--debug', '--debug-brk=5555',
'--expose_debug_as=v8debug',
'--inspect', '6666', '--inspect-brk',
'--debug-invalid',
];
coffee.fork(myBin, args, { cwd })
// .debug()
// .coverage(false)
.notExpect('stdout', /"b":".\/dist"/)
.expect('stdout', /"baseDir":".\/dist"/)
.expect('stdout', /"debug-invalid":true,"debugInvalid":true/)
.notExpect('stdout', /argv: {.*"debug-brk":5555,/)
.notExpect('stdout', /argv: {.*"debugBrk":5555,/)
.expect('stdout', /execArgv: --debug,--debug-brk=5555,--expose_debug_as=v8debug,--inspect=6666,--inspect-brk/)
.expect('stdout', /debugPort: 6666/)
.expect('stdout', /debugOptions: {"debug":true,"debug-brk":5555,"inspect":6666,"inspect-brk":true}/)
.expect('code', 0)
.end(done);
});

it('my-bin parserDebug without execArgv', done => {
const args = [
'parserDebug',
'--baseDir=./dist',
'--debug-invalid',
];
coffee.fork(myBin, args, { cwd })
// .debug()
// .coverage(false)
.expect('stdout', /"debug-invalid":true,"debugInvalid":true/)
.expect('stdout', /execArgv: undefined/)
.expect('stdout', /debugPort: undefined/)
.expect('stdout', /debugOptions: undefined/)
.expect('code', 0)
.end(done);
});

it('my-bin parserDebug $NODE_DEBUG_OPTION without port', done => {
const args = [
'parserDebug',
'--baseDir=./dist',
'--debug-port=5555',
];
coffee.fork(myBin, args, { cwd, env: Object.assign({}, process.env, { NODE_DEBUG_OPTION: '--debug-brk --expose_debug_as=v8debug' }) })
// .debug()
// .coverage(false)
.notExpect('stdout', /argv: {.*"debug-brk"/)
.notExpect('stdout', /argv: {.*"debugBrk"/)
.expect('stdout', /execArgv: --debug-port=5555,--debug-brk,--expose_debug_as=v8debug/)
.expect('stdout', /debugPort: 5555/)
.expect('stdout', /debugOptions: {"debug-port":5555,"debug-brk":true}/)
.expect('code', 0)
.end(done);
});

it('my-bin parserDebug $NODE_DEBUG_OPTION 6.x', done => {
const args = [
'parserDebug',
'--baseDir=./dist',
'--debug-port=5555',
];
coffee.fork(myBin, args, { cwd, env: Object.assign({}, process.env, { NODE_DEBUG_OPTION: '--debug-brk=6666 --expose_debug_as=v8debug' }) })
// .debug()
// .coverage(false)
.notExpect('stdout', /argv: {.*"debug-brk"/)
.notExpect('stdout', /argv: {.*"debugBrk"/)
.expect('stdout', /execArgv: --debug-port=5555,--debug-brk=6666,--expose_debug_as=v8debug/)
.expect('stdout', /debugPort: 6666/)
.expect('stdout', /debugOptions: {"debug-port":5555,"debug-brk":6666}/)
.expect('code', 0)
.end(done);
});

it('my-bin parserDebug $NODE_DEBUG_OPTION 8.x', done => {
const args = [
'parserDebug',
'--baseDir=./dist',
'--debug-port=5555',
];
coffee.fork(myBin, args, { cwd, env: Object.assign({}, process.env, { NODE_DEBUG_OPTION: '--inspect-brk=6666' }) })
// .debug()
// .coverage(false)
.notExpect('stdout', /argv: {.*"inspect-brk"/)
.notExpect('stdout', /argv: {.*"inspectBrk"/)
.expect('stdout', /execArgv: --debug-port=5555,--inspect-brk=6666/)
.expect('stdout', /debugPort: 6666/)
.expect('stdout', /debugOptions: {"debug-port":5555,"inspect-brk":6666}/)
.expect('code', 0)
.end(done);
});

it('my-bin parserOptions', done => {
const args = [
'parserOptions',
Expand Down
17 changes: 17 additions & 0 deletions test/my-helper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,21 @@ describe('test/my-helper.test.js', () => {
});
assert.deepEqual(execArgv, [ '--debug=5555', '--harmony', '--harmony_default_parameters' ]);
});

it('helper.extractExecArgv', () => {
const args = [
'echo',
'--baseDir=./dist',
'--debug=5555', '--debug-brk',
'--inspect', '6666', '--inspect-brk=7777',
'--inspect-port', '--debug-port=8888',
'--debug-invalid',
'--es_staging', '--harmony', '--harmony_default_parameters',
];
const argv = yargs.parse(args);
const { debugPort, debugOptions, execArgvObj } = helper.extractExecArgv(argv);
assert(debugPort === 8888);
assert.deepEqual(debugOptions, { debug: 5555, 'debug-brk': true, inspect: 6666, 'inspect-brk': 7777, 'inspect-port': true, 'debug-port': 8888 });
assert.deepEqual(execArgvObj, { debug: 5555, 'debug-brk': true, inspect: 6666, 'inspect-brk': 7777, 'inspect-port': true, 'debug-port': 8888, es_staging: true, harmony: true, harmony_default_parameters: true });
});
});

0 comments on commit a90e63c

Please sign in to comment.