Skip to content

Commit

Permalink
feat: debug proxy support
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 authored and fengmk2 committed Sep 13, 2017
1 parent 3e8ce0d commit 678b83d
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions lib/cmd/debug.js
Original file line number Diff line number Diff line change
@@ -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');

Expand Down Expand Up @@ -28,13 +32,48 @@ class DebugCommand extends Command {
return newDebugger ? undefined : '';
},
},

proxy: {
description: 'worker debug proxy port',
default: 9999,
},
};
process.env.EGG_DEBUG = 'true';
}

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;
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -71,4 +73,4 @@
"ci": {
"version": "6, 8"
}
}
}
7 changes: 7 additions & 0 deletions test/fixtures/example/app/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = app => {
app.get('/', function* () {
this.body = 'hi, egg';
});
};
3 changes: 3 additions & 0 deletions test/fixtures/example/config/config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

exports.key = '12345';
8 changes: 8 additions & 0 deletions test/fixtures/example/node_modules/egg/index.js

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

3 changes: 3 additions & 0 deletions test/fixtures/example/node_modules/egg/package.json

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

3 changes: 3 additions & 0 deletions test/fixtures/example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "example"
}
24 changes: 23 additions & 1 deletion test/lib/cmd/debug.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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();
});
});
});
8 changes: 0 additions & 8 deletions test/lib/cmd/dev.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 678b83d

Please sign in to comment.