Last active
December 17, 2015 22:49
-
-
Save shripadk/5684385 to your computer and use it in GitHub Desktop.
Script that watches Jade files and compiles all of them to a single javascript file.
This file contains hidden or 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
#!/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