Skip to content

Commit

Permalink
feat: revert to 4.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 authored and fengmk2 committed Sep 13, 2017
1 parent cf052f1 commit c65a00d
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 138 deletions.
10 changes: 0 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,10 @@ $ egg-bin dev

Debug egg app with [V8 Inspector Integration](https://nodejs.org/api/debugger.html#debugger_v8_inspector_integration_for_node_js).

automatically detect the protocol used by the targeted runtime, 8.0+ the new 'inspector' protocol is used.

```bash
$ egg-bin debug
```

##### options

- all `egg-bin dev` options is accepted.
- `--debug-port=6666` worker debug port, default to 9229(inspect) or 5858(debug), also has `--inspect` alias.
- `--debug-brk` whether stop at the top of worker initial script, also has `--brk` alias.
- `--debug-agent=7777` whether debug agent, could pass Number as debugPort, default to 9227(inspect) or 5856(debug), also has `--agent` alias.
- `--debug-agent-brk` whether stop at the top of agent initial script.

### test

Using [mocha] with [co-mocha] to run test.
Expand Down
63 changes: 18 additions & 45 deletions lib/cmd/debug.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
'use strict';

const semver = require('semver');
const Command = require('./dev');

class DebugCommand extends Command {
constructor(rawArgv) {
super(rawArgv);
// egg-bin debug --debug-port=6666 --agent=5555 --brk --agent-brk
const newDebugger = semver.gte(process.version, '8.0.0');
this.usage = 'Usage: egg-bin debug [dir] [options]';
this.options = {
debug: {
alias: 'inspect',
description: 'auto detect the protocol used by the targeted runtime, use inspect at 8.x+',
default: true,
},
'debug-port': {
description: 'worker debug port, default to 9229(inspect) or 5858(debug)',
// set default to empty so `--inspect` will always pass to fork
inspect: {
description: 'V8 Inspector port',
default() {
/* istanbul ignore next */
return newDebugger ? '' : undefined;
},
},
'debug-brk': {
alias: 'brk',
description: 'whether stop at the top of worker initial script',
'inspect-brk': {
description: 'whether break at start',
},
'debug-agent': {
alias: 'agent',
description: 'whether debug agent, could pass Number as debugPort, default to 9227(inspect) or 5856(debug)',
},
'debug-agent-brk': {
description: 'whether stop at the top of agent initial script',

debug: {
description: 'legacy debugger',
default() {
/* istanbul ignore next */
return newDebugger ? undefined : '';
},
},
};
process.env.EGG_DEBUG = 'true';
Expand All @@ -34,34 +35,6 @@ class DebugCommand extends Command {
get description() {
return 'Start server at local debug mode';
}

get context() {
const context = super.context;
const { argv, execArgvObj, debugOptions, debugPort } = context;

// use debugPort extract from `--inspect=9999 / --debug-port=1111` etc, if not provide just pass true
argv.debug = debugPort || true;

if (debugOptions['inspect-brk'] || debugOptions['debug-brk']) {
argv.debugBrk = true;
}

// remove unused
argv['debug-port'] = undefined;
argv['debug-brk'] = undefined;
argv['debug-agent'] = undefined;
argv['debug-agent-brk'] = undefined;

// remove all debug options from execArgv
for (const key of Object.keys(debugOptions)) {
execArgvObj[key] = undefined;
}

// recreate execArgv array
context.execArgv = this.helper.unparseArgv(execArgvObj);

return context;
}
}

module.exports = DebugCommand;
69 changes: 58 additions & 11 deletions lib/command.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,74 @@
'use strict';

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

class Command extends BaseCommand {
constructor(rawArgv) {
super(rawArgv);
this.parserOptions = {
execArgv: true,
removeAlias: true,
};
}

/**
* normalize context
* @param {Object} context - { cwd, argv, rawArgv }
* @return {Object} return with { cwd, argv, execArgv, rawArgv }
*/
get context() {
const context = super.context;
const argv = context.argv;

let execArgvObj = {};
let debugPort;

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

// compatible
if (context.debugPort) context.debug = context.debugPort;
// 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;

// remove unuse args
context.argv.$0 = undefined;
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: 1 addition & 4 deletions test/fixtures/my-egg-bin/lib/cmd/echo.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ class EchoCommand extends Command {
}

* run(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);
console.log('%j', context);
}
}

Expand Down
72 changes: 8 additions & 64 deletions test/lib/cmd/debug.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,73 +13,19 @@ describe('test/lib/cmd/debug.test.js', () => {

it('should startCluster success', () => {
return coffee.fork(eggBin, [ 'debug' ], { cwd })
.debug()
.notExpect('stderr', /Debugger listening/)
.expect('stdout', /options: {.*"debug":true/)
.notExpect('stdout', /process.execArgv:/)
.expect('code', 0)
.end();
});

it('--debug-port=7777', () => {
return coffee.fork(eggBin, [ 'debug', '--debug-port=7777' ], { cwd })
// .debug()
.notExpect('stderr', /Debugger listening/)
.expect('stdout', /options: {.*"debug":7777/)
.notExpect('stdout', /process.execArgv:/)
.expect('code', 0)
.end();
});

it('--inspect=7777', () => {
return coffee.fork(eggBin, [ 'debug', '--inspect=7777' ], { cwd })
// .debug()
.notExpect('stderr', /Debugger listening/)
.expect('stdout', /options: {.*"debug":7777/)
.notExpect('stdout', /options: {.*"inspect"/)
.notExpect('stdout', /process.execArgv:/)
.expect('code', 0)
.end();
});

it('--debug-brk --debug-agent --debug-agent-brk --inspect-brk', () => {
return coffee.fork(eggBin, [ 'debug', '--debug-brk', '--debug-agent', '--debug-agent-brk', '--inspect-brk' ], { cwd })
// .debug()
.notExpect('stderr', /Debugger listening/)
.expect('stdout', /options: {.*"debugBrk":true,"debugAgent":true,"debugAgentBrk":true,"debug":true/)
.notExpect('stdout', /options: {.*"inspect"/)
.notExpect('stdout', /process.execArgv:/)
.expect('code', 0)
.end();
});

it('--brk --agent', () => {
return coffee.fork(eggBin, [ 'debug', '--brk', '--agent' ], { cwd })
// .debug()
.notExpect('stderr', /Debugger listening/)
.expect('stdout', /options: {.*"debugBrk":true,"debugAgent":true,"debug":true/)
.notExpect('stdout', /options: {.*"inspect"/)
.notExpect('stdout', /process.execArgv:/)
.expect('code', 0)
.end();
});

it('--agent=6666', () => {
return coffee.fork(eggBin, [ 'debug', '--agent=6666' ], { cwd })
// .debug()
.notExpect('stderr', /Debugger listening/)
.expect('stdout', /options: {"debugAgent":6666,"debug":true/)
.notExpect('stdout', /options: {.*"inspect"/)
.notExpect('stdout', /process.execArgv:/)
.expect('stderr', /Debugger listening/)
// node 8 missing "chrome-devtools" url
// .expect('stderr', /chrome-devtools:/)
.expect('stdout', /"workers":1/)
.expect('code', 0)
.end();
});

it('should startCluster with port', () => {
return coffee.fork(eggBin, [ 'debug', '--port', '6001' ], { cwd })
// .debug()
.notExpect('stderr', /Debugger listening/)
.expect('stdout', /options: {.*"debug":true/)
.expect('stderr', /Debugger listening/)
.expect('stdout', /"port":6001/)
.expect('stdout', /"workers":1/)
.expect('stdout', /"baseDir":".*?demo-app"/)
Expand All @@ -89,12 +35,10 @@ describe('test/lib/cmd/debug.test.js', () => {
});

it('should debug with $NODE_DEBUG_OPTION', () => {
const env = Object.assign({}, process.env, { NODE_DEBUG_OPTION: '--inspect-brk=6666' });
const env = Object.assign({}, process.env, { NODE_DEBUG_OPTION: '--inspect=5555' });
return coffee.fork(eggBin, [ 'debug' ], { cwd, env })
.debug()
.notExpect('stderr', /Debugger listening/)
.expect('stdout', /options: {.*"debug":6666/)
.expect('stdout', /options: {.*"debugBrk":true/)
// .debug()
.expect('stderr', /Debugger listening.*5555/)
.expect('stdout', /"workers":1/)
.expect('code', 0)
.end();
Expand Down
7 changes: 3 additions & 4 deletions test/my-egg-bin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,9 @@ describe('test/my-egg-bin.test.js', () => {
coffee.fork(eggBin, args, { cwd })
// .debug()
.expect('stdout', /"baseDir":".\/dist"/)
.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('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('code', 0)
.end(done);
});
Expand Down

0 comments on commit c65a00d

Please sign in to comment.