UNPKG

@sap/dwf-generator

Version:

SAP HANA Data Warehousing Foundation - Generator

60 lines (50 loc) 1.63 kB
'use strict'; const loFs = require('fs'); const loLodash = require('lodash'); function EditDlmProfile(isPath, ioLog) { this.path = isPath; this.log = ioLog; this.data; this.readJson(); } EditDlmProfile.prototype.readJson = function () { try { this.data = JSON.parse(loFs.readFileSync(this.path, 'utf8')); if (typeof this.loLog != 'undefined') this.loLog.info(`Read "${this.path}" successfully`); } catch (ioErr) { console.error(ioErr); if (typeof this.loLog != 'undefined') this.loLog.error('Fail to read DLM profile'); throw new Error('Fail to read DLM profile'); } }; EditDlmProfile.prototype.writeJson = function (isMessage) { try { loFs.writeFileSync(this.path, JSON.stringify(this.data, null, 2)); if (typeof this.loLog != 'undefined') this.loLog.info(isMessage); } catch (ioErr) { if (typeof this.loLog != 'undefined') this.loLog.error(ioErr); console.error(ioErr); process.exit(1); } }; EditDlmProfile.prototype.get = function (isKey) { let loValue = loLodash.get(this.data, isKey); if (typeof loValue == 'undefined') { let loErr = new Error(`"${isKey}" is not defined in the dlm profile "${this.path}"`); throw loErr; } return loValue; }; EditDlmProfile.prototype.set = function (isKey, ioValue) { loLodash.set(this.data, isKey, ioValue); this.writeJson(`writing ${ioValue} to "${isKey}" in "${this.path}"`); }; EditDlmProfile.prototype.del = function (isKey) { this.data = loLodash.omit(this.data, isKey); this.writeJson(`deleting "${isKey}" in "${this.path}"`); }; module.exports = EditDlmProfile;