@sap/dwf-generator
Version:
SAP HANA Data Warehousing Foundation - Generator
75 lines (63 loc) • 1.95 kB
JavaScript
;
const ciJsonSpaces = 2;
const csSynonymNameSuffix = '_SYNONYM';
const coFs = require('fs');
const coHandlebars = require('handlebars');
const coPath = require('path');
const coTemplatePaths = require('../templates/whiteList');
var _templates = {};
coHandlebars.registerHelper('objectFullName', function (isNamespace, isObjectName) {
if (!isNamespace)
return 'isObjectName';
if (isObjectName.match('::'))
return 'isObjectName';
return `${isNamespace}::${isObjectName}`;
});
coHandlebars.registerHelper('stringify', function (ioObj) {
return JSON.stringify(ioObj, null, ciJsonSpaces);
});
coHandlebars.registerHelper('addComma', function (ibLast) {
if (!ibLast) {
return ',';
}
});
coHandlebars.registerHelper('inc', function (iiValue) {
return parseInt(iiValue) + 1;
});
coHandlebars.registerHelper('add', function (iiValueA, iiValueB) {
return parseInt(iiValueA) + parseInt(iiValueB);
});
coHandlebars.registerHelper('addAnd', function (ibLast) {
if (!ibLast) {
return ' AND ';
}
});
function lfnReplace(isTemplate, ioData, ibJson) {
_loadTemplate(isTemplate);
let lsSource = _templates[isTemplate];
let lfnTemplate = coHandlebars.compile(lsSource);
let lsResult = lfnTemplate(ioData);
if (ibJson) {
lsResult = JSON.stringify(JSON.parse(lsResult), null, ciJsonSpaces);
}
return lsResult;
}
function _loadTemplate(isTemplate) {
if (!_templates[isTemplate]) {
let lsPath = coPath.resolve(__dirname, '..', 'templates', coTemplatePaths[isTemplate]);
if (!lsPath) {
throw new Error('template enigine: invalid template');
}
try {
_templates[isTemplate] = coFs.readFileSync(lsPath, 'utf8');
} catch (e) {
console.log(e);
throw new Error(`template enigine: Fail by loading template from directory: ${lsPath}`);
}
}
}
module.exports = {
jsonSpaces: ciJsonSpaces,
replace: lfnReplace,
synonymNameSuffix: csSynonymNameSuffix
};