diff --git a/README.md b/README.md index ee19d95e..de998871 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/cmd/debug.js b/lib/cmd/debug.js index 242e50a0..7595a9d8 100644 --- a/lib/cmd/debug.js +++ b/lib/cmd/debug.js @@ -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'; @@ -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; diff --git a/test/fixtures/demo-app/node_modules/aliyun-egg/index.js b/test/fixtures/demo-app/node_modules/aliyun-egg/index.js index 95608e45..dc41c309 100644 --- a/test/fixtures/demo-app/node_modules/aliyun-egg/index.js +++ b/test/fixtures/demo-app/node_modules/aliyun-egg/index.js @@ -1,7 +1,7 @@ 'use strict'; exports.startCluster = options => { - console.log('%j', options); + console.log('options: %j', options); if (process.execArgv.length) { console.log('process.execArgv:', process.execArgv); } diff --git a/test/lib/cmd/debug.test.js b/test/lib/cmd/debug.test.js index 594cffa3..9ca0d554 100644 --- a/test/lib/cmd/debug.test.js +++ b/test/lib/cmd/debug.test.js @@ -13,11 +13,64 @@ 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(); }); @@ -25,7 +78,8 @@ describe('test/lib/cmd/debug.test.js', () => { 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"/) @@ -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();