Skip to content

Commit

Permalink
feat: auto detect available port
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 committed Dec 15, 2016
1 parent e33f0a2 commit 22e26bf
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 29 deletions.
14 changes: 1 addition & 13 deletions lib/debug_command.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,7 @@ class DebugCommand extends Command {
}

* run(cwd, args) {
args.push('--baseDir');
args.push(cwd);
args.push('--cluster');
args.push('1');

const eggPath = this.getFrameworkOrEggPath(cwd);
if (eggPath) {
args.push(`--eggPath=${eggPath}`);
}
args = yield this.helper.formatArgs(cwd, args);

const options = {
env: Object.assign({}, process.env),
Expand Down Expand Up @@ -74,10 +66,6 @@ class DebugCommand extends Command {
help() {
return 'Debug mode start';
}

getFrameworkOrEggPath(cwd) {
return this.utils.getFrameworkOrEggPath(cwd);
}
}

module.exports = DebugCommand;
17 changes: 2 additions & 15 deletions lib/dev_command.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,10 @@ class DevCommand extends Command {
* run(cwd, args) {
const execArgv = args ? args.filter(str => str.indexOf('--debug') === 0 || str.indexOf('--inspect') === 0) : [];

args.push('--baseDir');
args.push(cwd);
args.push('--cluster');
args.push('1');

const eggPath = this.getFrameworkOrEggPath(cwd);

if (eggPath) {
args.push(`--eggPath=${eggPath}`);
}
args = yield this.helper.formatArgs(cwd, args);

const options = {
env: process.env,
env: Object.assign({}, process.env),
execArgv,
};

Expand All @@ -33,10 +24,6 @@ class DevCommand extends Command {
help() {
return 'local env start';
}

getFrameworkOrEggPath(cwd) {
return this.utils.getFrameworkOrEggPath(cwd);
}
}

module.exports = DevCommand;
26 changes: 26 additions & 0 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

const path = require('path');
const glob = require('glob');
const detect = require('detect-port');
const utils = require('egg-utils');

exports.defaultPort = 7001;
exports.serverBin = path.join(__dirname, 'start-cluster');

exports.getTestFiles = function() {
Expand All @@ -20,3 +23,26 @@ exports.getTestFiles = function() {
exports.checkDeps = function* () {
return true;
};

exports.formatArgs = function* (cwd, args) {
args.push('--baseDir');
args.push(cwd);
args.push('--cluster');
args.push('1');

const eggPath = utils.getFrameworkOrEggPath(cwd);
if (eggPath) {
args.push(`--eggPath=${eggPath}`);
}

// auto detect available port
if (args.indexOf('-p') === -1 && args.indexOf('--port') === -1) {
const port = yield detect(exports.defaultPort);
if (port !== exports.defaultPort) {
args.push('-p', port);
console.warn(`[egg-bin] server port ${exports.defaultPort} is in use, now using port ${port}\n`);
}
}
return args;
};

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"commander": "^2.9.0",
"common-bin": "^1.0.0",
"debug": "^2.3.3",
"detect-port": "^1.0.7",
"egg-utils": "^1.0.0",
"glob": "^7.1.1",
"intelli-espower-loader": "^1.0.1",
Expand Down
27 changes: 26 additions & 1 deletion test/egg-debug.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const path = require('path');
const coffee = require('coffee');
const mm = require('mm');
const rimraf = require('rimraf');
const net = require('net');

describe('egg-bin debug', () => {
const eggBin = require.resolve('../bin/egg-bin.js');
Expand All @@ -21,6 +22,7 @@ describe('egg-bin debug', () => {
})
.debug()
.expect('stdout', /,"workers":1}/)
.expect('stderr', /Debugger listening/)
.expect('code', 0)
.end(done);
});
Expand All @@ -29,9 +31,32 @@ describe('egg-bin debug', () => {
coffee.fork(eggBin, [ 'debug', '--port', '6001' ], {
cwd: appdir,
})
.debug()
// .debug()
.expect('stdout', `{"baseDir":"${appdir}","workers":1,"port":"6001"}\n`)
.expect('stderr', /Debugger listening/)
.expect('code', 0)
.end(done);
});

describe('auto detect available port', () => {
let server;
before(done => {
server = net.createServer();
server.listen(7001, done);
});

after(() => server.close());

it('should auto detect available port', done => {
coffee.fork(eggBin, [ 'debug' ], {
cwd: appdir,
})
// .debug()
.expect('stdout', /,"workers":1}/)
.expect('stderr', /\[egg-bin] server port 7001 is in use/)
.expect('stderr', /Debugger listening/)
.expect('code', 0)
.end(done);
});
});
});
22 changes: 22 additions & 0 deletions test/egg-dev.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const path = require('path');
const coffee = require('coffee');
const net = require('net');

describe('egg-bin dev', () => {
const eggBin = require.resolve('../bin/egg-bin.js');
Expand All @@ -27,6 +28,27 @@ describe('egg-bin dev', () => {
.end(done);
});

describe('auto detect available port', () => {
let server;
before(done => {
server = net.createServer();
server.listen(7001, done);
});

after(() => server.close());

it('should auto detect available port', done => {
coffee.fork(eggBin, [ 'dev' ], {
cwd: appdir,
})
.debug()
.expect('stdout', `{"baseDir":"${appdir}","workers":1}\n`)
.expect('stderr', /\[egg-bin] server port 7001 is in use/)
.expect('code', 0)
.end(done);
});
});

it.skip('should startCluster with execArgv --debug', done => {
coffee.fork(eggBin, [ 'dev', '--debug=7000' ], {
cwd: appdir,
Expand Down

0 comments on commit 22e26bf

Please sign in to comment.