-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support $NODE_DEBUG_OPTION #71
Changes from 5 commits
db01134
476ea11
d82ff83
158ef9b
7fad4be
4114186
37c9964
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
'use strict'; | ||
|
||
const debug = require('debug')('egg-bin:test'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 为啥要改掉? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这几条指令不会同时执行的,简化下 DEBUG 变量配置。 |
||
const debug = require('debug')('egg-bin'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const globby = require('globby'); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
const BaseCommand = require('common-bin'); | ||
const changeCase = require('change-case'); | ||
const parser = require('yargs-parser'); | ||
|
||
class Command extends BaseCommand { | ||
/** | ||
|
@@ -13,30 +14,19 @@ class Command extends BaseCommand { | |
const context = super.context; | ||
const argv = context.argv; | ||
|
||
// extract execArgv to special item | ||
const execArgvObj = {}; | ||
let execArgvObj = {}; | ||
let debugPort; | ||
const match = (key, arr) => arr.some(x => x instanceof RegExp ? x.test(key) : x === key); // eslint-disable-line no-confusing-arrow | ||
for (const key of Object.keys(argv)) { | ||
let isMatch = false; | ||
|
||
// debug / debug-brk / debug-port / inspect / inspect-brk / inspect-port | ||
if (match(key, [ /^debug.*/, /^inspect.*/ ])) { | ||
isMatch = true; | ||
// extract debug port | ||
if (debugPort === undefined || typeof argv[key] === 'number') { | ||
debugPort = argv[key]; | ||
} | ||
} else if (match(key, [ 'es_staging', 'expose_debug_as', /^harmony.*/ ])) { | ||
isMatch = true; | ||
} | ||
|
||
if (isMatch) { | ||
execArgvObj[key] = argv[key]; | ||
argv[key] = undefined; | ||
// also remove `debugBrk` | ||
argv[changeCase.camelCase(key)] = undefined; | ||
} | ||
|
||
// extract from command argv | ||
debugPort = findDebugPort(argv); | ||
execArgvObj = extractExecArgv(argv); | ||
|
||
// extract from WebStorm env `$NODE_DEBUG_OPTION` | ||
if (context.env.NODE_DEBUG_OPTION) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 存在这个变量的时候,优先使用。 |
||
console.log('Use $NODE_DEBUG_OPTION: %s', context.env.NODE_DEBUG_OPTION); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. console? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 我改为 debug There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 还是用 console 吧,可以看到 WebStorm 配置的环境变量值。 |
||
const argvFromEnv = parser(context.env.NODE_DEBUG_OPTION); | ||
debugPort = findDebugPort(argvFromEnv); | ||
Object.assign(execArgvObj, extractExecArgv(argvFromEnv)); | ||
} | ||
|
||
context.execArgv = this.helper.unparseArgv(execArgvObj); | ||
|
@@ -45,9 +35,41 @@ class Command extends BaseCommand { | |
|
||
// remove unuse args | ||
argv.$0 = undefined; | ||
argv.v = 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; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,15 +11,15 @@ describe('test/lib/cmd/debug.test.js', () => { | |
|
||
afterEach(mm.restore); | ||
|
||
it('should startCluster success', () => { | ||
return coffee.fork(eggBin, [ 'debug' ], { cwd }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 不应该的啊,默认是返回 promise 的,跟 done 效果是一样的 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 改了 |
||
.debug() | ||
it('should startCluster success', done => { | ||
coffee.fork(eggBin, [ 'debug' ], { cwd }) | ||
// .debug() | ||
.expect('stderr', /Debugger listening/) | ||
// node 8 missing "chrome-devtools" url | ||
// .expect('stderr', /chrome-devtools:/) | ||
.expect('stdout', /"workers":1/) | ||
.expect('code', 0) | ||
.end(); | ||
.end(done); | ||
}); | ||
|
||
it('should startCluster with port', done => { | ||
|
@@ -30,7 +30,16 @@ describe('test/lib/cmd/debug.test.js', () => { | |
.expect('stdout', /"workers":1/) | ||
.expect('stdout', /"baseDir":".*?demo-app"/) | ||
.expect('stdout', /"framework":".*?aliyun-egg"/) | ||
.expect('stdout', /--inspect/) | ||
.expect('code', 0) | ||
.end(done); | ||
}); | ||
|
||
it('should debug with $NODE_DEBUG_OPTION', done => { | ||
const env = Object.assign({}, process.env, { NODE_DEBUG_OPTION: '--inspect=5555' }); | ||
coffee.fork(eggBin, [ 'debug' ], { cwd, env }) | ||
.debug() | ||
.expect('stderr', /Debugger listening.*5555/) | ||
.expect('stdout', /"workers":1/) | ||
.expect('code', 0) | ||
.end(done); | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
加个检测,这样不管在什么版本下,都只需
egg-bin debug
而不用在 6.x 改为egg-bin dev --debug