@skyra/env-utilities
Version:
Functional utilities for reading and parsing environmental variables
113 lines (108 loc) • 3.98 kB
JavaScript
;
var url = require('url');
var dotenv = require('dotenv');
var dotenvExpand = require('dotenv-expand');
var path = require('path');
var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var packageVersion = "2.0.1";
function loadEnvFiles(options) {
const log = options?.debug ? (message) => console.debug(`[@skyra/env-utilities@${packageVersion}][DEBUG] ${message}`) : (_) => void 0;
if (!process.env.NODE_ENV) {
throw new Error("The NODE_ENV environment variable is required but was not specified.");
}
const env = options?.env || process.env.NODE_ENV;
const dotenvPath = options?.path || path.resolve(process.cwd(), ".env");
const dotenvFiles = [`${dotenvPath}.${env}.local`, `${dotenvPath}.${env}`, dotenvPath];
if (process.env.NODE_ENV !== "test") {
dotenvFiles.splice(1, 0, `${dotenvPath}.local`);
}
let parsed = {};
for (const dotenvFile of dotenvFiles) {
const dotenvFileString = typeof dotenvFile === "string" ? dotenvFile : url.fileURLToPath(dotenvFile);
log(`loading \`${path.basename(dotenvFileString)}\``);
const result = dotenvExpand.expand(
dotenv.config({
debug: options?.debug,
encoding: options?.encoding,
path: dotenvFile
})
);
if (result.error) {
if (result.error.code === "ENOENT") {
log(`\`${path.basename(dotenvFileString)}\` file not found`);
continue;
}
throw result.error;
}
parsed = { ...result.parsed, ...parsed };
}
if (options?.prefix) {
const prefixRegExp = new RegExp(`^${options.prefix}`, "i");
parsed = Object.keys(parsed).filter((key) => {
const match = prefixRegExp.test(key);
log(`Prefix for key \`${key}\` ${match ? "matches" : "does not match"} \`${options.prefix}\``);
return match;
}).reduce((obj, key) => {
obj[key] = parsed[key];
return obj;
}, {});
}
return {
parsed
};
}
__name(loadEnvFiles, "loadEnvFiles");
// src/lib/utils.ts
function envParseBoolean(key, defaultValue) {
const value = process.env[key];
if (!value) {
if (defaultValue === void 0) throw new ReferenceError(`[ENV] ${key} - The key must be a boolean, but is empty or undefined.`);
return defaultValue;
}
if (value.length === 4 && value.toLowerCase() === "true") return true;
if (value.length === 5 && value.toLowerCase() === "false") return false;
throw new TypeError(`[ENV] ${key} - The key must be a boolean, but received '${value}'.`);
}
__name(envParseBoolean, "envParseBoolean");
function envIsUndefined(key) {
const value = process.env[key];
return value === void 0 || value.length === 0;
}
__name(envIsUndefined, "envIsUndefined");
function envIsDefined(...keys) {
return keys.every((key) => !envIsUndefined(key));
}
__name(envIsDefined, "envIsDefined");
// src/lib/setup.ts
function setup(pathOrOptions) {
process.env.NODE_ENV ??= "development";
let options;
if (typeof pathOrOptions === "undefined") {
options = {};
} else if (typeof pathOrOptions === "string") {
options = { path: pathOrOptions };
} else if (typeof pathOrOptions === "object") {
if (pathOrOptions instanceof URL) {
options = { path: url.fileURLToPath(pathOrOptions) };
} else {
const { path, ...rest } = pathOrOptions;
options = { path: typeof path === "undefined" ? path : typeof path === "string" ? path : url.fileURLToPath(path), ...rest };
}
} else {
throw new TypeError("Expected undefined, string, URL, or EnvSetupOptions");
}
return loadEnvFiles({
debug: envIsDefined("DOTENV_DEBUG") ? envParseBoolean("DOTENV_DEBUG") : void 0,
encoding: process.env.DOTENV_ENCODING,
env: process.env.DOTENV_ENV,
path: process.env.DOTENV_PATH,
prefix: process.env.DOTENV_PREFIX,
...options
});
}
__name(setup, "setup");
// src/setup.ts
setup();
//# sourceMappingURL=setup.cjs.map
//# sourceMappingURL=setup.cjs.map