diff options
author | kpdecker <kpdecker@gmail.com> | 2015-08-04 10:55:51 -0500 |
---|---|---|
committer | kpdecker <kpdecker@gmail.com> | 2015-08-04 10:55:51 -0500 |
commit | 00f74420f949a42bedd99af022c20cdc5027228d (patch) | |
tree | 633639cb0fc33b0ac7cafed8d5a572c9afab21c3 /lib | |
parent | a62cbad95acdf544dc9daae9834588453ee1f835 (diff) | |
download | handlebars.js-00f74420f949a42bedd99af022c20cdc5027228d.zip handlebars.js-00f74420f949a42bedd99af022c20cdc5027228d.tar.gz handlebars.js-00f74420f949a42bedd99af022c20cdc5027228d.tar.bz2 |
Convert precompiler template loading to async
Diffstat (limited to 'lib')
-rw-r--r-- | lib/precompiler.js | 105 |
1 files changed, 64 insertions, 41 deletions
diff --git a/lib/precompiler.js b/lib/precompiler.js index 2809b1b..8a2019d 100644 --- a/lib/precompiler.js +++ b/lib/precompiler.js @@ -1,62 +1,85 @@ /*eslint-disable no-console */ +import Async from 'async'; import fs from 'fs'; import * as Handlebars from './handlebars'; import {basename} from 'path'; import {SourceMapConsumer, SourceNode} from 'source-map'; import uglify from 'uglify-js'; -module.exports.loadTemplates = function(opts) { +module.exports.loadTemplates = function(opts, callback) { // Build file extension pattern let extension = (opts.extension || 'handlebars').replace(/[\\^$*+?.():=!|{}\-\[\]]/g, function(arg) { return '\\' + arg; }); extension = new RegExp('\\.' + extension + '$'); - let ret = []; - function processTemplate(template, root) { - let path = template, - stat; - try { - stat = fs.statSync(template); - } catch (err) { - throw new Handlebars.Exception(`Unable to open template file "${template}"`); - } - - if (stat.isDirectory()) { - opts.hasDirectory = true; - - fs.readdirSync(template).map(function(file) { - let childPath = template + '/' + file; - - if (extension.test(childPath) || fs.statSync(childPath).isDirectory()) { - processTemplate(childPath, root || template); - } - }); - } else { - let data = fs.readFileSync(path, 'utf8'); + let ret = [], + queue = opts.files.map((template) => ({template, root: opts.root})); + Async.whilst(() => queue.length, function(callback) { + let {template: path, root} = queue.shift(); - if (opts.bom && data.indexOf('\uFEFF') === 0) { - data = data.substring(1); + fs.stat(path, function(err, stat) { + if (err) { + return callback(new Handlebars.Exception(`Unable to open template file "${path}"`)); } - // Clean the template name - if (!root) { - template = basename(template); - } else if (template.indexOf(root) === 0) { - template = template.substring(root.length + 1); + if (stat.isDirectory()) { + opts.hasDirectory = true; + + fs.readdir(path, function(err, children) { + /* istanbul ignore next : Race condition that being too lazy to test */ + if (err) { + return callback(err); + } + children.forEach(function(file) { + let childPath = path + '/' + file; + + if (extension.test(childPath) || fs.statSync(childPath).isDirectory()) { + queue.push({template: childPath, root: root || path}); + } + }); + + callback(); + }); + } else { + fs.readFile(path, 'utf8', function(err, data) { + /* istanbul ignore next : Race condition that being too lazy to test */ + if (err) { + return callback(err); + } + + if (opts.bom && data.indexOf('\uFEFF') === 0) { + data = data.substring(1); + } + + // Clean the template name + let name = path; + if (!root) { + name = basename(name); + } else if (name.indexOf(root) === 0) { + name = name.substring(root.length + 1); + } + name = name.replace(extension, ''); + + ret.push({ + path: path, + name: name, + source: data + }); + + callback(); + }); } - template = template.replace(extension, ''); + }); + }, + function(err) { + if (err) { + callback(err); + } else { + opts.templates = ret; - ret.push({ - path: path, - name: template, - source: data - }); + callback(undefined, opts); } - } - opts.templates.forEach(function(template) { - processTemplate(template, opts.root); }); - opts.templates = ret; -}; +} module.exports.cli = function(opts) { if (opts.version) { |