UNPKG

3.53 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5}) : (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
7 o[k2] = m[k];
8}));
9var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10 Object.defineProperty(o, "default", { enumerable: true, value: v });
11}) : function(o, v) {
12 o["default"] = v;
13});
14var __importStar = (this && this.__importStar) || function (mod) {
15 if (mod && mod.__esModule) return mod;
16 var result = {};
17 if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18 __setModuleDefault(result, mod);
19 return result;
20};
21Object.defineProperty(exports, "__esModule", { value: true });
22exports.loadTemplatizedYaml = exports.render = void 0;
23const fs = __importStar(require("fs-extra"));
24const handlebars = __importStar(require("handlebars"));
25const os_1 = require("os");
26const path_1 = require("path");
27const utils_1 = require("./utils");
28async function render(templateString, data) {
29 const hbs = getHandlebars();
30 const tmpl = hbs.compile(templateString, { noEscape: true });
31 const outContent = tmpl(data);
32 return outContent;
33}
34exports.render = render;
35async function loadTemplatizedYaml(path, data) {
36 try {
37 const stringContent = await fs.readFile(path, 'utf8');
38 const content = await render(stringContent, data);
39 return utils_1.yaml(content);
40 }
41 catch (ex) {
42 console.log(`ERROR - Cannot load ${path_1.resolve(path)}\n\tcause ${ex}`);
43 throw ex;
44 }
45}
46exports.loadTemplatizedYaml = loadTemplatizedYaml;
47// --------- Handlebars Factory --------- //
48let _handlebars = null;
49// Initial
50function getHandlebars() {
51 if (_handlebars) {
52 return _handlebars;
53 }
54 // encode64
55 handlebars.registerHelper('encode64', function (arg1, arg2) {
56 let val;
57 let opts;
58 // if we do not have two args, then, it is a regular helper
59 if (arg2 === undefined) {
60 opts = arg1;
61 val = opts.fn(this);
62 }
63 // if we have two args, then, the first is the value
64 else {
65 val = arg1;
66 opts = arg2;
67 }
68 const b64 = Buffer.from(val).toString('base64');
69 return b64;
70 });
71 handlebars.registerHelper('username', function () {
72 const username = os_1.userInfo().username;
73 return username;
74 });
75 // return the absolute dir of the project (where the script is being run) (with an ending '/')
76 handlebars.registerHelper('projectDir', function () {
77 return path_1.resolve('./') + '/';
78 });
79 handlebars.registerHelper('assign', function (varName, varValue, options) {
80 if (!options.data.root) {
81 options.data.root = {};
82 }
83 options.data.root[varName] = varValue;
84 });
85 handlebars.registerHelper('assignJsonValue', function (varName, jsonFile, namePath, options) {
86 if (!options.data.root) {
87 options.data.root = {};
88 }
89 // handlebars helper does not support async
90 const obj = fs.readJsonSync(jsonFile);
91 const val = utils_1.findVal(obj, namePath);
92 options.data.root[varName] = val;
93 });
94 _handlebars = handlebars;
95 return _handlebars;
96}
97// --------- /Handlebars Factory --------- //