From b72675ee2d27fbf31a55de6e82758e666cbcbcbc Mon Sep 17 00:00:00 2001 From: TZ Date: Wed, 13 Sep 2017 10:27:00 +0800 Subject: [PATCH 1/5] feat: revert to 4.2.0 --- README.md | 10 ---- lib/cmd/debug.js | 63 ++++++--------------- lib/command.js | 69 +++++++++++++++++++---- test/fixtures/my-egg-bin/lib/cmd/echo.js | 5 +- test/lib/cmd/debug.test.js | 72 +++--------------------- test/my-egg-bin.test.js | 7 +-- 6 files changed, 88 insertions(+), 138 deletions(-) diff --git a/README.md b/README.md index de998871..ee19d95e 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/cmd/debug.js b/lib/cmd/debug.js index 63ea2a4a..242e50a0 100644 --- a/lib/cmd/debug.js +++ b/lib/cmd/debug.js @@ -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'; @@ -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; diff --git a/lib/command.js b/lib/command.js index 096a9966..3f7a93ee 100644 --- a/lib/command.js +++ b/lib/command.js @@ -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; diff --git a/test/fixtures/my-egg-bin/lib/cmd/echo.js b/test/fixtures/my-egg-bin/lib/cmd/echo.js index 138d0b1a..d139d80a 100644 --- a/test/fixtures/my-egg-bin/lib/cmd/echo.js +++ b/test/fixtures/my-egg-bin/lib/cmd/echo.js @@ -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); } } diff --git a/test/lib/cmd/debug.test.js b/test/lib/cmd/debug.test.js index 9ca0d554..594cffa3 100644 --- a/test/lib/cmd/debug.test.js +++ b/test/lib/cmd/debug.test.js @@ -13,64 +13,11 @@ 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(); }); @@ -78,8 +25,7 @@ describe('test/lib/cmd/debug.test.js', () => { 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"/) @@ -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(); diff --git a/test/my-egg-bin.test.js b/test/my-egg-bin.test.js index 4c574eab..76f57f3f 100644 --- a/test/my-egg-bin.test.js +++ b/test/my-egg-bin.test.js @@ -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); }); From b88c042d43fa378601b5dd8a17562b67cac685c0 Mon Sep 17 00:00:00 2001 From: TZ Date: Tue, 5 Sep 2017 14:31:05 +0800 Subject: [PATCH 2/5] refactor: use common-bin parse execArgv --- lib/command.js | 69 ++++-------------------- test/fixtures/my-egg-bin/lib/cmd/echo.js | 5 +- test/my-egg-bin.test.js | 7 +-- 3 files changed, 19 insertions(+), 62 deletions(-) diff --git a/lib/command.js b/lib/command.js index 3f7a93ee..096a9966 100644 --- a/lib/command.js +++ b/lib/command.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; diff --git a/test/fixtures/my-egg-bin/lib/cmd/echo.js b/test/fixtures/my-egg-bin/lib/cmd/echo.js index d139d80a..138d0b1a 100644 --- a/test/fixtures/my-egg-bin/lib/cmd/echo.js +++ b/test/fixtures/my-egg-bin/lib/cmd/echo.js @@ -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); } } diff --git a/test/my-egg-bin.test.js b/test/my-egg-bin.test.js index 76f57f3f..4c574eab 100644 --- a/test/my-egg-bin.test.js +++ b/test/my-egg-bin.test.js @@ -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); }); From 055792941c25b69faa52f8b640453ec630324892 Mon Sep 17 00:00:00 2001 From: TZ Date: Wed, 13 Sep 2017 14:36:43 +0800 Subject: [PATCH 3/5] feat: debug proxy support --- .gitignore | 2 + lib/cmd/debug.js | 39 +++++++++++++++++++ package.json | 4 +- test/fixtures/example/app/router.js | 7 ++++ .../fixtures/example/config/config.default.js | 3 ++ .../example/node_modules/egg/index.js | 8 ++++ .../example/node_modules/egg/package.json | 3 ++ test/fixtures/example/package.json | 3 ++ test/lib/cmd/debug.test.js | 24 +++++++++++- test/lib/cmd/dev.test.js | 8 ---- 10 files changed, 91 insertions(+), 10 deletions(-) create mode 100644 test/fixtures/example/app/router.js create mode 100644 test/fixtures/example/config/config.default.js create mode 100644 test/fixtures/example/node_modules/egg/index.js create mode 100644 test/fixtures/example/node_modules/egg/package.json create mode 100644 test/fixtures/example/package.json diff --git a/.gitignore b/.gitignore index d8340224..348042ed 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,8 @@ coverage/ !test/fixtures/demo-app/node_modules/aliyun-egg/node_modules/ !test/fixtures/test-files-glob/** !test/fixtures/test-files-stack/node_modules/ +!test/fixtures/example/node_modules/ +**/run/*.json .tmp .vscode *.log diff --git a/lib/cmd/debug.js b/lib/cmd/debug.js index 242e50a0..5bd312eb 100644 --- a/lib/cmd/debug.js +++ b/lib/cmd/debug.js @@ -1,5 +1,9 @@ 'use strict'; +const cp = require('child_process'); +const chalk = require('chalk'); +const InspectorProxy = require('inspector-proxy'); +const debug = require('debug')('egg-bin'); const semver = require('semver'); const Command = require('./dev'); @@ -28,6 +32,11 @@ class DebugCommand extends Command { return newDebugger ? undefined : ''; }, }, + + proxy: { + description: 'worker debug proxy port', + default: 9999, + }, }; process.env.EGG_DEBUG = 'true'; } @@ -35,6 +44,36 @@ class DebugCommand extends Command { get description() { return 'Start server at local debug mode'; } + + * run(context) { + const proxyPort = context.argv.proxy; + context.argv.proxy = undefined; + + const eggArgs = yield this.formatArgs(context); + const options = { + execArgv: context.execArgv, + env: Object.assign({ NODE_ENV: 'development', EGG_DEBUG: true }, context.env), + }; + debug('%s %j %j, %j', this.serverBin, eggArgs, options.execArgv, options.env.NODE_ENV); + + // start egg + const child = cp.fork(this.serverBin, eggArgs, options); + + // start debug proxy + const proxy = new InspectorProxy({ port: proxyPort }); + // proxy to new worker + child.on('message', msg => { + if (msg && msg.action === 'debug' && msg.from === 'app') { + const { debugPort, pid } = msg.data; + debug(`recieve new worker#${pid} debugPort: ${debugPort}`); + proxy.start({ debugPort }).then(() => { + console.log(chalk.yellow(`DevTools → ${proxy.url}`)); + }); + } + }); + + child.on('exit', () => proxy.end()); + } } module.exports = DebugCommand; diff --git a/package.json b/package.json index 91f64f73..1da8873c 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,9 @@ "babel-register": "^6.4.3", "coffee": "^4.1.0", "cross-env": "^3.1.3", + "egg": "^1.8.0", "egg-ci": "^1.8.0", + "egg-mock": "^3.12.0", "enzyme": "^2.0.0", "eslint": "^4.6.1", "eslint-config-egg": "^5.1.1", @@ -71,4 +73,4 @@ "ci": { "version": "6, 8" } -} \ No newline at end of file +} diff --git a/test/fixtures/example/app/router.js b/test/fixtures/example/app/router.js new file mode 100644 index 00000000..8bfd2442 --- /dev/null +++ b/test/fixtures/example/app/router.js @@ -0,0 +1,7 @@ +'use strict'; + +module.exports = app => { + app.get('/', function* () { + this.body = 'hi, egg'; + }); +}; diff --git a/test/fixtures/example/config/config.default.js b/test/fixtures/example/config/config.default.js new file mode 100644 index 00000000..762c2647 --- /dev/null +++ b/test/fixtures/example/config/config.default.js @@ -0,0 +1,3 @@ +'use strict'; + +exports.key = '12345'; diff --git a/test/fixtures/example/node_modules/egg/index.js b/test/fixtures/example/node_modules/egg/index.js new file mode 100644 index 00000000..2060e8f7 --- /dev/null +++ b/test/fixtures/example/node_modules/egg/index.js @@ -0,0 +1,8 @@ +'use strict'; + +module.exports = require('../../../../../node_modules/egg'); + +setTimeout(() => { + console.log('exit by master test end') + process.exit(0); +}, 5000); diff --git a/test/fixtures/example/node_modules/egg/package.json b/test/fixtures/example/node_modules/egg/package.json new file mode 100644 index 00000000..6697ad3f --- /dev/null +++ b/test/fixtures/example/node_modules/egg/package.json @@ -0,0 +1,3 @@ +{ + "name": "egg" +} diff --git a/test/fixtures/example/package.json b/test/fixtures/example/package.json new file mode 100644 index 00000000..8389bd69 --- /dev/null +++ b/test/fixtures/example/package.json @@ -0,0 +1,3 @@ +{ + "name": "example" +} \ No newline at end of file diff --git a/test/lib/cmd/debug.test.js b/test/lib/cmd/debug.test.js index 594cffa3..09a184e8 100644 --- a/test/lib/cmd/debug.test.js +++ b/test/lib/cmd/debug.test.js @@ -2,7 +2,7 @@ const path = require('path'); const coffee = require('coffee'); -const mm = require('mm'); +const mm = require('egg-mock'); const net = require('net'); describe('test/lib/cmd/debug.test.js', () => { @@ -62,4 +62,26 @@ describe('test/lib/cmd/debug.test.js', () => { .end(); }); }); + + describe('real egg', () => { + const cwd = path.join(__dirname, '../../fixtures/example'); + + it('should proxy', () => { + return coffee.fork(eggBin, [ 'debug' ], { cwd }) + // .debug() + .expect('stderr', /Debugger listening/) + .expect('stdout', /DevTools → chrome-devtools:.*:9999/) + .expect('code', 0) + .end(); + }); + + it('should proxy with port', () => { + return coffee.fork(eggBin, [ 'debug', '--proxy=6666' ], { cwd }) + // .debug() + .expect('stderr', /Debugger listening/) + .expect('stdout', /DevTools → chrome-devtools:.*:6666/) + .expect('code', 0) + .end(); + }); + }); }); diff --git a/test/lib/cmd/dev.test.js b/test/lib/cmd/dev.test.js index 36411684..cdaf7c13 100644 --- a/test/lib/cmd/dev.test.js +++ b/test/lib/cmd/dev.test.js @@ -130,14 +130,6 @@ describe('test/lib/cmd/dev.test.js', () => { }); }); - it.skip('should startCluster with execArgv --debug', done => { - coffee.fork(eggBin, [ 'dev', '--debug=7000' ], { cwd }) - .debug() - .expect('stderr', /Debugger listening on .*7000/) - .expect('code', 0) - .end(done); - }); - it('should startCluster with execArgv --inspect', done => { coffee.fork(eggBin, [ 'dev', '--inspect=7000' ], { cwd }) // .debug() From ca35090e9af6bafd7882845d118dcd29925e5145 Mon Sep 17 00:00:00 2001 From: TZ Date: Wed, 13 Sep 2017 16:18:16 +0800 Subject: [PATCH 4/5] fix: debug at 6.x --- lib/cmd/debug.js | 5 +++-- package.json | 9 ++++++--- test/lib/cmd/debug.test.js | 24 ++++++++++++++---------- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/lib/cmd/debug.js b/lib/cmd/debug.js index 5bd312eb..7a4adf00 100644 --- a/lib/cmd/debug.js +++ b/lib/cmd/debug.js @@ -6,11 +6,11 @@ const InspectorProxy = require('inspector-proxy'); const debug = require('debug')('egg-bin'); const semver = require('semver'); const Command = require('./dev'); +const newDebugger = semver.gte(process.version, '8.0.0'); class DebugCommand extends Command { constructor(rawArgv) { super(rawArgv); - const newDebugger = semver.gte(process.version, '8.0.0'); this.usage = 'Usage: egg-bin debug [dir] [options]'; this.options = { // set default to empty so `--inspect` will always pass to fork @@ -67,7 +67,8 @@ class DebugCommand extends Command { const { debugPort, pid } = msg.data; debug(`recieve new worker#${pid} debugPort: ${debugPort}`); proxy.start({ debugPort }).then(() => { - console.log(chalk.yellow(`DevTools → ${proxy.url}`)); + console.log(chalk.yellow(`Debug Proxy online, now you could attach to ${proxyPort} without worry about reload.`)); + if (newDebugger) console.log(chalk.yellow(`DevTools → ${proxy.url}`)); }); } }); diff --git a/package.json b/package.json index 1da8873c..0673a782 100644 --- a/package.json +++ b/package.json @@ -9,17 +9,20 @@ }, "dependencies": { "autod": "^2.9.0", + "chalk": "^2.1.0", "co-mocha": "^1.2.0", "common-bin": "^2.7.0", "debug": "^3.0.1", "detect-port": "^1.2.1", "egg-utils": "^2.2.0", "globby": "^6.1.0", + "inspector-proxy": "^1.0.0", "intelli-espower-loader": "^1.0.1", - "mocha": "^3.5.0", + "mocha": "^3.5.3", "mz-modules": "^2.0.0", - "nyc": "^11.2.0", + "nyc": "^11.2.1", "power-assert": "^1.4.4", + "semver": "^5.4.1", "test-exclude": "^4.1.1", "ypkgfiles": "^1.4.0" }, @@ -36,7 +39,7 @@ "eslint": "^4.6.1", "eslint-config-egg": "^5.1.1", "jsdom": "^8.0.1", - "mm": "^2.1.0", + "mm": "^2.2.0", "mz": "^2.6.0", "react": "^0.14.7", "react-addons-test-utils": "^0.14.7", diff --git a/test/lib/cmd/debug.test.js b/test/lib/cmd/debug.test.js index 09a184e8..59cb0992 100644 --- a/test/lib/cmd/debug.test.js +++ b/test/lib/cmd/debug.test.js @@ -4,6 +4,7 @@ const path = require('path'); const coffee = require('coffee'); const mm = require('egg-mock'); const net = require('net'); +const semver = require('semver'); describe('test/lib/cmd/debug.test.js', () => { const eggBin = require.resolve('../../../bin/egg-bin.js'); @@ -65,21 +66,24 @@ describe('test/lib/cmd/debug.test.js', () => { describe('real egg', () => { const cwd = path.join(__dirname, '../../fixtures/example'); + const newDebugger = semver.gte(process.version, '8.0.0'); - it('should proxy', () => { - return coffee.fork(eggBin, [ 'debug' ], { cwd }) - // .debug() - .expect('stderr', /Debugger listening/) - .expect('stdout', /DevTools → chrome-devtools:.*:9999/) + it('should proxy', function* () { + const app = coffee.fork(eggBin, [ 'debug' ], { cwd }); + // app.debug(); + if (newDebugger) app.expect('stdout', /DevTools → chrome-devtools:.*:9999/); + yield app.expect('stderr', /Debugger listening/) + .expect('stdout', /Debug Proxy online, now you could attach to 9999/) .expect('code', 0) .end(); }); - it('should proxy with port', () => { - return coffee.fork(eggBin, [ 'debug', '--proxy=6666' ], { cwd }) - // .debug() - .expect('stderr', /Debugger listening/) - .expect('stdout', /DevTools → chrome-devtools:.*:6666/) + it('should proxy with port', function* () { + const app = coffee.fork(eggBin, [ 'debug', '--proxy=6666' ], { cwd }); + // app.debug(); + if (newDebugger) app.expect('stdout', /DevTools → chrome-devtools:.*:6666/); + yield app.expect('stderr', /Debugger listening/) + .expect('stdout', /Debug Proxy online, now you could attach to 6666/) .expect('code', 0) .end(); }); From b655eef7500d3b924ad3a5904fec5b6e5b359a8c Mon Sep 17 00:00:00 2001 From: TZ Date: Wed, 13 Sep 2017 16:23:29 +0800 Subject: [PATCH 5/5] docs: add readme --- README.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ee19d95e..2c6bc23a 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. + +use [inspector-proxy](https://github.com/whxaxes/inspector-proxy) to proxy worker debug, so you don't need to worry about reload. + ```bash -$ egg-bin debug +$ egg-bin debug --debug-port=9229 --proxy=9999 ``` +##### options + +- all `egg-bin dev` options is accepted. +- `--proxy=9999` worker debug proxy port. + + ### test Using [mocha] with [co-mocha] to run test.