Permalink
Browse files

feat: auto check port with autoPort (#24)

  • Loading branch information...
popomore committed Jan 5, 2019
1 parent bf05a99 commit 59d20a0e7f1451aee14cb1a5117e8cda2d999498
@@ -9,7 +9,7 @@ function startDevServer(agent) {
if (!agent.config.assets.isLocalOrUnittest) return;
if (!agent.config.assets.devServer.enable) return;

assert(agent.config.assets.devServer.port, 'devServer.port is required when devServer is enabled');
assert(agent.config.assets.devServer.autoPort || agent.config.assets.devServer.port, 'port or autoPort is required when devServer is enabled');

const server = new DevServer(agent);
server.ready(err => {
@@ -27,6 +27,7 @@ module.exports = appInfo => ({
devServer: {
enable: true,
command: '',
autoPort: false,
port: null,
env: {},
debug: false,
@@ -6,6 +6,7 @@ const detect = require('detect-port');
const sleep = require('mz-modules/sleep');
const awaitEvent = require('await-event');
const debug = require('debug')('egg-view-assets:dev_server');
const detectPort = require('detect-port');


class DevServer extends Base {
@@ -18,9 +19,15 @@ class DevServer extends Base {
}

async init() {
// check whether the port is using
if (await this.checkPortExist()) {
throw new Error(`port ${this.app.config.assets.devServer.port} has been used`);
const { devServer } = this.app.config.assets;

if (devServer.autoPort) {
devServer.port = await detectPort(10000);
} else {
// check whether the port is using
if (await this.checkPortExist()) {
throw new Error(`port ${this.app.config.assets.devServer.port} has been used`);
}
}

// start dev server asynchronously
@@ -30,6 +37,7 @@ class DevServer extends Base {

startAsync() {
const { devServer } = this.app.config.assets;
devServer.command = devServer.command.replace('{port}', devServer.port);
const [ command, ...args ] = devServer.command.split(/\s+/);

const env = Object.assign({}, process.env, devServer.env);
@@ -98,7 +106,7 @@ class DevServer extends Base {
const { devServer } = this.app.config.assets;
const code = codeOrError;
const message = `[egg-view-assets] Run "${devServer.command}" exit with code ${code}`;
if (code === 0) {
if (!code || code === 0) {
logger.info(message);
return;
}
@@ -17,26 +17,25 @@
"dependencies": {
"await-event": "^2.1.0",
"cross-spawn": "^6.0.5",
"debug": "^3.1.0",
"detect-port": "^1.2.3",
"debug": "^4.1.1",
"detect-port": "^1.3.0",
"mz": "^2.7.0",
"mz-modules": "^2.1.0",
"sdk-base": "^3.5.0",
"utility": "^1.14.0"
"sdk-base": "^3.5.1"
},
"devDependencies": {
"autod": "^3.0.1",
"autod-egg": "^1.1.0",
"egg": "^2.10.0",
"egg-bin": "^4.8.1",
"egg-ci": "^1.8.0",
"egg-mock": "^3.19.2",
"egg": "^2.14.2",
"egg-bin": "^4.10.0",
"egg-ci": "^1.11.0",
"egg-mock": "^3.21.0",
"egg-view-ejs": "^2.0.0",
"egg-view-nunjucks": "^2.1.6",
"eslint": "^5.3.0",
"eslint-config-egg": "^7.0.0",
"puppeteer": "^1.7.0",
"supertest": "^3.1.0",
"eslint": "^5.12.0",
"eslint-config-egg": "^7.1.0",
"puppeteer": "^1.11.0",
"supertest": "^3.3.0",
"uglify-js": "^3.3.21",
"webstorm-disable-index": "^1.2.0"
},
@@ -145,7 +145,7 @@ describe('test/dev_server.test.js', () => {
await app.ready();

app.expect('code', 1);
app.expect('stderr', /devServer.port is required when devServer is enabled/);
app.expect('stderr', /port or autoPort is required when devServer is enabled/);
});

it('should not check port when devServer is disabled', async () => {
@@ -160,4 +160,27 @@ describe('test/dev_server.test.js', () => {
app.expect('code', 0);
app.expect('stdout', /egg started/);
});

it('should auto check port with autoPort', async () => {
mock.env('local');
const app1 = mock.cluster({
baseDir: 'apps/autoport',
});
app1.debug();
await app1.ready();

app1.expect('stdout', /\[server] listening 10000/);

app = mock.cluster({
baseDir: 'apps/autoport',
});
app.debug();
try {
await app.ready();

app.expect('stdout', /\[server] listening 10001/);
} finally {
await app1.close();
}
});
});
@@ -0,0 +1,11 @@
'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
async index() {
await this.ctx.render('index.js');
}
}

module.exports = HomeController;
@@ -0,0 +1,7 @@
'use strict';

module.exports = app => {
const { router, controller } = app;

router.get('/', controller.home.index);
};
@@ -0,0 +1,14 @@
'use strict';

const path = require('path');

exports.keys = '123456';
exports.assets = {
devServer: {
waitStart: true,
autoPort: true,
command: 'node ' + path.join(__dirname, '../../mocktool/server.js') + ' {port}',
env: {},
debug: true,
},
};
@@ -0,0 +1,3 @@
{
"name": "egg-view-assets"
}
@@ -3,6 +3,8 @@
const http = require('http');
const Koa = require('koa');

const port = Number(process.argv[2]) || 8000;

const app = new Koa();
app.use(async ctx => {
ctx.body = 'done';
@@ -14,11 +16,11 @@ server.once('error', err => {
process.exit(1);
});
server.once('listening', () => {
console.info('[server] listening 8000');
console.info('[server] listening ' + port);
});

console.info('[server] listen 8000');
server.listen(8000);
console.info('[server] listen ' + port);
server.listen(port);

process.once('SIGTERM', () => {
console.log('[server] server stopped');

0 comments on commit 59d20a0

Please sign in to comment.