@cimo/environment
Version:
Environment file processor. Light, fast and secure.
40 lines • 1.67 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkVariable = exports.loadFile = void 0;
const fs_1 = __importDefault(require("fs"));
const loadFile = (path) => {
const resultObject = {};
if (typeof process !== "undefined") {
if (!fs_1.default.existsSync(path)) {
throw new Error(`@cimo/environment - Service.ts - loadFile() => Environment file ${path} not found!`);
}
const fileLine = fs_1.default.readFileSync(path, "utf-8").split("\n");
for (const line of fileLine) {
const [key, value] = line.split("=");
if (key && value) {
const keyCleaned = key.trim();
const valueCleaned = value.trim().replace(/^'|'$/g, "");
const valueFinal = !Object.prototype.hasOwnProperty.call(process.env, keyCleaned) ? valueCleaned : process.env[keyCleaned] || "";
process.env[keyCleaned] = valueFinal;
resultObject[`process.env.${keyCleaned}`] = `'${valueFinal}'`;
}
}
}
return resultObject;
};
exports.loadFile = loadFile;
const checkVariable = (key) => {
if (typeof process !== "undefined") {
const value = process.env[key];
if (value === undefined) {
throw new Error(`@cimo/environment - Service.ts - checkVariable() => Environment ${key} value is not defined!`);
}
return value;
}
return "";
};
exports.checkVariable = checkVariable;
//# sourceMappingURL=Service.js.map