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.
This commit is contained in:
+1
-1
@@ -92,4 +92,4 @@ var optimist = require('optimist')
|
||||
var argv = optimist.argv;
|
||||
argv.templates = argv._;
|
||||
delete argv._;
|
||||
return require('../lib/handlebars/precompiler')(argv);
|
||||
return require('../lib/precompiler').cli(argv);
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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"');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user