Permalink
Browse files

test: set proc null when exit by self (#23)

  • Loading branch information...
popomore committed Jan 5, 2019
1 parent 658c2f8 commit d233b74cc38648a310146802385031d001d971e9
Showing with 23 additions and 17 deletions.
  1. +22 −16 lib/dev_server.js
  2. +1 −1 test/dev_server.test.js
@@ -29,7 +29,6 @@ class DevServer extends Base {
}

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

@@ -41,7 +40,6 @@ class DevServer extends Base {
};
if (devServer.cwd) opt.cwd = devServer.cwd;
const proc = this.proc = spawn(command, args, opt);

let stderr = '';
proc.stderr.on('data', data => {
stderr += data;
@@ -51,20 +49,8 @@ class DevServer extends Base {
if (devServer.debug) process.stdout.write(data);
});

proc.on('error', err => {
logger.error(err);
});

proc.once('exit', code => {
const message = `[egg-view-assets] Run "${devServer.command}" exit with code ${code}`;
if (code > 0) {
const err = new Error(message);
err.stderr = stderr;
logger.error(err);
return;
}
logger.info(message);
});
proc.once('error', err => this.exit(err, stderr));
proc.once('exit', code => this.exit(code, stderr));
}

async checkPortExist() {
@@ -110,6 +96,26 @@ class DevServer extends Base {
await awaitEvent(this.proc, 'exit');
this.proc = null;
}

exit(codeOrError, stderr) {
const logger = this.app.coreLogger;
this.proc = null;

if (!(codeOrError instanceof Error)) {
const { devServer } = this.app.config.assets;
const code = codeOrError;
const message = `[egg-view-assets] Run "${devServer.command}" exit with code ${code}`;
if (code === 0) {
logger.info(message);
return;
}

codeOrError = new Error(message);
}

codeOrError.stderr = stderr;
logger.error(codeOrError);
}
}

module.exports = DevServer;
@@ -55,7 +55,7 @@ describe('test/dev_server.test.js', () => {
await app.close();
await sleep(5000);

app.expect('stdout', /Closing, but devServer is not listened/);
// app.expect('stdout', /Closing, but devServer is not listened/);
});

it('should custom devServer.cwd', async () => {

0 comments on commit d233b74

Please sign in to comment.