Permalink
Browse files

fix: port should be paased from agent to app (#25)

  • Loading branch information...
popomore committed Jan 7, 2019
1 parent 403dc95 commit 9378984ad71465eb9cc46beba4936e8bb95f97f6
Showing with 36 additions and 5 deletions.
  1. +6 −4 agent.js
  2. +11 −1 app.js
  3. +3 −0 config/config.default.js
  4. +5 −0 lib/dev_server.js
  5. +5 −0 test/dev_server.test.js
  6. +6 −0 test/fixtures/apps/autoport/config/config.default.js
@@ -6,17 +6,19 @@ const DevServer = require('./lib/dev_server');
module.exports = agent => startDevServer(agent);

function startDevServer(agent) {
if (!agent.config.assets.isLocalOrUnittest) return;
if (!agent.config.assets.devServer.enable) return;
const assetsConfig = agent.config.assets;

assert(agent.config.assets.devServer.autoPort || agent.config.assets.devServer.port, 'port or autoPort is required when devServer is enabled');
if (!assetsConfig.isLocalOrUnittest) return;
if (!assetsConfig.devServer.enable) return;

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

const server = new DevServer(agent);
server.ready(err => {
if (err) agent.coreLogger.error('[egg-view-assets]', err.message);
});

if (agent.config.assets.devServer.waitStart) {
if (assetsConfig.devServer.waitStart) {
agent.beforeStart(async () => {
await server.ready();
});
12 app.js
@@ -9,7 +9,17 @@ module.exports = app => {
const assetsConfig = app.config.assets;

if (assetsConfig.devServer.enable && assetsConfig.isLocalOrUnittest) {
assetsConfig.url = 'http://127.0.0.1:' + assetsConfig.devServer.port;
let port = assetsConfig.devServer.port;
if (assetsConfig.devServer.autoPort === true) {
try {
port = fs.readFileSync(assetsConfig.devServer.portPath, 'utf8');
console.log(port);
} catch (err) {
// istanbul ignore next
throw new Error('check autoPort fail');
}
}
assetsConfig.url = 'http://127.0.0.1:' + port;
}

// it should check manifest.json on deployment
@@ -1,5 +1,7 @@
'use strict';

const path = require('path');

module.exports = appInfo => ({
/**
* assets options
@@ -29,6 +31,7 @@ module.exports = appInfo => ({
command: '',
autoPort: false,
port: null,
portPath: path.join(appInfo.baseDir, 'run/assetsPort'),
env: {},
debug: false,
timeout: 60 * 1000,
@@ -1,12 +1,15 @@
'use strict';

const path = require('path');
const fs = require('mz/fs');
const spawn = require('cross-spawn');
const Base = require('sdk-base');
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');
const mkdirp = require('mz-modules/mkdirp');


class DevServer extends Base {
@@ -23,6 +26,8 @@ class DevServer extends Base {

if (devServer.autoPort) {
devServer.port = await detectPort(10000);
await mkdirp(path.dirname(devServer.portPath));
await fs.writeFile(devServer.portPath, devServer.port);
} else {
// check whether the port is using
if (await this.checkPortExist()) {
@@ -169,6 +169,11 @@ describe('test/dev_server.test.js', () => {
app1.debug();
await app1.ready();

await app1.httpRequest()
.get('/')
.expect(/http:\/\/127.0.0.1:\d+\/index.js/)
.expect(200);

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

app = mock.cluster({
@@ -3,6 +3,12 @@
const path = require('path');

exports.keys = '123456';
exports.view = {
mapping: {
'.js': 'assets',
'.jsx': 'assets',
},
};
exports.assets = {
devServer: {
waitStart: true,

0 comments on commit 9378984

Please sign in to comment.