-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
40 lines (36 loc) · 976 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
var assign = require("lodash.assign");
var path = require("path");
var Bluebird = require("bluebird");
var parser = require("./lib/parser");
var errors = require("./lib/errors");
/**
* Initialize a SQL templater
* @param {Array} files
* @param {Object} [opts]
* @param {Function} [callback]
* @return {Promise}
*/
module.exports = function(files, opts, callback) {
opts = assign({
prettyErrors: false
}, opts);
// Generate the templates
var templates = {};
files.forEach(function(filepath) {
filepath = path.resolve(filepath);
templates[filepath] = parser(filepath, templates, opts);
});
return Bluebird
.props(templates)
.then(function(_templates) {
return function(key, data) {
key = path.normalize(key);
if(_templates.hasOwnProperty(key)) {
return _templates[key](data);
} else {
throw new errors.NoSuchTemplate(key);
}
}
})
.nodeify(callback);
}