Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

handlebar-rider

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

handlebar-rider - npm Package Compare versions

Comparing version 0.1.1 to 0.1.2

276

lib/handlebar-rider.js

@@ -0,1 +1,2 @@

handlebars = require('handlebars');

@@ -5,113 +6,214 @@ uglify = require('uglify-js');

//colored output
var hbcolor = {
red : '\u001b[31m',
blue: '\u001b[34m',
green: '\u001b[32m',
yellow: '\u001b[33m',
reset: '\u001b[0m'
}
//options and helper functions scoped globally in simple namespaced object
var hbrider = {
(function(){
templates_dir: 'app/handlebars/',
outfile: 'public/javascript/templates.js',
files: null,
minify: true,
//watches each file for changes
watchIndividualFileHandler: function(event, filename){
console.log(hbcolor.green + '[handlebar-rider] Change detected to file, recompiling' + hbcolor.reset);
hbrider.compileTemplates();
},
//colored output
var color = {
red : '\u001b[31m',
blue: '\u001b[34m',
green: '\u001b[32m',
yellow: '\u001b[33m',
reset: '\u001b[0m'
}
//options and helper functions scoped globally in simple namespaced object
var rider = {
templates_dir: 'app/handlebars/',
outfile: 'public/javascript/templates.js',
templates: [],
minify: true
}
// recursively reads in directory and namespaces template and partial objects
var readTemplatesDirectory = function(dir, done) {
// stores results
var results = []
// read the directory
fs.readdir(dir, function(err, list) {
if (err) return done(err);
var pending = list.length;
if (!pending) return done(null, results);
// walk it
list.forEach( function(file) {
file = dir + '/' + file;
// determine if file or directory
fs.stat(file, function(err, stat) {
if (stat && stat.isDirectory()) {
// push as directory
results.push('directory:' + file);
readTemplatesDirectory(file, function(err, res) {
results = results.concat(res);
if (!--pending) done(null, results);
});
} else {
// push as file
results.push('file:' + file);
if (!--pending) done(null, results);
}
});
});
});
};
//watches a file for changes
var fileHasChanged = function(event, filename){
console.log(color.green + '[handlebar-rider] Change detected to file, recompiling' + color.reset);
compileTemplates();
};
// watches a directory for changes
var directoryHasChanged = function(event, filename){
console.log(color.blue + '[handlebar-rider] New or removed file detected, recompiling' + color.reset);
readAndCompile();
};
//compiles to output destination
compileTemplates:function(){
var compileTemplates = function(){
try {
var output = ['(function() {\n var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {}; \n'];
var templates = hbrider.files;
for(var t = 0; t < templates.length; t++){
var data = fs.readFileSync(hbrider.templates_dir + templates[t], 'utf8');
var tmpl_name = templates[t].substring(0, templates[t].indexOf('.'));
var compiled = 'templates[\'' + tmpl_name + '\'] = template(' + handlebars.precompile(data, {}) + ');\n'
output.push(compiled);
var output = ['(function() {\n var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {}; Handlebars.partials = Handlebars.templates; \n'];
for(var t = 0; t < rider.templates.length; t++){
var data = fs.readFileSync(rider.templates[t].file, 'utf8');
// clean the data a bit
data = data.replace(/<!--(.*?)-->/gm, "")
.replace(/(\r\n|\n|\r)/gm,"");
var compiled = 'templates[\'' + rider.templates[t].namespace + '\'] = template(' + handlebars.precompile(data, {}) + ');\n'
output.push(compiled);
}
} catch(e) {
console.log(color.red + '[handlebar-rider] ERROR! ' + color.reset + '\n' + e);
}
//complete output array to single string
output.push('})();');
output = output.join('');
if(rider.minify){
var ast = uglify.parser.parse(output);
ast = uglify.uglify.ast_mangle(ast);
ast = uglify.uglify.ast_squeeze(ast);
output = uglify.uglify.gen_code(ast);
} catch(e) {
}
//write the output file
try {
fs.writeFileSync(rider.outfile, output, 'utf8');
console.log(hbcolor.red + '[handlebar-rider] ERROR! ' + hbcolor.reset + '\n' + e);
} catch(e){
console.log(color.red + '[handlebar-rider] ERROR! Destination file or directory does not exist.' + color.reset + '\n:' + e);
}
};
var readAndCompile = function(files){
try{
}
//read files in the directory
readTemplatesDirectory(dir, function(err, files) {
//complete output array to single string
output.push('})();');
output = output.join('');
if(hbrider.minify){
var ast = uglify.parser.parse(output);
ast = uglify.uglify.ast_mangle(ast);
ast = uglify.uglify.ast_squeeze(ast);
output = uglify.uglify.gen_code(ast);
}
//write the output file
try {
fs.writeFileSync(hbrider.outfile, output, 'utf8');
if (err) throw err;
rider.templates = []
for(var f = 0; f < files.length; f++){
// determine whether or not it's a file or directory.
info = files[f].split(':')
if( info[0] == 'file' && (info[1].indexOf('.hb') > 0 || info[1].indexOf('.handlebars') > 0) ){
// watch the file
fs.watch(info[1], fileHasChanged)
// get the template namespace by using the extra directory
template_dir = rider.templates_dir.toString();
file_and_dir = info[1].substr( (info[1].indexOf(template_dir) + template_dir.length) + 1, info[1].length);
namespaced = file_and_dir.replace(/\.hb/,'');
// push object for the compiler to work with
rider.templates.push({
namespace: namespaced,
file: info[1]
})
} else if ( info[0] == 'directory') {
// watch the directory for new / removed files
fs.watch(info[1], directoryHasChanged)
}
}
compileTemplates();
});
} catch(e){
console.log(hbcolor.red + '[handlebar-rider] ERROR! Destination file or directory does not exist.' + hbcolor.reset + '\n:' + e);
console.log(color.red + '[handlebar-rider] ERROR! Check to be sure your directories exist. ' + color.reset + e);
}
}
};
(function(){
}
//get options specified on command line
var opts = process.argv.splice(2);
var dir = opts[0] ? opts[0] : hbrider.templates_dir;
hbrider.templates_dir = dir;
var dir = opts[0] ? opts[0] : rider.templates_dir;
rider.templates_dir = dir;
//scan through remaning arguments and set appropriate variables
for(var o = 1; o < opts.length; o++){
if(opts[o] == '--out' && typeof opts[o+1] != undefined){
hbrider.outfile = opts[o+1];
rider.outfile = opts[o+1];
}
if(opts[o] == '--readable'){
hbrider.minify = false;
rider.minify = false;
}
}
//compile on startup
console.log(color.green + '[handlebar-rider] Compiling template directory ' + dir + ' to ' + rider.outfile + color.reset);
console.log(color.yellow + '[handlebar-rider] Watching template directory ' + dir + color.reset);
readAndCompile();
try{
//read files in the directory
hbrider.files = fs.readdirSync(dir);
//compile on startup
console.log(hbcolor.green + '[handlebar-rider] Compiling template directory ' + dir + ' to ' + hbrider.outfile + hbcolor.reset);
hbrider.compileTemplates();
console.log(hbcolor.yellow + '[handlebar-rider] Watching template directory ' + dir + hbcolor.reset);
for(var f = 0; f < hbrider.files.length; f++){
fs.watch(dir + hbrider.files[f], hbrider.watchIndividualFileHandler);
}
//handles changes to the directory
var directoryWatcher = fs.watch(dir, function (event, filename) {
console.log(hbcolor.blue + '[handlebar-rider] New or removed file detected, recompiling' + hbcolor.reset);
hbrider.files = fs.readdirSync(dir);
//rewatch the directory in the event of a new file.
for(var f = 0; f < hbrider.files.length; f++){
fs.watch(dir + hbrider.files[f], hbrider.watchIndividualFileHandler);
}
hbrider.compileTemplates();
});
} catch(e){
console.log(hbcolor.red + '[handlebar-rider] ERROR! Check to be sure your directories exist. ' + hbcolor.reset + e);
}
})()

@@ -5,3 +5,3 @@ {

"description" : "Watches a directory and precompiles handlebars templates",
"version" : "0.1.1",
"version" : "0.1.2",

@@ -8,0 +8,0 @@ "repository" : {

@@ -5,12 +5,13 @@ handlebar-rider

Command line tool that will watch a handlebars template directory pre-compile containing handlebars template files and concatenate them
into a single javascript file in build/public.
into a single javascript file in build/public. The directory structure you use will namespace the templates as expected with JST et all
Templates will be available in Handlebars.templates.filename (without the extension). See [Handlebars Precompiler](http://handlebarsjs.com/precompilation.html) documentation for more info.
Run-time usage example:
html_output = Handlebars.templates['users/view'](data)
Install:
git clone https://github.com/cif/handlebar-rider.git
cd handlebar-rider
npm install -g .
npm install -g handlebar-rider
Usage:

@@ -24,2 +25,3 @@

--readable This will avoid uglifying your output javascript.
--readable This will avoid uglifying your output javascript.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc