Skip to content

Commit

Permalink
fix: should support -p (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 authored and fengmk2 committed Jan 17, 2017
1 parent 042f42e commit df86893
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/node_modules/
node_modules/
coverage/
!test/fixtures/**/node_modules/
!test/fixtures/custom-framework-app/node_modules/
.tmp
3 changes: 3 additions & 0 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const fs = require('fs');
const path = require('path');
const glob = require('glob');
const detect = require('detect-port');
const debug = require('debug')('egg-bin');

exports.defaultPort = 7001;
exports.serverBin = path.join(__dirname, 'start-cluster');
Expand Down Expand Up @@ -73,11 +74,13 @@ exports.formatArgs = function* (cwd, args, options) {

// auto detect available port
if (args.indexOf('-p') === -1 && args.indexOf('--port') === -1) {
debug('detect available port');
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`);
}
debug(`use available port ${port}`);
}
return args;
};
2 changes: 1 addition & 1 deletion lib/start-cluster
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const commander = require('commander');
commander
.option('--eggPath [eggPath]')
.option('--baseDir [baseDir]')
.option('--port [port]')
.option('-p, --port [port]')
.option('--cluster [workers]')
.allowUnknownOption(true)
.parse(process.argv);
Expand Down
10 changes: 9 additions & 1 deletion test/egg-dev.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,22 @@ describe('egg-bin dev', () => {
.end(done);
});

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

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

it('should startCluster with custom yadan framework', done => {
const baseDir = path.join(__dirname, 'fixtures/custom-framework-app');
const customEgg = path.join(baseDir, 'node_modules', 'yadan');
Expand Down

0 comments on commit df86893

Please sign in to comment.