Created
April 21, 2016 20:30
-
-
Save zwhitchcox/5fcb45c5483b95f47949bdeaf7b75c64 to your computer and use it in GitHub Desktop.
Start a server and restart it any time there is a change in a file
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
'use strict' | |
const path = require('path') | |
const cp = require('child_process') | |
const gaze = require('gaze') | |
// edit to match the path of your server | |
const serverPath = __dirname+'/index' | |
let server; | |
let serverScript = 'require(\''+serverPath+'\')' | |
function runServer(cb) { | |
if (server) { | |
server.kill('SIGTERM') | |
} | |
server = cp.spawn('node', ['--eval', serverScript], { | |
env: Object.assign({ NODE_ENV: 'dev' }, process.env), | |
stdio: 'inherit', | |
silent: false, | |
}) | |
} | |
process.on('exit', () => { | |
if (server) { | |
server.kill('SIGTERM') | |
} | |
}) | |
runServer() | |
gaze(['**','!node_modules/**'], function(err, watcher) { | |
this.on('all', function(event, filepath) { | |
runServer() | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment