-
-
Save chrisblossom/c67268ba865d7dd00b0a8b38dc557a43 to your computer and use it in GitHub Desktop.
run_server.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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