'use strict'; 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 = "1.3.0"; 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 envParseInteger(key, defaultValue) { const value = process.env[key]; if (!value) { if (defaultValue === void 0) throw new ReferenceError(`[ENV] ${key} - The key must be an integer, but is empty or undefined.`); return defaultValue; } const integer = Number(value); if (Number.isInteger(integer)) return integer; throw new TypeError(`[ENV] ${key} - The key must be an integer, but received '${value}'.`); } __name(envParseInteger, "envParseInteger"); function envParseNumber(key, defaultValue) { const value = process.env[key]; if (!value) { if (defaultValue === void 0) throw new ReferenceError(`[ENV] ${key} - The key must be a number, but is empty or undefined.`); return defaultValue; } const integer = Number(value); if (!Number.isNaN(integer)) return integer; throw new TypeError(`[ENV] ${key} - The key must be a number, but received '${value}'.`); } __name(envParseNumber, "envParseNumber"); 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 envParseString(key, defaultValue) { const value = process.env[key]; if (!value) { if (defaultValue === void 0) throw new ReferenceError(`[ENV] ${key} - The key must be a string, but is empty or undefined.`); return defaultValue; } return value; } __name(envParseString, "envParseString"); function envParseArray(key, defaultValue) { const value = process.env[key]; if (!value) { if (defaultValue === void 0) throw new ReferenceError(`[ENV] ${key} - The key must be an array, but is empty or undefined.`); return defaultValue; } return value.split(" "); } __name(envParseArray, "envParseArray"); function envIsNull(key) { const value = process.env[key]; return typeof value === "string" && (value === "0" || value.length === 4 && value.toLowerCase() === "null"); } __name(envIsNull, "envIsNull"); function envIsUndefined(key) { const value = process.env[key]; return value === void 0 || value.length === 0; } __name(envIsUndefined, "envIsUndefined"); function envIsNullish(key) { return envIsUndefined(key) || envIsNull(key); } __name(envIsNullish, "envIsNullish"); 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"); exports.envIsDefined = envIsDefined; exports.envIsNull = envIsNull; exports.envIsNullish = envIsNullish; exports.envIsUndefined = envIsUndefined; exports.envParseArray = envParseArray; exports.envParseBoolean = envParseBoolean; exports.envParseInteger = envParseInteger; exports.envParseNumber = envParseNumber; exports.envParseString = envParseString; exports.setup = setup; //# sourceMappingURL=out.js.map //# sourceMappingURL=index.cjs.map