Forked from selvakn/rhino-handlebars-precompiler.js
Last active
December 16, 2015 15:18
-
-
Save denniskuczynski/5454433 to your computer and use it in GitHub Desktop.
update to scan the directory recursively
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
importPackage(java.io); | |
importPackage(java.util); | |
(function(args) { | |
var templateFileExtension = 'handlebars', | |
output = ['// This file is auto-generated and should be ignored from version control.\n'], | |
console = { | |
log: print | |
}, | |
showUsage = function() { | |
console.log('Usage: java -jar <rhino.jar> rhino-handlebars-compiler.js --handlebars <handlebars library path> --templates <templates directory> --output <output file>'); | |
}, | |
handlebarsLibrary, | |
templatesDirectory, | |
outputFile, | |
templateFiles, | |
outStream, | |
index, | |
templateFile, | |
templateContents, | |
argumentsParser, | |
findFiles, | |
options; | |
argumentsParser = function() { | |
var arg, parse = function(args) { | |
var options = {}; | |
args = Array.prototype.slice.call(args); | |
arg = args.shift(); | |
while (arg) { | |
if (arg.indexOf("--") === 0) { | |
options[arg.substring(2)] = args.shift(); | |
} | |
arg = args.shift(); | |
} | |
return options; | |
}; | |
return { parse: parse }; | |
}; | |
findFiles = function(directory, fileList) { | |
var dir = new File(directory), | |
files = null, | |
i = 0; | |
if (!dir.isDirectory()) { | |
fileList.add(dir); | |
return fileList; | |
} | |
files = dir.listFiles(); | |
for (i=0; i < files.length; i++) { | |
findFiles(files[i], fileList); | |
} | |
return fileList; | |
}; | |
options = new argumentsParser().parse(args); | |
handlebarsLibrary = options.handlebars; | |
templatesDirectory = options.templates; | |
outputFile = options.output; | |
if (undefined === handlebarsLibrary || undefined === templatesDirectory) { | |
showUsage(); | |
java.lang.System.exit(1); | |
} | |
load(handlebarsLibrary); | |
templateFiles = findFiles(templatesDirectory, new java.util.ArrayList()); | |
outStream = new BufferedWriter(new FileWriter(outputFile)); | |
output.push('(function(){'); | |
output.push('\n var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};\n'); | |
//templateFiles = templateFiles.filter(function(fileName) { | |
// return(fileName.getName().substr(-11) === ("." + templateFileExtension)); | |
//}); | |
for (index = 0; index < templateFiles.size(); index++) { | |
templateFile = templateFiles.get(index); | |
templateName = templateFile.getPath(); | |
templateContents = Handlebars.precompile(readFile(templateFile.getAbsolutePath())); | |
output.push(' templates[\'' + templateName + '\'] = template(' + templateContents + ');\n'); | |
} | |
output.push('}());'); | |
outStream.write(output.join('')); | |
outStream.close(); | |
}(arguments)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment