Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix: port should be paased from agent to app (#25)
  • Loading branch information
popomore committed Jan 7, 2019
1 parent 403dc95 commit 9378984
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 5 deletions.
10 changes: 6 additions & 4 deletions agent.js
Expand Up @@ -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();
});
Expand Down
12 changes: 11 additions & 1 deletion app.js
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions config/config.default.js
@@ -1,5 +1,7 @@
'use strict';

const path = require('path');

module.exports = appInfo => ({
/**
* assets options
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions lib/dev_server.js
@@ -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 {
Expand All @@ -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()) {
Expand Down
5 changes: 5 additions & 0 deletions test/dev_server.test.js
Expand Up @@ -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({
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/apps/autoport/config/config.default.js
Expand Up @@ -3,6 +3,12 @@
const path = require('path');

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

0 comments on commit 9378984

Please sign in to comment.