diff --git a/.autod.conf b/.autod.conf index d9af5a64..d87055e7 100644 --- a/.autod.conf +++ b/.autod.conf @@ -12,6 +12,7 @@ module.exports = { 'mocha', 'thunk-mocha', 'intelli-espower-loader', + 'power-assert', ], devdep: [ 'autod', diff --git a/lib/debug_command.js b/lib/debug_command.js index 98c9af35..7c7703cf 100644 --- a/lib/debug_command.js +++ b/lib/debug_command.js @@ -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), @@ -74,10 +66,6 @@ class DebugCommand extends Command { help() { return 'Debug mode start'; } - - getFrameworkOrEggPath(cwd) { - return this.utils.getFrameworkOrEggPath(cwd); - } } module.exports = DebugCommand; diff --git a/lib/dev_command.js b/lib/dev_command.js index 53865b27..342347b6 100644 --- a/lib/dev_command.js +++ b/lib/dev_command.js @@ -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, }; @@ -33,10 +24,6 @@ class DevCommand extends Command { help() { return 'local env start'; } - - getFrameworkOrEggPath(cwd) { - return this.utils.getFrameworkOrEggPath(cwd); - } } module.exports = DevCommand; diff --git a/lib/helper.js b/lib/helper.js index cc185658..67483796 100644 --- a/lib/helper.js +++ b/lib/helper.js @@ -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() { @@ -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; +}; + diff --git a/package.json b/package.json index b9beee7f..b139a3f4 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" }, diff --git a/test/egg-cov.test.js b/test/egg-cov.test.js index e987d8e5..4dc1cc3c 100644 --- a/test/egg-cov.test.js +++ b/test/egg-cov.test.js @@ -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/) @@ -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/) @@ -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`/) diff --git a/test/egg-debug.test.js b/test/egg-debug.test.js index e8fb3b8e..b59eb14a 100644 --- a/test/egg-debug.test.js +++ b/test/egg-debug.test.js @@ -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'); @@ -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); + }); + }); }); diff --git a/test/egg-dev.test.js b/test/egg-dev.test.js index 47173435..71e196f0 100644 --- a/test/egg-dev.test.js +++ b/test/egg-dev.test.js @@ -2,15 +2,14 @@ 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) @@ -18,19 +17,34 @@ describe('egg-bin dev', () => { }); 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/) @@ -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/)