1 | "use strict";
|
2 | var handlebars = require("handlebars");
|
3 | var fs = require("fs");
|
4 | var templates_1 = require("./templates");
|
5 | exports.loadFromPath = function (filePath) {
|
6 | if (fs.existsSync(filePath)) {
|
7 | return fs.readFileSync(filePath, 'utf8');
|
8 | }
|
9 | else {
|
10 | throw new Error("Template file " + filePath + " does not exists!");
|
11 | }
|
12 | };
|
13 | var getGeneratorTemplate = function (templateName) {
|
14 | return templates_1.getGenerators().find(function (item) { return (item.aliases || []).indexOf(templateName.toLowerCase()) > -1; });
|
15 | };
|
16 | exports.getTemplateGenerator = function (template) {
|
17 | return new Promise(function (resolve, reject) {
|
18 | var generatorTemplate = getGeneratorTemplate(template);
|
19 | if (generatorTemplate) {
|
20 | resolve(generatorTemplate);
|
21 | }
|
22 | else {
|
23 | var allowedTemplates = templates_1.getGenerators().map(function (item) { return item.aliases; }).reduce(function (a, b) { return a.concat(b); }).join(', ');
|
24 | reject("Unknown template language specified: " + template + ", available are: " + allowedTemplates);
|
25 | }
|
26 | });
|
27 | };
|
28 | exports.compileTemplate = function (compileContext, templatePath) {
|
29 | return handlebars.compile(exports.loadFromPath(templatePath))(compileContext);
|
30 | };
|
31 |
|
\ | No newline at end of file |