Permalink
Browse files

feat(devServer): support autoport in env (#26)

<!--
Thank you for your pull request. Please review below requirements.
Bug fixes and new features should include tests and possibly benchmarks.
Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md

感谢您贡献代码。请确认下列 checklist 的完成情况。
Bug 修复和新功能必须包含测试,必要时请附上性能测试。
Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md
-->

##### Checklist
<!-- Remove items that do not apply. For completed items, change [ ] to [x]. -->

- [ ] `npm test` passes
- [ ] tests and/or benchmarks are included
- [ ] documentation is changed or added
- [ ] commit message follows commit guidelines

##### Affected core subsystem(s)
<!-- Provide affected core subsystem(s). -->


##### Description of change
<!-- Provide a description of the change below this comment. -->
  • Loading branch information...
popomore committed Jan 7, 2019
1 parent db2b3a5 commit f6e9fb7b51435516ce9d7f20a8de2e7a62b87c61
@@ -10,6 +10,7 @@ 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');
const is = require('is-type-of');


class DevServer extends Base {
@@ -42,11 +43,15 @@ class DevServer extends Base {

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

const env = Object.assign({}, process.env, devServer.env);
env.PATH = `${this.app.config.baseDir}/node_modules/.bin:${env.PATH}`;
// replace {port}
Object.keys(env).forEach(key => {
env[key] = this.replacePort(env[key]);
});
const opt = {
// disable stdout by default
stdio: [ 'inherit', 'ignore', 'inherit' ],
@@ -121,6 +126,12 @@ class DevServer extends Base {

logger.error(codeOrError);
}

replacePort(str) {
if (!is.string(str)) return str;
return str.replace('{port}', this.app.config.assets.devServer.port);
}

}

module.exports = DevServer;
@@ -19,6 +19,7 @@
"cross-spawn": "^6.0.5",
"debug": "^4.1.1",
"detect-port": "^1.3.0",
"is-type-of": "^1.2.1",
"mz": "^2.7.0",
"mz-modules": "^2.1.0",
"sdk-base": "^3.5.1"
@@ -166,7 +166,7 @@ describe('test/dev_server.test.js', () => {
const app1 = mock.cluster({
baseDir: 'apps/autoport',
});
app1.debug();
// app1.debug();
await app1.ready();

await app1.httpRequest()
@@ -175,11 +175,12 @@ describe('test/dev_server.test.js', () => {
.expect(200);

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

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

@@ -14,7 +14,9 @@ exports.assets = {
waitStart: true,
autoPort: true,
command: 'node ' + path.join(__dirname, '../../mocktool/server.js') + ' {port}',
env: {},
env: {
SOCKET_SERVER: 'http://127.0.0.1:{port}',
},
debug: true,
},
};
@@ -5,6 +5,8 @@ const Koa = require('koa');

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

console.info('[server] SOCKET_SERVER: ' + process.env.SOCKET_SERVER);

const app = new Koa();
app.use(async ctx => {
ctx.body = 'done';

0 comments on commit f6e9fb7

Please sign in to comment.