duster/js/dustjs_shim.js

37 lines
866 B
JavaScript
Raw Normal View History

var dust = require('dustjs-linkedin');
var fs = require('fs');
const path = require('path');
var argv = process.argv.slice(2);
if (argv.length < 1) {
console.error("Expecting only 1 argument (a path to a template)");
process.exit(1);
}
var context = JSON.parse(fs.readFileSync(0, 'utf-8'));
var main_template = path.parse(argv[0])["name"];
for (var i = 0, len = argv.length; i < len; ++i) {
var filename = path.parse(argv[i])["name"];
try {
var template_source = fs.readFileSync(argv[i], 'utf-8');
} catch (err) {
console.error(err);
process.exit(1);
}
var compiled_template = dust.compile(template_source, filename);
dust.loadSource(compiled_template);
}
dust.render(main_template, context, function(err, out) {
if(err) {
console.error(err);
process.exit(1);
} else {
console.log(out);
process.exit(0);
}
});