Skip to content

Instantly share code, notes, and snippets.

@abourget
Created March 30, 2012 04:00
Show Gist options
  • Save abourget/2246368 to your computer and use it in GitHub Desktop.
Save abourget/2246368 to your computer and use it in GitHub Desktop.
A quick and dirty dust.js command line compiler (written in NodeJS, for use elsewhere)
// you need to take out this line for it to work with node 0.6+:
// comment out this line in the dust/lib/server.js file after "npm install dust":
require.paths.unshift(path.join(__dirname, '..'));
// otherwise, you'll get:
/*
node test/server.js
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: require.paths is removed. Use node_modules folders, or the NODE_PATH environment variable instead.
at Function.<anonymous> (module.js:376:11)
at Object.<anonymous> (/home/abourget/iron/Yellow/node_modules/dust/lib/server.js:6:8)
at Module._compile (module.js:432:26)
at Object..js (module.js:450:10)
at Module.load (module.js:351:31)
at Function._load (module.js:310:12)
at Module.require (module.js:357:17)
at require (module.js:368:17)
at Object.<anonymous> (/home/abourget/iron/Yellow/node_modules/dust/lib/dust.js:511:7)
at Module._compile (module.js:432:26)
*/
/**
* call like this:
*
* node dustcompiler.js mypath/myfilename.dust output1.js
*
* Do that with all your .dust files, and then cat your output files to a single .js file, and load them in the browser
*/
var fs = require('fs');
var path = require('path');
var parse = require('url').parse;
var dust = require('dust');
function compile() {
var src = process.argv[2];
var dest = process.argv[3];
fs.readFile(src, 'utf8', function(err, str){
if (err) {
console.log("Error", err);
} else {
dust_compiler(str, function(err, str){
if (err) {
console.log("Error compiling", err);
} else {
fs.writeFile(dest, str, 'utf8', function(err){
console.log("Error writing file", err);
});
}
}, {path: src});
}
});
}
function dust_compiler(str, fn, opts) {
try {
var path = opts.path.replace(/\.dust$/, "").replace("/","-")
if(path[0] == '-')
path= path.substring(1)
console.log("PATH", opts, path, str)
fn(null, dust.compile(str,path))
} catch (err) {
fn(err)
}
};
@abourget
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment