@deepkit/app
Version:
Deepkit App, CLI framework and service container
106 lines • 3.85 kB
JavaScript
/*
* Deepkit Framework
* Copyright (C) 2021 Deepkit UG, Marc J. Schmidt
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the MIT License.
*
* You should have received a copy of the MIT License along with this program.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.EnvConfiguration = void 0;
exports.resolveEnvFilePath = resolveEnvFilePath;
const path_1 = require("path");
const fs_1 = require("fs");
class ConfigOptionNotFound extends Error {
}
ConfigOptionNotFound.__type = [() => Error, 'ConfigOptionNotFound', 'P7!5w"'];
function resolveEnvFilePath(path) {
const resolvedPath = (0, path_1.isAbsolute)(path) ? path : findFileUntilPackageRoot(path);
if (!resolvedPath || !(0, fs_1.existsSync)(resolvedPath))
return undefined;
return resolvedPath;
}
resolveEnvFilePath.__type = ['path', 'resolveEnvFilePath', 'P&2!P&-J/"'];
function findFileUntilPackageRoot(fileName) {
let dir = process.cwd();
while (true) {
const candidate = (0, path_1.join)(dir, fileName);
if ((0, fs_1.existsSync)(candidate)) {
return candidate;
}
// reached root, so stop
if ((0, fs_1.existsSync)((0, path_1.join)(dir, 'package.json')))
return;
const next = (0, path_1.join)(dir, '../');
if (next === dir)
return; //reached root
dir = next;
}
}
findFileUntilPackageRoot.__type = ['fileName', 'findFileUntilPackageRoot', 'P&2!P&-J/"'];
class EnvConfiguration {
constructor() {
this.container = {};
}
/**
* Reads a .env file from given path, based to basePath.
*/
loadEnvFile(path) {
const resolvedPath = resolveEnvFilePath(path);
if (!resolvedPath)
return false;
const RE_INI_KEY_VAL = /^\s*([\w.-]+)\s*=\s*(.*)?\s*$/;
const content = (0, fs_1.readFileSync)(resolvedPath);
for (const line of content.toString('utf8').split('\n')) {
const keyValueArr = line.match(RE_INI_KEY_VAL);
if (!keyValueArr)
continue;
const key = keyValueArr[1];
let value = keyValueArr[2] || '';
const end = value.length - 1;
const isDoubleQuoted = value[0] === '"' && value[end] === '"';
const isSingleQuoted = value[0] === '\'' && value[end] === '\'';
// if single or double quoted, remove quotes
if (isSingleQuoted) {
value = value.substring(1, end);
}
else if (isDoubleQuoted) {
value = JSON.parse(value);
}
else {
value = value.trim();
}
this.container[key] = value;
}
return true;
}
getKeys() {
return Object.keys(this.container);
}
getAll() {
return this.container;
}
/**
* Returns the value for a configuration option.
*
* Priority is first process.env, then manually set options, then values from the loaded env file.
*/
get(name) {
if (process.env[name] !== undefined)
return process.env[name];
if (this.container[name] !== undefined)
return this.container[name];
throw new ConfigOptionNotFound(`Config option ${name} not found.`);
}
/**
* Sets an option value. If a env file has been read, it will be overwritten.
*/
set(name, value) {
this.container[name] = value;
}
}
exports.EnvConfiguration = EnvConfiguration;
EnvConfiguration.__type = ['container', function () { return {}; }, 'path', 'loadEnvFile', 'getKeys', 'getAll', 'name', 'get', 'value', 'set', 'EnvConfiguration', 'P&"LM3!<>"P&2#)0$P&F0%PP&"LM0&P&2\'"0(P&2\'"2)"0*5w+'];
//# sourceMappingURL=configuration.js.map
;