Created
July 31, 2014 21:34
-
-
Save asbjornenge/ada0faecaa660de14f8b to your computer and use it in GitHub Desktop.
"Livereload" for node-webkit
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
var reloadSocket = new WebSocket('ws://localhost:35730'); | |
reloadSocket.onmessage = function(msg) { | |
if (msg.data === 'reload') setTimeout(function() { location.reload(); },10) | |
} |
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
var fs = require('fs') | |
var gaze = require('gaze') | |
var path = require('path') | |
var ws = require('ws') | |
var browserify = require('browserify') | |
var stylus = require('accord').load('stylus') | |
var isopen = function(cons) { | |
return cons.filter(function(ws) { return ws.readyState == ws.OPEN }) | |
} | |
var sendVerb = function(verb, cons) { | |
return cons.forEach(function(ws) { ws.send(verb) }) | |
} | |
/** Websocket **/ | |
var wss = new ws.Server({port: 35730}); | |
var cons = [] | |
wss.on('connection', function(ws) { | |
cons.push(ws) | |
}); | |
/** Gaze **/ | |
gaze(['./styles/**/*.styl'], function(err, watcher) { | |
watcher.on('all', function(event, filepath) { | |
console.log('started', filepath) | |
stylus.renderFile('./styles/entry.styl') | |
.catch(function(err) { | |
console.log(err) | |
}) | |
.done(function(css) { | |
var wstream = fs.createWriteStream('./app.css') | |
wstream.on('finish', function() { | |
console.log('done') | |
sendVerb('reload', isopen(cons)) | |
}) | |
wstream.write(css) | |
wstream.end() | |
}) | |
}) | |
}) | |
gaze(['./modules/**/*.js','./public/index.html'], function(err, watcher) { | |
watcher.on('all', function(event, filepath) { | |
console.log('started', filepath) | |
var b = browserify(['./modules/entry.js'], {}) | |
var wstream = fs.createWriteStream('./app.js') | |
wstream.on('finish', function() { | |
console.log('done') | |
sendVerb('reload', isopen(cons)) | |
}) | |
b.bundle().pipe(wstream) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment