Skip to content

Commit

Permalink
refactor: egg-bin debug pass debugOptions to startCluster
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 authored and fengmk2 committed Sep 7, 2017
1 parent 831c77d commit f7628b2
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 26 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,20 @@ $ 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
62 changes: 45 additions & 17 deletions lib/cmd/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ const Command = require('./dev');
class DebugCommand extends Command {
constructor(rawArgv) {
super(rawArgv);
const newDebugger = semver.gte(process.version, '8.0.0');
// egg-bin debug --debug-port=6666 --agent=5555 --brk --agent-brk
this.usage = 'Usage: egg-bin debug [dir] [options]';
this.options = {
// set default to empty so `--inspect` will always pass to fork
inspect: {
description: 'V8 Inspector port',
default() {
/* istanbul ignore next */
return newDebugger ? '' : undefined;
},
debug: {
alias: 'inspect',
description: 'auto detect the protocol used by the targeted runtime, use inspect at 8.x+',
default: true,
},
'inspect-brk': {
description: 'whether break at start',
'debug-port': {
description: 'worker debug port, default to 9229(inspect) or 5858(debug)',
},

debug: {
description: 'legacy debugger',
default() {
/* istanbul ignore next */
return newDebugger ? undefined : '';
},
'debug-brk': {
alias: 'brk',
description: 'whether stop at the top of worker initial script',
},
'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',
},
};
process.env.EGG_DEBUG = 'true';
Expand All @@ -35,6 +35,34 @@ 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;
2 changes: 1 addition & 1 deletion test/fixtures/demo-app/node_modules/aliyun-egg/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 64 additions & 8 deletions test/lib/cmd/debug.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,73 @@ 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()
.expect('stderr', /Debugger listening/)
// node 8 missing "chrome-devtools" url
// .expect('stderr', /chrome-devtools:/)
.expect('stdout', /"workers":1/)
.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('code', 0)
.end();
});

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

it('should debug with $NODE_DEBUG_OPTION', () => {
const env = Object.assign({}, process.env, { NODE_DEBUG_OPTION: '--inspect=5555' });
const env = Object.assign({}, process.env, { NODE_DEBUG_OPTION: '--inspect-brk=6666' });
return coffee.fork(eggBin, [ 'debug' ], { cwd, env })
// .debug()
.expect('stderr', /Debugger listening.*5555/)
.debug()
.notExpect('stderr', /Debugger listening/)
.expect('stdout', /options: {.*"debug":6666/)
.expect('stdout', /options: {.*"debugBrk":true/)
.expect('stdout', /"workers":1/)
.expect('code', 0)
.end();
Expand Down

0 comments on commit f7628b2

Please sign in to comment.