Skip to content

Commit

Permalink
feat: auto detect available port (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 authored and fengmk2 committed Dec 16, 2016
1 parent e33f0a2 commit 6d7ad41
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 59 deletions.
1 change: 1 addition & 0 deletions .autod.conf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = {
'mocha',
'thunk-mocha',
'intelli-espower-loader',
'power-assert',
],
devdep: [
'autod',
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;
};

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"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",
Expand All @@ -27,7 +28,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
12 changes: 3 additions & 9 deletions test/egg-cov.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ describe('egg-bin cov', () => {

it('should fail when test fail', done => {
mm(process.env, 'TESTS', 'test/fail.js');
coffee.fork(eggBin, [ 'cov' ], {
cwd: appdir,
})
coffee.fork(eggBin, [ 'cov' ], { cwd: appdir })
.coverage(false)
// .debug()
.expect('stdout', /1\) should fail/)
Expand All @@ -76,9 +74,7 @@ describe('egg-bin cov', () => {

it('should fail when test fail with power-assert', done => {
mm(process.env, 'TESTS', 'test/power-assert-fail.js');
coffee.fork(eggBin, [ 'cov' ], {
cwd: appdir,
})
coffee.fork(eggBin, [ 'cov' ], { cwd: appdir })
.coverage(false)
// .debug()
.expect('stdout', /1\) should fail/)
Expand All @@ -90,9 +86,7 @@ describe('egg-bin cov', () => {

it('should warn when require intelli-espower-loader', done => {
mm(process.env, 'TESTS', 'test/power-assert-fail.js');
coffee.fork(eggBin, [ 'cov', '-r', 'intelli-espower-loader' ], {
cwd: appdir,
})
coffee.fork(eggBin, [ 'cov', '-r', 'intelli-espower-loader' ], { cwd: appdir })
.coverage(false)
// .debug()
.expect('stderr', /manually require `intelli-espower-loader`/)
Expand Down
32 changes: 24 additions & 8 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 @@ -16,22 +17,37 @@ describe('egg-bin debug', () => {
afterEach(mm.restore);

it('should startCluster success', done => {
coffee.fork(eggBin, [ 'debug' ], {
cwd: appdir,
})
.debug()
coffee.fork(eggBin, [ 'debug' ], { cwd: appdir })
// .debug()
.expect('stdout', /,"workers":1}/)
.expect('code', 0)
.end(done);
});

it('should startCluster with port', done => {
coffee.fork(eggBin, [ 'debug', '--port', '6001' ], {
cwd: appdir,
})
.debug()
coffee.fork(eggBin, [ 'debug', '--port', '6001' ], { cwd: appdir })
// .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);
});
});
});
36 changes: 24 additions & 12 deletions test/egg-dev.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,49 @@

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

describe('egg-bin dev', () => {
const eggBin = require.resolve('../bin/egg-bin.js');
const appdir = path.join(__dirname, 'fixtures/demo-app');

it('should startCluster success', done => {
coffee.fork(eggBin, [ 'dev' ], {
cwd: appdir,
})
coffee.fork(eggBin, [ 'dev' ], { cwd: appdir })
// .debug()
.expect('stdout', `{"baseDir":"${appdir}","workers":1}\n`)
.expect('code', 0)
.end(done);
});

it('should startCluster with port', done => {
coffee.fork(eggBin, [ 'dev', '--port', '6001' ], {
cwd: appdir,
})
coffee.fork(eggBin, [ 'dev', '--port', '6001' ], { cwd: appdir })
// .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, [ '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,
})
coffee.fork(eggBin, [ 'dev', '--debug=7000' ], { cwd: appdir })
// .debug()
.expect('stdout', `{"baseDir":"${appdir}","workers":1}\n`)
.expect('stderr', /Debugger listening on .*7000/)
Expand All @@ -39,9 +53,7 @@ describe('egg-bin dev', () => {
});

it.skip('should startCluster with execArgv --inspect', done => {
coffee.fork(eggBin, [ 'dev', '--inspect=7000' ], {
cwd: appdir,
})
coffee.fork(eggBin, [ 'dev', '--inspect=7000' ], { cwd: appdir })
// .debug()
.expect('stdout', `{"baseDir":"${appdir}","workers":1}\n`)
.expect('stderr', /Debugger listening on .*7000/)
Expand Down

0 comments on commit 6d7ad41

Please sign in to comment.