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 3b2305e
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 33 deletions.
1 change: 1 addition & 0 deletions lib/cov_command.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class CovCommand extends Command {
'--',
'--timeout', process.env.TEST_TIMEOUT || '200000',
'--require', require.resolve('thunk-mocha'),
'--require', require.resolve('power-assert'),
]).concat(this.helper.getTestFiles()).concat(args);

if (args.indexOf('intelli-espower-loader') !== -1) {
Expand Down
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 lib/test_command.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class TestCommand extends Command {
'--reporter', process.env.TEST_REPORTER || 'spec',
'--timeout', process.env.TEST_TIMEOUT || '30000',
'--require', require.resolve('thunk-mocha'),
'--require', require.resolve('power-assert'),
].concat(this.helper.getTestFiles()).concat(args);

if (args.indexOf('intelli-espower-loader') !== -1) {
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
"childprocess": "^2.0.2",
"commander": "^2.9.0",
"common-bin": "^1.0.0",
"debug": "^2.3.3",
"debug": "^2.4.4",
"detect-port": "^1.0.7",
"egg-utils": "^1.0.0",
"glob": "^7.1.1",
"intelli-espower-loader": "^1.0.1",
"istanbul": "^0.4.5",
"mkdirp": "^0.5.1",
"mocha": "^3.2.0",
"power-assert": "^1.4.2",
"rimraf": "^2.5.4",
"semver": "^5.3.0",
"thunk-mocha": "^1.0.8"
Expand All @@ -27,7 +27,7 @@
"autod": "^2.7.1",
"coffee": "^3.3.0",
"egg-ci": "^1.1.0",
"eslint": "^3.12.1",
"eslint": "^3.12.2",
"eslint-config-egg": "^3.2.0",
"mm": "^2.0.0"
},
Expand Down
26 changes: 24 additions & 2 deletions 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 @@ -19,7 +20,7 @@ describe('egg-bin debug', () => {
coffee.fork(eggBin, [ 'debug' ], {
cwd: appdir,
})
.debug()
// .debug()
.expect('stdout', /,"workers":1}/)
.expect('code', 0)
.end(done);
Expand All @@ -29,9 +30,30 @@ describe('egg-bin debug', () => {
coffee.fork(eggBin, [ 'debug', '--port', '6001' ], {
cwd: appdir,
})
.debug()
// .debug()
.expect('stdout', `{"baseDir":"${appdir}","workers":1,"port":"6001"}\n`)
.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('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 3b2305e

Please sign in to comment.