Skip to content
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

Merged
merged 7 commits into from
Aug 30, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ All the commands support these specific v8 options:
$ egg-bin [command] --debug --es_staging
```

if `process.env.NODE_DEBUG_OPTION` is provided (WebStorm etc), will use it as debug options.

### dev

Start dev cluster on `local` env, it will start a master, an agent and a worker.
Expand Down
67 changes: 44 additions & 23 deletions lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

class Command extends BaseCommand {
/**
Expand All @@ -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) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

存在这个变量的时候,优先使用。

console.log('Use $NODE_DEBUG_OPTION: %s', context.env.NODE_DEBUG_OPTION);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

console?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我改为 debug

Copy link
Member Author

Choose a reason for hiding this comment

The 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);
Expand All @@ -50,4 +40,35 @@ class Command extends BaseCommand {
}
}

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;
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@
"autod": "^2.9.0",
"change-case": "^3.0.1",
"co-mocha": "^1.2.0",
"common-bin": "^2.4.0",
"debug": "^2.6.8",
"common-bin": "^2.6.0",
"debug": "^3.0.1",
"detect-port": "^1.2.1",
"egg-utils": "^2.2.0",
"globby": "^6.1.0",
"intelli-espower-loader": "^1.0.1",
"mocha": "^3.5.0",
"mz-modules": "^1.0.0",
"mz-modules": "^2.0.0",
"nyc": "^11.1.0",
"power-assert": "^1.4.4",
"test-exclude": "^4.1.1",
"yargs-parser": "^7.0.0",
"ypkgfiles": "^1.4.0"
},
"devDependencies": {
Expand All @@ -32,8 +33,8 @@
"cross-env": "^3.1.3",
"egg-ci": "^1.8.0",
"enzyme": "^2.0.0",
"eslint": "^4.3.0",
"eslint-config-egg": "^5.0.0",
"eslint": "^4.5.0",
"eslint-config-egg": "^5.1.0",
"jsdom": "^8.0.1",
"mm": "^2.1.0",
"mz": "^2.6.0",
Expand Down
10 changes: 10 additions & 0 deletions test/lib/cmd/debug.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ describe('test/lib/cmd/debug.test.js', () => {
.end(done);
});

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

describe('auto detect available port', () => {
let server;
before(done => {
Expand Down