Created
August 7, 2018 04:22
-
-
Save alur222/487deacafb1b22184649311a5d4ec039 to your computer and use it in GitHub Desktop.
Sample gulp file for node js development with auto reload
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
const gulp = require('gulp'); | |
const spawn = require('child_process').spawn; | |
let node; | |
function server() { | |
if (node) { | |
node.kill(); | |
} | |
node = spawn('npm', ['run', 'runserver'], {stdio: 'inherit'}); | |
node.on('close', function (code) { | |
if (code === 8) { | |
gulp.log('Error detected, waiting for changes...'); | |
} | |
}); | |
} | |
gulp.task('server', function() { | |
server(); | |
}); | |
gulp.task('watch-server', function() { | |
}); | |
const watcher = gulp.watch(['./backend/**/*.js', './backend.js']); | |
watcher.on('change', function() { | |
console.log('server files modified') | |
server(); | |
}) | |
gulp.task('default', gulp.series('server', 'watch-server')); | |
// clean up if an error goes unhandled. | |
process.on('exit', function() { | |
if (node) { | |
node.kill(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment