Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Commit

Permalink
Fix restarting server in dev
Browse files Browse the repository at this point in the history
Wait for the server process to exit before starting a new one, so that
the server debugger port is free when the new process begins.
  • Loading branch information
jarrodldavis committed May 15, 2019
1 parent ab939b8 commit 6add251
Showing 1 changed file with 42 additions and 36 deletions.
78 changes: 42 additions & 36 deletions src/api/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,48 +267,54 @@ class Watcher extends EventEmitter {
});
};

const start_server = () => {
// we need to give the child process its own DevTools port,
// otherwise Node will try to use the parent's (and fail)
const debugArgRegex = /--inspect(?:-brk|-port)?|--debug-port/;
const execArgv = process.execArgv.slice();
if (execArgv.some((arg: string) => !!arg.match(debugArgRegex))) {
execArgv.push(`--inspect-port=${this.devtools_port}`);
}

this.proc = child_process.fork(`${dest}/server/server.js`, [], {
cwd: process.cwd(),
env: Object.assign({
PORT: this.port
}, process.env),
stdio: ['ipc'],
execArgv
});

this.proc.stdout.on('data', chunk => {
this.emit('stdout', chunk);
});

this.proc.stderr.on('data', chunk => {
this.emit('stderr', chunk);
});

this.proc.on('message', message => {
if (message.__sapper__ && message.event === 'basepath') {
this.emit('basepath', {
basepath: message.basepath
});
}
});

this.proc.on('exit', emitFatal);
};

if (this.proc) {
this.proc.removeListener('exit', emitFatal);
this.proc.kill();
this.proc.on('exit', restart);
this.proc.on('exit', () => {
start_server();
restart();
});
} else {
start_server();
restart();
}

// we need to give the child process its own DevTools port,
// otherwise Node will try to use the parent's (and fail)
const debugArgRegex = /--inspect(?:-brk|-port)?|--debug-port/;
const execArgv = process.execArgv.slice();
if (execArgv.some((arg: string) => !!arg.match(debugArgRegex))) {
execArgv.push(`--inspect-port=${this.devtools_port}`);
}

this.proc = child_process.fork(`${dest}/server/server.js`, [], {
cwd: process.cwd(),
env: Object.assign({
PORT: this.port
}, process.env),
stdio: ['ipc'],
execArgv
});

this.proc.stdout.on('data', chunk => {
this.emit('stdout', chunk);
});

this.proc.stderr.on('data', chunk => {
this.emit('stderr', chunk);
});

this.proc.on('message', message => {
if (message.__sapper__ && message.event === 'basepath') {
this.emit('basepath', {
basepath: message.basepath
});
}
});

this.proc.on('exit', emitFatal);
});
}
});
Expand Down

0 comments on commit 6add251

Please sign in to comment.