Skip to content

Instantly share code, notes, and snippets.

@shripadk shripadk/watch_jade.js
Last active Dec 17, 2015

Embed
What would you like to do?
Script that watches Jade files and compiles all of them to a single javascript file.
#!/usr/bin/env node
/**
* dependencies:
* clientjade (sudo npm install -g clientjade)
* chokidar (sudo npm install chokidar)
* how to run:
* node watch_jade.js
* to render template, just call:
* jade.render(domNode, templateName, data);
*/
var fs = require('fs');
var chokidar = require('chokidar');
var exec = require('child_process').exec;
// input directory for all your client-side jade templates
var input = __dirname + '/templates';
// output all compiled jade templates to single javascript file
var output = __dirname + '/public/tpl/compiled.js';
console.log('Cleaning compiled.js');
exec('cat /dev/null > ' + output);
console.log('\nStarting watcher...');
var watcher = chokidar.watch(input, {persistent: true});
var paths = {};
var compile_files = function(path, msg) {
var paths_str = Object.keys(paths).join(' ');
if(!paths_str.length) return;
process.nextTick(function() {
exec('clientjade -c ' + paths_str + ' > ' + output);
console.log(msg + path);
});
};
watcher.on('add', function(path) {
if(path.search('.jade') !== -1) {
paths[path] = 1;
compile_files(path, 'File added: ');
}
}).on('change', function(path) {
if(path.search('.jade') !== -1) {
paths[path] = 1;
compile_files(path, 'File change: ');
}
}).on('unlink', function(path) {
if(path.search('.jade') !== -1) {
delete paths[path];
compile_files(path, 'File removed: ');
}
});
console.log('Started!');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.