@deepkit/app
Version:
Deepkit App, CLI framework and service container
101 lines • 3.6 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.
*/
import { isAbsolute, join } from 'path';
import { existsSync, readFileSync } from 'fs';
class ConfigOptionNotFound extends Error {
}
ConfigOptionNotFound.__type = [() => Error, 'ConfigOptionNotFound', 'P7!5w"'];
export function resolveEnvFilePath(path) {
const resolvedPath = isAbsolute(path) ? path : findFileUntilPackageRoot(path);
if (!resolvedPath || !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 = join(dir, fileName);
if (existsSync(candidate)) {
return candidate;
}
// reached root, so stop
if (existsSync(join(dir, 'package.json')))
return;
const next = join(dir, '../');
if (next === dir)
return; //reached root
dir = next;
}
}
findFileUntilPackageRoot.__type = ['fileName', 'findFileUntilPackageRoot', 'P&2!P&-J/"'];
export 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 = 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;
}
}
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