summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkpdecker <kpdecker@gmail.com>2014-07-05 14:17:11 -0500
committerkpdecker <kpdecker@gmail.com>2014-07-05 14:39:28 -0500
commit059532295d022c775f6c3e6636a9d6ffce8da728 (patch)
treea9766dd85630be28c24e5f1bd0bd3c7b988640c4
parent0c084399e27946c29ead76e870d76a7504e13474 (diff)
downloadhandlebars.js-059532295d022c775f6c3e6636a9d6ffce8da728.zip
handlebars.js-059532295d022c775f6c3e6636a9d6ffce8da728.tar.gz
handlebars.js-059532295d022c775f6c3e6636a9d6ffce8da728.tar.bz2
Move precompiler to safer location
Attempts to avoid some of the Node vs. ES6 confusion between the different styles within the application. Also adds some simple tests in addition to the integration test.
-rwxr-xr-xbin/handlebars2
-rw-r--r--lib/precompiler.js (renamed from lib/handlebars/precompiler.js)22
-rw-r--r--spec/precompiler.js40
3 files changed, 54 insertions, 10 deletions
diff --git a/bin/handlebars b/bin/handlebars
index b400aaa..4ea3296 100755
--- a/bin/handlebars
+++ b/bin/handlebars
@@ -92,4 +92,4 @@ var optimist = require('optimist')
var argv = optimist.argv;
argv.templates = argv._;
delete argv._;
-return require('../lib/handlebars/precompiler')(argv); \ No newline at end of file
+return require('../lib/precompiler').cli(argv);
diff --git a/lib/handlebars/precompiler.js b/lib/precompiler.js
index 19a5e4c..bae923c 100644
--- a/lib/handlebars/precompiler.js
+++ b/lib/precompiler.js
@@ -1,29 +1,33 @@
var fs = require('fs'),
- handlebars = require('../../lib'),
+ Handlebars = require('./index'),
basename = require('path').basename,
uglify = require('uglify-js');
-module.exports = function(opts) {
+module.exports.cli = function(opts) {
+ if (opts.version) {
+ console.log(Handlebars.VERSION);
+ return;
+ }
var template = [0];
if (!opts.templates.length) {
- throw 'Must define at least one template or directory.';
+ throw new Handlebars.Exception('Must define at least one template or directory.');
}
opts.templates.forEach(function(template) {
try {
fs.statSync(template);
} catch (err) {
- throw 'Unable to open template file "' + template + '"';
+ throw new Handlebars.Exception('Unable to open template file "' + template + '"');
}
});
if (opts.simple && opts.min) {
- throw 'Unable to minimze simple output';
+ throw new Handlebars.Exception('Unable to minimze simple output');
}
if (opts.simple && (opts.templates.length !== 1 || fs.statSync(opts.templates[0]).isDirectory())) {
- throw 'Unable to output multiple templates in simple mode';
+ throw new Handlebars.Exception('Unable to output multiple templates in simple mode');
}
// Convert the known list into a hash
@@ -92,17 +96,17 @@ module.exports = function(opts) {
template = template.replace(extension, '');
if (opts.simple) {
- output.push(handlebars.precompile(data, options) + '\n');
+ output.push(Handlebars.precompile(data, options) + '\n');
} else if (opts.partial) {
if(opts.amd && (opts.templates.length == 1 && !fs.statSync(opts.templates[0]).isDirectory())) {
output.push('return ');
}
- output.push('Handlebars.partials[\'' + template + '\'] = template(' + handlebars.precompile(data, options) + ');\n');
+ output.push('Handlebars.partials[\'' + template + '\'] = template(' + Handlebars.precompile(data, options) + ');\n');
} else {
if(opts.amd && (opts.templates.length == 1 && !fs.statSync(opts.templates[0]).isDirectory())) {
output.push('return ');
}
- output.push('templates[\'' + template + '\'] = template(' + handlebars.precompile(data, options) + ');\n');
+ output.push('templates[\'' + template + '\'] = template(' + Handlebars.precompile(data, options) + ');\n');
}
}
}
diff --git a/spec/precompiler.js b/spec/precompiler.js
new file mode 100644
index 0000000..10e2a2e
--- /dev/null
+++ b/spec/precompiler.js
@@ -0,0 +1,40 @@
+/*global shouldThrow */
+
+// NOP Under non-node environments
+if (typeof process === 'undefined') {
+ return;
+}
+
+var Handlebars = require('../lib'),
+ Precompiler = require('../lib/precompiler');
+
+describe('precompiler', function() {
+ var log,
+ logFunction;
+
+ beforeEach(function() {
+ logFunction = console.log;
+ log = '';
+ console.log = function() {
+ log += Array.prototype.join.call(arguments, '');
+ };
+ });
+ afterEach(function() {
+ console.log = logFunction;
+ });
+
+ it('should output version', function() {
+ Precompiler.cli({templates: [], version: true});
+ equals(log, Handlebars.VERSION);
+ });
+ it('should throw if lacking templates', function() {
+ shouldThrow(function() {
+ Precompiler.cli({templates: []});
+ }, Handlebars.Exception, 'Must define at least one template or directory.');
+ });
+ it('should throw on missing template', function() {
+ shouldThrow(function() {
+ Precompiler.cli({templates: ['foo']});
+ }, Handlebars.Exception, 'Unable to open template file "foo"');
+ });
+});