1 | "use strict";
|
2 | var __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 | }));
|
9 | var __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 | });
|
14 | var __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 | };
|
21 | Object.defineProperty(exports, "__esModule", { value: true });
|
22 | exports.loadTemplatizedYaml = exports.render = void 0;
|
23 | const fs = __importStar(require("fs-extra"));
|
24 | const handlebars = __importStar(require("handlebars"));
|
25 | const os_1 = require("os");
|
26 | const path_1 = require("path");
|
27 | const utils_1 = require("./utils");
|
28 | async 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 | }
|
34 | exports.render = render;
|
35 | async 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 | }
|
46 | exports.loadTemplatizedYaml = loadTemplatizedYaml;
|
47 |
|
48 | let _handlebars = null;
|
49 |
|
50 | function getHandlebars() {
|
51 | if (_handlebars) {
|
52 | return _handlebars;
|
53 | }
|
54 |
|
55 | handlebars.registerHelper('encode64', function (arg1, arg2) {
|
56 | let val;
|
57 | let opts;
|
58 |
|
59 | if (arg2 === undefined) {
|
60 | opts = arg1;
|
61 | val = opts.fn(this);
|
62 | }
|
63 |
|
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 |
|
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 |
|
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 |
|