Skip to content

Instantly share code, notes, and snippets.

@chrisblossom
Last active July 27, 2016 22:43
Show Gist options
  • Save chrisblossom/c67268ba865d7dd00b0a8b38dc557a43 to your computer and use it in GitHub Desktop.
Save chrisblossom/c67268ba865d7dd00b0a8b38dc557a43 to your computer and use it in GitHub Desktop.
run_server.js
// Largely borrowed from https://github.com/kriasoft/react-starter-kit/blob/master/tools/runServer.js
import { spawn } from 'child_process';
import path from 'path';
class Server {
constructor(script, options = { logger: console }) {
this.script = script;
this.logger = options.logger;
}
restart() {
if (this.node) {
this.logger.info(`${path.relative(process.cwd(), this.script)} restarting...`);
this.node.kill();
} else {
this.logger.info(`${path.relative(process.cwd(), this.script)} starting...`);
}
this.node = spawn('node', [this.script], { stdio: 'inherit' });
this.node.on('close', code => {
if (code === 8) {
this.logger.info('Error detected, waiting for changes...');
}
});
process.on('exit', () => {
if (this.node) {
this.node.kill();
}
process.exit();
});
}
}
export default Server;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment