Newer
Older
Shane Tomlinson
committed
#!/usr/bin/env node
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Shane Tomlinson
committed
Lloyd Hilaiel
committed
const
fs = require("fs"),
path = require('path'),
ejs = require('ejs'),
config = require('../lib/configuration');
Shane Tomlinson
committed
var dir = process.env.TEMPLATE_DIR || process.cwd();
var output_dir = process.env.BUILD_DIR || dir;
Shane Tomlinson
committed
Shane Tomlinson
committed
function generateTemplates(outputType, templatesDir, namePrefix) {
if (templatesDir) dir = templatesDir;
if (!namePrefix) namePrefix = "";
var bundle = bundles[templatesDir] || (bundles[templatesDir] = {});
var lastGen = bundle.lastGen || 0;
var templateData = bundle.data;
var fileNames = fs.readdirSync(dir);
var templates = {};
Lloyd Hilaiel
committed
// is a regen even neccesary?
try {
if (outputType !== generateTemplates.RETURN) {
lastGen = fs.statSync(path.join(output_dir, "templates.js")).mtime;
}
Lloyd Hilaiel
committed
for (var i = 0; i < fileNames.length; i++) {
if (lastGen < fs.statSync(path.join(dir, fileNames[i])).mtime) {
throw "newer";
}
};
// no rebuild needed
console.log("templates.js is up to date");
return templateData;
Lloyd Hilaiel
committed
} catch (e) {
console.log("creating templates.js");
Lloyd Hilaiel
committed
}
Shane Tomlinson
committed
for(var index = 0, max = fileNames.length; index < max; index++) {
var fileName = fileNames[index];
if(fileName.match(/\.ejs$/)) {
var templateName = namePrefix + fileName.replace(/\.ejs/, '');
var templateText = fs.readFileSync(dir + "/" + fileName, "utf8");
templates[templateName] = ejs.compile(templateText, {
client: true,
compileDebug: !config.get('use_minified_resources')
Shane Tomlinson
committed
}
}
templateData = "BrowserID.Templates = BrowserID.Templates || {};";
for (var t in templates) {
if (templates.hasOwnProperty(t)) {
templateData += "\nBrowserID.Templates['" + t + "'] = " + String(templates[t]);
}
}
Shane Tomlinson
committed
if (outputType === generateTemplates.RETURN) {
bundle.lastGen = Date.now();
bundle.data = templateData;
return templateData;
} else {
fs.writeFileSync(output_dir + "/templates.js", templateData, "utf8");
}
Lloyd Hilaiel
committed
};
Shane Tomlinson
committed
generateTemplates.FILE = 0;
generateTemplates.RETURN = 1;
Lloyd Hilaiel
committed
// run or export the function
if (process.argv[1] === __filename) generateTemplates();
else module.exports = generateTemplates;