Skip to content

Instantly share code, notes, and snippets.

@chaosprinz
Last active April 19, 2016 19:10
Show Gist options
  • Save chaosprinz/36cbfecd2bed0a8975c5b835dfdb3edc to your computer and use it in GitHub Desktop.
Save chaosprinz/36cbfecd2bed0a8975c5b835dfdb3edc to your computer and use it in GitHub Desktop.
Watching for changes on source-files and rebuild them. Say no to grunt and co.
'use strict';
/*
# Build-environment filewatcher-script
*/
const chokidar = require('chokidar');
const child_process = require('child_process');
const Path = require('path')
const config = require('./watch.conf');
/*
## JadeWatcher
Watching for files in the template-source-directory.
These files are going to be rendered in the front-end, so they have to be
precompiled to a function.
Look at env/client_jade.js for details about this process.
*/
const jadeWatcher = chokidar.watch(config.jade.in, { persistent: true });
var compileJade = function(action, path){
console.log(`Jade: ${action} on ${path}`);
let cmd = `node env/client_jade ${config.jade.in} ${config.jade.out}`
child_process.exec(cmd,function(err, stdout, stderr){
if(err) {
console.error("JadeWatcher error:");
console.error(err);
}
if(stderr) {
console.error("Jade compile-error:");
console.error(stderr);
}
console.log("Jade compiler-output:");
console.log(stdout);
});
}
jadeWatcher
.on('all', compileJade)
/*
## StylusWatcher
Watching for files in the stylesheets-source-directory and recompile the
index.styl-file on changes.
*/
const stylusWatcher = chokidar.watch(config.stylus.in,{ persistent: true });
var compileStylus = function(action, path){
console.log(`Stylus: ${action} on ${path}`);
let cmd = "node node_modules\\stylus\\bin\\stylus"
cmd += ` source\\stylesheets\\index.styl ${config.stylus.in}\\`;
cmd += config.stylus.main;
config.stylus.libs.forEach(function(lib){
cmd += ` -u ${lib}`;
});
cmd += ` --out ${config.stylus.out}`;
child_process.exec(cmd, function(err, stdout, stderr){
if(err) {
console.error("StylusWatcher error:");
console.error(err);
}
if(stderr) {
console.error("Stylus compile-error:");
console.error(stderr);
}
console.log("Stylus compiler-output");
console.log(stdout)
});
}
stylusWatcher.on('all', compileStylus);
/*
## BrowserifyWatcher
Watch for changes on client-javascript in their source dir and and let
browserify rebuild them.
*/
const browserifyWatcher = chokidar.watch(config.browserify.in, {
persistent: true
});
var rebuildJavascripts = function(action,path){
console.log(`Browserify: ${action} on ${path}`);
let cmd = `node node_modules\\browserify\\bin\\cmd `;
cmd += `${config.browserify.in}\\main.js -o `;
cmd += `${config.browserify.out}\\main.js`;
child_process.exec(cmd, function(err, stdout, stderr){
if(err){
console.error("BrowserifyWatcher error:");
console.error(err);
}
if(stderr){
console.error("Browserify compile-error:");
console.error(stderr);
}
console.log("Rebuilt client-javascript");
})
}
browserifyWatcher.on('all', rebuildJavascripts);
{
"jade": {
"in": "source/templates",
"out": "source/javascripts/template.js"
},
"stylus": {
"in": "source/stylesheets",
"out": "public/css",
"main": "index.styl",
"libs": ["nib","jeet","rupture"]
},
"browserify": {
"in": "source/javascripts",
"out": "public/js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment