Skip to content
Permalink
Browse files

feat: debug proxy support

  • Loading branch information
atian25 authored and fengmk2 committed Sep 13, 2017
1 parent 3e8ce0d commit 678b83d64ad850ac390607ee281e5336473da808
@@ -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
@@ -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,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;
@@ -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"
}
}
}
@@ -0,0 +1,7 @@
'use strict';

module.exports = app => {
app.get('/', function* () {
this.body = 'hi, egg';
});
};
@@ -0,0 +1,3 @@
'use strict';

exports.key = '12345';

Some generated files are not rendered by default. Learn more.

Some generated files are not rendered by default. Learn more.

@@ -0,0 +1,3 @@
{
"name": "example"
}
@@ -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();
});
});
});
@@ -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()

0 comments on commit 678b83d

Please sign in to comment.
You can’t perform that action at this time.