ff-stream-web

git clone git://archive.git.mtrnord.blog/MTRNord/ff-stream-web.git
Log | Files | Refs | README | LICENSE

commit 233feaef99f7868746c91e47ec0faca07c750c1f
parent a4120caab47ddf5d11ce6aa42d237566ecdfebe9
Author: Konstantin Tarkus <hello@tarkus.me>
Date:   Wed, 11 Feb 2015 10:10:34 +0300

Remove `nodemon` in favor of a custom dev server startup script

Fixes issue with website not reloading when content is changed.

Closes #68, #69

Diffstat:
Mgulpfile.js | 83+++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------
Mpackage.json | 1-
Msrc/server.js | 8+++++---
3 files changed, 59 insertions(+), 33 deletions(-)

diff --git a/gulpfile.js b/gulpfile.js @@ -37,6 +37,7 @@ var AUTOPREFIXER_BROWSERS = [ // https://github.com/ai/autoprefi var src = {}; var watch = false; +var browserSync; // The default task gulp.task('default', ['sync']); @@ -113,54 +114,78 @@ gulp.task('build', ['clean'], function(cb) { runSequence(['vendor', 'assets', 'styles', 'bundle'], cb); }); -// Launch a lightweight HTTP Server -gulp.task('serve', function(cb) { - var nodemon = require('nodemon'); - +// Build and start watching for modifications +gulp.task('build:watch', function(cb) { watch = true; - runSequence('build', function() { - var server = require('nodemon')({ - script: 'build/server.js', - watch: [path.join(__dirname, 'build/server.js')], - env: {NODE_ENV: 'development'} - }).on('log', function(log) { - $.util.log('nodemon', $.util.colors.green(log.message)); - }).on('crash', function() { - $.util.log($.util.colors.red('nodemon crashed')); - }).once('start', function() { - process.on('exit', function () { - server.emit('exit'); - }); - - gulp.watch(src.assets, ['assets']); - gulp.watch(src.styles, ['styles']); - cb(); + gulp.watch(src.assets, ['assets']); + gulp.watch(src.styles, ['styles']); + cb(); + }); +}); + +// Launch a Node.js/Express server +gulp.task('serve', ['build:watch'], function(cb) { + src.server = [ + DEST + '/server.js', + DEST + '/content/**/*', + DEST + '/templates/**/*' + ]; + + var started = false; + var cp = require('child_process'); + var assign = require('react/lib/Object.assign'); + + var server = (function startup() { + var child = cp.fork(DEST + '/server.js', { + env: assign({ NODE_ENV: 'development' }, process.env) + }); + child.once('message', function(message) { + if (message.match(/^online$/)) { + if (browserSync) { + browserSync.reload(); + } + if (!started) { + started = true; + gulp.watch(src.server, function (file) { + $.util.log('Restarting development server.'); + server.kill('SIGTERM'); + server = startup(); + }); + cb(); + } + } }); + return child; + })(); + + process.on('exit', function() { + server.kill('SIGTERM'); }); }); -gulp.task('sync', ['serve'], function() { - var browserSync = require('browser-sync'); +// Launch BrowserSync development server +gulp.task('sync', ['serve'], function(cb) { + browserSync = require('browser-sync'); browserSync({ notify: false, - // Customize the BrowserSync console logging prefix - logPrefix: 'QC', // Run as an https by setting 'https: true' // Note: this uses an unsigned certificate which on first access // will present a certificate warning in the browser. https: false, // Informs browser-sync to proxy our Express app which would run // at the following location - proxy: 'http://localhost:5000' - }); + proxy: 'localhost:5000' + }, cb); - process.on('exit', function () { + process.on('exit', function() { browserSync.exit(); }); - gulp.watch(DEST + '/**/*.*', function (file) { + gulp.watch([DEST + '/**/*.*'].concat( + src.server.map(function(file) {return '!' + file;}) + ), function(file) { browserSync.reload(path.relative(__dirname, file.path)); }); }); diff --git a/package.json b/package.json @@ -56,7 +56,6 @@ "less": "^2.4.0", "less-loader": "^2.0.0", "minimist": "^1.1.0", - "nodemon": "^1.3.6", "protractor": "^1.6.1", "psi": "^1.0.5", "react-tools": "^0.12.2", diff --git a/src/server.js b/src/server.js @@ -90,7 +90,9 @@ server.get('*', function(req, res) { })(); server.listen(server.get('port'), function() { - console.log('The server is running at http://localhost:' + server.get('port')); + if (process.send) { + process.send('online'); + } else { + console.log('The server is running at http://localhost:' + server.get('port')); + } }); - -module.exports.server = server;