@wolfstar/env-utilities 2.0.8 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -4
- package/dist/cjs/index.cjs +1 -1
- package/dist/cjs/index.d.cts +29 -30
- package/dist/cjs/index.d.cts.map +1 -1
- package/dist/cjs/setup.cjs +1 -1
- package/dist/cjs/{src-EtAdpAbB.cjs → src-5_ZeHgLf.cjs} +18 -12
- package/dist/cjs/{src-EtAdpAbB.cjs.map → src-5_ZeHgLf.cjs.map} +1 -1
- package/dist/esm/index.d.ts +29 -30
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/setup.js +1 -1
- package/dist/esm/{src-CFjmMu75.js → src-DbngwUYN.js} +19 -13
- package/dist/esm/{src-CFjmMu75.js.map → src-DbngwUYN.js.map} +1 -1
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -1,4 +1,19 @@
|
|
|
1
|
-
|
|
1
|
+
<div align="center">
|
|
2
|
+
<picture>
|
|
3
|
+
<img src="https://cdn.wolfstar.rocks/assets/stars-components/wordmark.webp" alt="Stars Components" width="440" />
|
|
4
|
+
</picture>
|
|
5
|
+
|
|
6
|
+
# @wolfstar/env-utilities
|
|
7
|
+
|
|
8
|
+
**Functional utilities for reading and parsing environment variables.**
|
|
9
|
+
|
|
10
|
+
[](https://npmx.dev/package/@wolfstar/env-utilities)
|
|
11
|
+
[](https://npmx.dev/package/@wolfstar/env-utilities)
|
|
12
|
+
[](https://github.com/wolfstar-project/stars-components/blob/main/LICENSE)
|
|
13
|
+
|
|
14
|
+
</div>
|
|
15
|
+
|
|
16
|
+
## Description
|
|
2
17
|
|
|
3
18
|
Functional utilities for reading and parsing environmental variables, based on [Wolfstar](https://wolfstar.rocks)'s internal tools.
|
|
4
19
|
|
|
@@ -11,9 +26,8 @@ To setup `@wolfstar/env-utilities`, you use the `setup` function exported by the
|
|
|
11
26
|
```typescript
|
|
12
27
|
import { setup } from '@wolfstar/env-utilities';
|
|
13
28
|
|
|
14
|
-
//
|
|
15
|
-
|
|
16
|
-
setup(new URL('.env', import.meta.url));
|
|
29
|
+
// Finds src/.env* and .env* from the current project automatically.
|
|
30
|
+
setup();
|
|
17
31
|
```
|
|
18
32
|
|
|
19
33
|
Alternatively, if you do not need to provide any custom options you can import it as a side effect:
|
|
@@ -32,6 +46,9 @@ You can also pass a `string` or if you want to define other options, you may use
|
|
|
32
46
|
|
|
33
47
|
### What `.env` files can be used?
|
|
34
48
|
|
|
49
|
+
Every file below is searched first under `src/`, then at the project root. An explicit `path` or `DOTENV_PATH`
|
|
50
|
+
disables this discovery and uses that base path only.
|
|
51
|
+
|
|
35
52
|
- `.env`: Default.
|
|
36
53
|
- `.env.local`: Local overrides. This file is loaded for all environments except test.
|
|
37
54
|
- `.env.development`, `.env.test`, `.env.production`: Environment-specific settings.
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
const require_src = require('./src-
|
|
2
|
+
const require_src = require('./src-5_ZeHgLf.cjs');
|
|
3
3
|
|
|
4
4
|
exports.envIsDefined = require_src.envIsDefined;
|
|
5
5
|
exports.envIsNull = require_src.envIsNull;
|
package/dist/cjs/index.d.cts
CHANGED
|
@@ -13,7 +13,7 @@ interface EnvLoaderOptions extends Omit<DotenvConfigOptions, 'path'> {
|
|
|
13
13
|
* Specify a custom path if your file containing environment variables is located elsewhere.
|
|
14
14
|
* Can also be an array of strings, specifying multiple paths.
|
|
15
15
|
*
|
|
16
|
-
* @default `
|
|
16
|
+
* @default `src/.env*`, then `.env*` in `process.cwd()`
|
|
17
17
|
*
|
|
18
18
|
* @example with CJS
|
|
19
19
|
* ```typescript
|
|
@@ -31,29 +31,29 @@ interface EnvLoaderOptions extends Omit<DotenvConfigOptions, 'path'> {
|
|
|
31
31
|
}
|
|
32
32
|
//#endregion
|
|
33
33
|
//#region src/lib/setup.d.ts
|
|
34
|
-
declare function setup(pathOrOptions?: string | URL | EnvSetupOptions): DotenvConfigOutput;
|
|
35
|
-
interface EnvSetupOptions extends Omit<EnvLoaderOptions, 'path'> {
|
|
34
|
+
export declare function setup(pathOrOptions?: string | URL | EnvSetupOptions): DotenvConfigOutput;
|
|
35
|
+
export interface EnvSetupOptions extends Omit<EnvLoaderOptions, 'path'> {
|
|
36
36
|
/**
|
|
37
37
|
* You may specify a custom path if your file containing environment variables is located elsewhere.
|
|
38
38
|
*/
|
|
39
39
|
path?: string | URL;
|
|
40
40
|
}
|
|
41
|
-
type EnvSetupResult = DotenvConfigOutput;
|
|
41
|
+
export type EnvSetupResult = DotenvConfigOutput;
|
|
42
42
|
//#endregion
|
|
43
43
|
//#region src/lib/types.d.ts
|
|
44
|
-
type BooleanString = 'true' | 'false';
|
|
45
|
-
type IntegerString = `${bigint}`;
|
|
46
|
-
type NumberString = `${number}`;
|
|
47
|
-
type ArrayString = string & {
|
|
44
|
+
export type BooleanString = 'true' | 'false';
|
|
45
|
+
export type IntegerString = `${bigint}`;
|
|
46
|
+
export type NumberString = `${number}`;
|
|
47
|
+
export type ArrayString = string & {
|
|
48
48
|
__type__: 'ArrayString';
|
|
49
49
|
};
|
|
50
|
-
type EnvAny = keyof Env;
|
|
51
|
-
type EnvString = { [K in EnvAny]: Env[K] extends BooleanString | IntegerString | NumberString ? never : K; }[EnvAny];
|
|
52
|
-
type EnvBoolean = { [K in EnvAny]: Env[K] extends BooleanString | undefined ? K : never; }[EnvAny];
|
|
53
|
-
type EnvInteger = { [K in EnvAny]: Env[K] extends IntegerString | undefined ? K : never; }[EnvAny];
|
|
54
|
-
type EnvNumber = { [K in EnvAny]: Env[K] extends NumberString | undefined ? K : never; }[EnvAny];
|
|
55
|
-
type EnvArray = { [K in EnvAny]: Env[K] extends ArrayString | undefined ? K : never; }[EnvAny];
|
|
56
|
-
interface Env {
|
|
50
|
+
export type EnvAny = keyof Env;
|
|
51
|
+
export type EnvString = { [K in EnvAny]: Env[K] extends BooleanString | IntegerString | NumberString ? never : K; }[EnvAny];
|
|
52
|
+
export type EnvBoolean = { [K in EnvAny]: Env[K] extends BooleanString | undefined ? K : never; }[EnvAny];
|
|
53
|
+
export type EnvInteger = { [K in EnvAny]: Env[K] extends IntegerString | undefined ? K : never; }[EnvAny];
|
|
54
|
+
export type EnvNumber = { [K in EnvAny]: Env[K] extends NumberString | undefined ? K : never; }[EnvAny];
|
|
55
|
+
export type EnvArray = { [K in EnvAny]: Env[K] extends ArrayString | undefined ? K : never; }[EnvAny];
|
|
56
|
+
export interface Env {
|
|
57
57
|
NODE_ENV: 'test' | 'development' | 'production';
|
|
58
58
|
DOTENV_DEBUG?: BooleanString;
|
|
59
59
|
DOTENV_ENCODING?: string;
|
|
@@ -63,16 +63,16 @@ interface Env {
|
|
|
63
63
|
}
|
|
64
64
|
//#endregion
|
|
65
65
|
//#region src/lib/utils.d.ts
|
|
66
|
-
declare function envParseInteger(key: EnvInteger, defaultValue?: number): number;
|
|
67
|
-
declare function envParseInteger(key: EnvInteger, defaultValue: number | null): number | null;
|
|
68
|
-
declare function envParseNumber(key: EnvNumber, defaultValue?: number): number;
|
|
69
|
-
declare function envParseNumber(key: EnvNumber, defaultValue: number | null): number | null;
|
|
70
|
-
declare function envParseBoolean(key: EnvBoolean, defaultValue?: boolean): boolean;
|
|
71
|
-
declare function envParseBoolean(key: EnvBoolean, defaultValue: boolean | null): boolean | null;
|
|
72
|
-
declare function envParseString<K extends EnvString>(key: K, defaultValue?: Env[K]): NonNullable<Env[K]>;
|
|
73
|
-
declare function envParseString<K extends EnvString>(key: K, defaultValue: Env[K] | null): NonNullable<Env[K]> | null;
|
|
74
|
-
declare function envParseArray(key: EnvArray, defaultValue?: string[]): string[];
|
|
75
|
-
declare function envParseArray(key: EnvArray, defaultValue: string[] | null): string[] | null;
|
|
66
|
+
export declare function envParseInteger(key: EnvInteger, defaultValue?: number): number;
|
|
67
|
+
export declare function envParseInteger(key: EnvInteger, defaultValue: number | null): number | null;
|
|
68
|
+
export declare function envParseNumber(key: EnvNumber, defaultValue?: number): number;
|
|
69
|
+
export declare function envParseNumber(key: EnvNumber, defaultValue: number | null): number | null;
|
|
70
|
+
export declare function envParseBoolean(key: EnvBoolean, defaultValue?: boolean): boolean;
|
|
71
|
+
export declare function envParseBoolean(key: EnvBoolean, defaultValue: boolean | null): boolean | null;
|
|
72
|
+
export declare function envParseString<K extends EnvString>(key: K, defaultValue?: Env[K]): NonNullable<Env[K]>;
|
|
73
|
+
export declare function envParseString<K extends EnvString>(key: K, defaultValue: Env[K] | null): NonNullable<Env[K]> | null;
|
|
74
|
+
export declare function envParseArray(key: EnvArray, defaultValue?: string[]): string[];
|
|
75
|
+
export declare function envParseArray(key: EnvArray, defaultValue: string[] | null): string[] | null;
|
|
76
76
|
/**
|
|
77
77
|
* Checks if the value of the specified environment variable is null.
|
|
78
78
|
* @param key - The name of the environment variable.
|
|
@@ -80,7 +80,7 @@ declare function envParseArray(key: EnvArray, defaultValue: string[] | null): st
|
|
|
80
80
|
* - The string `"0"`
|
|
81
81
|
* - The string `"null"`, case insensitive
|
|
82
82
|
*/
|
|
83
|
-
declare function envIsNull(key: EnvAny): boolean;
|
|
83
|
+
export declare function envIsNull(key: EnvAny): boolean;
|
|
84
84
|
/**
|
|
85
85
|
* Checks if the value of the specified environment variable is undefined.
|
|
86
86
|
* @param key - The name of the environment variable.
|
|
@@ -88,7 +88,7 @@ declare function envIsNull(key: EnvAny): boolean;
|
|
|
88
88
|
* - `undefined`
|
|
89
89
|
* - An empty string (`""`)
|
|
90
90
|
*/
|
|
91
|
-
declare function envIsUndefined(key: EnvAny): boolean;
|
|
91
|
+
export declare function envIsUndefined(key: EnvAny): boolean;
|
|
92
92
|
/**
|
|
93
93
|
* Checks if the value of the specified environment variable is nullish.
|
|
94
94
|
* @param key - The name of the environment variable.
|
|
@@ -98,13 +98,13 @@ declare function envIsUndefined(key: EnvAny): boolean;
|
|
|
98
98
|
* - The string `"0"`
|
|
99
99
|
* - The string `"null"`, case insensitive
|
|
100
100
|
*/
|
|
101
|
-
declare function envIsNullish(key: EnvAny): boolean;
|
|
101
|
+
export declare function envIsNullish(key: EnvAny): boolean;
|
|
102
102
|
/**
|
|
103
103
|
* Checks if any of the specified environment variables is defined.
|
|
104
104
|
* @param key - The name of the environment variable.
|
|
105
105
|
* @returns Whether the value of any of the specified environment variables is a non-empty string
|
|
106
106
|
*/
|
|
107
|
-
declare function envIsDefined(...keys: readonly EnvAny[]): boolean;
|
|
107
|
+
export declare function envIsDefined(...keys: readonly EnvAny[]): boolean;
|
|
108
108
|
//#endregion
|
|
109
109
|
//#region src/index.d.ts
|
|
110
110
|
declare global {
|
|
@@ -113,5 +113,4 @@ declare global {
|
|
|
113
113
|
}
|
|
114
114
|
}
|
|
115
115
|
//#endregion
|
|
116
|
-
export { ArrayString, BooleanString, Env, EnvAny, EnvArray, EnvBoolean, EnvInteger, EnvNumber, EnvSetupOptions, EnvSetupResult, EnvString, IntegerString, NumberString, envIsDefined, envIsNull, envIsNullish, envIsUndefined, envParseArray, envParseBoolean, envParseInteger, envParseNumber, envParseString, setup };
|
|
117
116
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/cjs/index.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","names":[],"sources":["../../src/lib/env-loader.ts","../../src/lib/setup.ts","../../src/lib/types.ts","../../src/lib/utils.ts","../../src/index.ts"],"mappings":";;
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../../src/lib/env-loader.ts","../../src/lib/setup.ts","../../src/lib/types.ts","../../src/lib/utils.ts","../../src/index.ts"],"mappings":";;UAMiB,yBAAyB,KAAK;;;;EAI9C;;;;EAIA;;;;;;;;;;;;;;;;;;;EAmBA,gBAAgB;;;;wBC5BD,MAAM,yBAAyB,MAAM,kBAAkB;iBA8BtD,wBAAwB,KAAK;;;;EAI7C,gBAAgB;;YAGL,iBAAiB;;;YC1CjB;YACA;YACA;YACA;EAAyB;;YAEzB,eAAe;YACf,eAAe,KAAK,SAAS,IAAI,WAAW,gBAAgB,gBAAgB,uBAAuB,KAAI;YACvG,gBAAgB,KAAK,SAAS,IAAI,WAAW,4BAA4B,aAAY;YACrF,gBAAgB,KAAK,SAAS,IAAI,WAAW,4BAA4B,aAAY;YACrF,eAAe,KAAK,SAAS,IAAI,WAAW,2BAA2B,aAAY;YACnF,cAAc,KAAK,SAAS,IAAI,WAAW,0BAA0B,aAAY;iBAE5E;EAChB;EACA,eAAe;EACf;EACA;EACA;EACA;;;;wBChBe,gBAAgB,KAAK,YAAY;wBACjC,gBAAgB,KAAK,YAAY;wBAajC,eAAe,KAAK,WAAW;wBAC/B,eAAe,KAAK,WAAW;wBAa/B,gBAAgB,KAAK,YAAY;wBACjC,gBAAgB,KAAK,YAAY;wBAajC,eAAe,UAAU,WAAW,KAAK,GAAG,eAAe,IAAI,KAAK,YAAY,IAAI;wBACpF,eAAe,UAAU,WAAW,KAAK,GAAG,cAAc,IAAI,YAAY,YAAY,IAAI;wBAW1F,cAAc,KAAK,UAAU;wBAC7B,cAAc,KAAK,UAAU;;;;;;;;wBAkB7B,UAAU,KAAK;;;;;;;;wBAYf,eAAe,KAAK;;;;;;;;;;wBAcpB,aAAa,KAAK;;;;;;wBASlB,gBAAgB,eAAe;;;QCxGvC;YACG;cACC,mBAAmB"}
|
package/dist/cjs/setup.cjs
CHANGED
|
@@ -1,27 +1,28 @@
|
|
|
1
1
|
let node_url = require("node:url");
|
|
2
|
+
let _sapphire_pieces = require("@sapphire/pieces");
|
|
2
3
|
let dotenv = require("dotenv");
|
|
3
4
|
let dotenv_expand = require("dotenv-expand");
|
|
4
5
|
let node_path = require("node:path");
|
|
5
6
|
|
|
6
7
|
//#region src/lib/env-loader.ts
|
|
7
|
-
const packageVersion = "2.
|
|
8
|
+
const packageVersion = "2.1.1";
|
|
9
|
+
function resolveDebugLogger() {
|
|
10
|
+
return _sapphire_pieces.container.logger ?? console;
|
|
11
|
+
}
|
|
8
12
|
function loadEnvFiles(options) {
|
|
9
|
-
const log = options?.debug ? (message) =>
|
|
13
|
+
const log = options?.debug ? (message) => resolveDebugLogger().debug(`[@wolfstar/env-utilities@${packageVersion}] ${message}`) : () => void 0;
|
|
10
14
|
/**
|
|
11
15
|
* @see {@linkplain https://github.com/facebook/create-react-app/blob/d960b9e38c062584ff6cfb1a70e1512509a966e7/packages/react-scripts/config/env.js#L18-L23}
|
|
12
16
|
*/
|
|
13
17
|
if (!process.env.NODE_ENV) throw new Error("The NODE_ENV environment variable is required but was not specified.");
|
|
14
18
|
const env = options?.env || process.env.NODE_ENV;
|
|
15
|
-
const
|
|
16
|
-
/**
|
|
17
|
-
* @see {@linkplain https://github.com/facebook/create-react-app/blob/d960b9e38c062584ff6cfb1a70e1512509a966e7/packages/react-scripts/config/env.js#L25-L34}
|
|
18
|
-
*/
|
|
19
|
+
const dotenvPaths = options?.path ? [options.path] : [(0, node_path.resolve)(process.cwd(), "src", ".env"), (0, node_path.resolve)(process.cwd(), ".env")];
|
|
19
20
|
const dotenvFiles = [
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
21
|
+
`.${env}.local`,
|
|
22
|
+
...process.env.NODE_ENV === "test" ? [] : [".local"],
|
|
23
|
+
`.${env}`,
|
|
24
|
+
""
|
|
25
|
+
].flatMap((suffix) => dotenvPaths.map((path) => appendSuffix(path, suffix)));
|
|
25
26
|
/**
|
|
26
27
|
* @see {@linkplain https://github.com/facebook/create-react-app/blob/d960b9e38c062584ff6cfb1a70e1512509a966e7/packages/react-scripts/config/env.js#L36-L49}
|
|
27
28
|
*/
|
|
@@ -62,6 +63,11 @@ function loadEnvFiles(options) {
|
|
|
62
63
|
}
|
|
63
64
|
return { parsed };
|
|
64
65
|
}
|
|
66
|
+
function appendSuffix(path, suffix) {
|
|
67
|
+
if (typeof path === "string") return `${path}${suffix}`;
|
|
68
|
+
const file = (0, node_url.fileURLToPath)(path);
|
|
69
|
+
return (0, node_path.join)((0, node_path.dirname)(file), `${(0, node_path.basename)(file)}${suffix}`);
|
|
70
|
+
}
|
|
65
71
|
|
|
66
72
|
//#endregion
|
|
67
73
|
//#region src/lib/utils.ts
|
|
@@ -242,4 +248,4 @@ Object.defineProperty(exports, 'setup', {
|
|
|
242
248
|
return setup;
|
|
243
249
|
}
|
|
244
250
|
});
|
|
245
|
-
//# sourceMappingURL=src-
|
|
251
|
+
//# sourceMappingURL=src-5_ZeHgLf.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"src-
|
|
1
|
+
{"version":3,"file":"src-5_ZeHgLf.cjs","names":["fileURLToPath"],"sources":["../../src/lib/utils.ts","../../src/lib/setup.ts"],"sourcesContent":["import type { Env, EnvAny, EnvArray, EnvBoolean, EnvInteger, EnvNumber, EnvString } from './types';\n\nexport function envParseInteger(key: EnvInteger, defaultValue?: number): number;\nexport function envParseInteger(key: EnvInteger, defaultValue: number | null): number | null;\nexport function envParseInteger(key: EnvInteger, defaultValue?: number | null): number | null {\n\tconst value = process.env[key];\n\tif (!value) {\n\t\tif (defaultValue === undefined) throw new ReferenceError(`[ENV] ${key} - The key must be an integer, but is empty or undefined.`);\n\t\treturn defaultValue;\n\t}\n\n\tconst integer = Number(value);\n\tif (Number.isInteger(integer)) return integer;\n\tthrow new TypeError(`[ENV] ${key} - The key must be an integer, but received '${value}'.`);\n}\n\nexport function envParseNumber(key: EnvNumber, defaultValue?: number): number;\nexport function envParseNumber(key: EnvNumber, defaultValue: number | null): number | null;\nexport function envParseNumber(key: EnvNumber, defaultValue?: number | null): number | null {\n\tconst value = process.env[key];\n\tif (!value) {\n\t\tif (defaultValue === undefined) throw new ReferenceError(`[ENV] ${key} - The key must be a number, but is empty or undefined.`);\n\t\treturn defaultValue;\n\t}\n\n\tconst integer = Number(value);\n\tif (!Number.isNaN(integer)) return integer;\n\tthrow new TypeError(`[ENV] ${key} - The key must be a number, but received '${value}'.`);\n}\n\nexport function envParseBoolean(key: EnvBoolean, defaultValue?: boolean): boolean;\nexport function envParseBoolean(key: EnvBoolean, defaultValue: boolean | null): boolean | null;\nexport function envParseBoolean(key: EnvBoolean, defaultValue?: boolean | null): boolean | null {\n\tconst value = process.env[key];\n\tif (!value) {\n\t\tif (defaultValue === undefined) throw new ReferenceError(`[ENV] ${key} - The key must be a boolean, but is empty or undefined.`);\n\t\treturn defaultValue;\n\t}\n\n\tif (value.length === 4 && value.toLowerCase() === 'true') return true;\n\tif (value.length === 5 && value.toLowerCase() === 'false') return false;\n\tthrow new TypeError(`[ENV] ${key} - The key must be a boolean, but received '${value}'.`);\n}\n\nexport function envParseString<K extends EnvString>(key: K, defaultValue?: Env[K]): NonNullable<Env[K]>;\nexport function envParseString<K extends EnvString>(key: K, defaultValue: Env[K] | null): NonNullable<Env[K]> | null;\nexport function envParseString<K extends EnvString>(key: K, defaultValue?: Env[K] | null): NonNullable<Env[K]> | null {\n\tconst value = process.env[key];\n\tif (!value) {\n\t\tif (defaultValue === undefined) throw new ReferenceError(`[ENV] ${key} - The key must be a string, but is empty or undefined.`);\n\t\treturn defaultValue!;\n\t}\n\n\treturn value!;\n}\n\nexport function envParseArray(key: EnvArray, defaultValue?: string[]): string[];\nexport function envParseArray(key: EnvArray, defaultValue: string[] | null): string[] | null;\nexport function envParseArray(key: EnvArray, defaultValue?: string[] | null): string[] | null {\n\tconst value = process.env[key];\n\tif (!value) {\n\t\tif (defaultValue === undefined) throw new ReferenceError(`[ENV] ${key} - The key must be an array, but is empty or undefined.`);\n\t\treturn defaultValue;\n\t}\n\n\treturn value.split(' ');\n}\n\n/**\n * Checks if the value of the specified environment variable is null.\n * @param key - The name of the environment variable.\n * @returns Whether the value of the specified environment variable is:\n * - The string `\"0\"`\n * - The string `\"null\"`, case insensitive\n */\nexport function envIsNull(key: EnvAny): boolean {\n\tconst value = process.env[key];\n\treturn typeof value === 'string' && (value === '0' || (value.length === 4 && value.toLowerCase() === 'null'));\n}\n\n/**\n * Checks if the value of the specified environment variable is undefined.\n * @param key - The name of the environment variable.\n * @returns Whether the value of the specified environment variable is:\n * - `undefined`\n * - An empty string (`\"\"`)\n */\nexport function envIsUndefined(key: EnvAny): boolean {\n\tconst value = process.env[key];\n\treturn value === undefined || value.length === 0;\n}\n\n/**\n * Checks if the value of the specified environment variable is nullish.\n * @param key - The name of the environment variable.\n * @returns Whether the value of the specified environment variable is:\n * - `undefined`\n * - An empty string (`\"\"`)\n * - The string `\"0\"`\n * - The string `\"null\"`, case insensitive\n */\nexport function envIsNullish(key: EnvAny): boolean {\n\treturn envIsUndefined(key) || envIsNull(key);\n}\n\n/**\n * Checks if any of the specified environment variables is defined.\n * @param key - The name of the environment variable.\n * @returns Whether the value of any of the specified environment variables is a non-empty string\n */\nexport function envIsDefined(...keys: readonly EnvAny[]): boolean {\n\treturn keys.every((key) => !envIsUndefined(key));\n}\n","import type { DotenvConfigOutput } from 'dotenv';\nimport { fileURLToPath } from 'node:url';\nimport { loadEnvFiles, type EnvLoaderOptions } from './env-loader';\nimport { envIsDefined, envParseBoolean } from './utils';\n\nexport function setup(pathOrOptions?: string | URL | EnvSetupOptions): DotenvConfigOutput {\n\t// Unless explicitly defined, set NODE_ENV as development:\n\tprocess.env.NODE_ENV ??= 'development';\n\n\tlet options: EnvLoaderOptions;\n\tif (typeof pathOrOptions === 'undefined') {\n\t\toptions = {};\n\t} else if (typeof pathOrOptions === 'string') {\n\t\toptions = { path: pathOrOptions };\n\t} else if (typeof pathOrOptions === 'object') {\n\t\tif (pathOrOptions instanceof URL) {\n\t\t\toptions = { path: fileURLToPath(pathOrOptions) };\n\t\t} else {\n\t\t\tconst { path, ...rest } = pathOrOptions;\n\t\t\toptions = { path: typeof path === 'undefined' ? path : typeof path === 'string' ? path : fileURLToPath(path), ...rest };\n\t\t}\n\t} else {\n\t\tthrow new TypeError('Expected undefined, string, URL, or EnvSetupOptions');\n\t}\n\n\treturn loadEnvFiles({\n\t\tdebug: envIsDefined('DOTENV_DEBUG') ? envParseBoolean('DOTENV_DEBUG') : undefined,\n\t\tencoding: process.env.DOTENV_ENCODING,\n\t\tenv: process.env.DOTENV_ENV,\n\t\tpath: process.env.DOTENV_PATH,\n\t\tprefix: process.env.DOTENV_PREFIX,\n\t\t...options\n\t});\n}\n\nexport interface EnvSetupOptions extends Omit<EnvLoaderOptions, 'path'> {\n\t/**\n\t * You may specify a custom path if your file containing environment variables is located elsewhere.\n\t */\n\tpath?: string | URL;\n}\n\nexport type EnvSetupResult = DotenvConfigOutput;\n"],"mappings":"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"}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ interface EnvLoaderOptions extends Omit<DotenvConfigOptions, 'path'> {
|
|
|
13
13
|
* Specify a custom path if your file containing environment variables is located elsewhere.
|
|
14
14
|
* Can also be an array of strings, specifying multiple paths.
|
|
15
15
|
*
|
|
16
|
-
* @default `
|
|
16
|
+
* @default `src/.env*`, then `.env*` in `process.cwd()`
|
|
17
17
|
*
|
|
18
18
|
* @example with CJS
|
|
19
19
|
* ```typescript
|
|
@@ -31,29 +31,29 @@ interface EnvLoaderOptions extends Omit<DotenvConfigOptions, 'path'> {
|
|
|
31
31
|
}
|
|
32
32
|
//#endregion
|
|
33
33
|
//#region src/lib/setup.d.ts
|
|
34
|
-
declare function setup(pathOrOptions?: string | URL | EnvSetupOptions): DotenvConfigOutput;
|
|
35
|
-
interface EnvSetupOptions extends Omit<EnvLoaderOptions, 'path'> {
|
|
34
|
+
export declare function setup(pathOrOptions?: string | URL | EnvSetupOptions): DotenvConfigOutput;
|
|
35
|
+
export interface EnvSetupOptions extends Omit<EnvLoaderOptions, 'path'> {
|
|
36
36
|
/**
|
|
37
37
|
* You may specify a custom path if your file containing environment variables is located elsewhere.
|
|
38
38
|
*/
|
|
39
39
|
path?: string | URL;
|
|
40
40
|
}
|
|
41
|
-
type EnvSetupResult = DotenvConfigOutput;
|
|
41
|
+
export type EnvSetupResult = DotenvConfigOutput;
|
|
42
42
|
//#endregion
|
|
43
43
|
//#region src/lib/types.d.ts
|
|
44
|
-
type BooleanString = 'true' | 'false';
|
|
45
|
-
type IntegerString = `${bigint}`;
|
|
46
|
-
type NumberString = `${number}`;
|
|
47
|
-
type ArrayString = string & {
|
|
44
|
+
export type BooleanString = 'true' | 'false';
|
|
45
|
+
export type IntegerString = `${bigint}`;
|
|
46
|
+
export type NumberString = `${number}`;
|
|
47
|
+
export type ArrayString = string & {
|
|
48
48
|
__type__: 'ArrayString';
|
|
49
49
|
};
|
|
50
|
-
type EnvAny = keyof Env;
|
|
51
|
-
type EnvString = { [K in EnvAny]: Env[K] extends BooleanString | IntegerString | NumberString ? never : K; }[EnvAny];
|
|
52
|
-
type EnvBoolean = { [K in EnvAny]: Env[K] extends BooleanString | undefined ? K : never; }[EnvAny];
|
|
53
|
-
type EnvInteger = { [K in EnvAny]: Env[K] extends IntegerString | undefined ? K : never; }[EnvAny];
|
|
54
|
-
type EnvNumber = { [K in EnvAny]: Env[K] extends NumberString | undefined ? K : never; }[EnvAny];
|
|
55
|
-
type EnvArray = { [K in EnvAny]: Env[K] extends ArrayString | undefined ? K : never; }[EnvAny];
|
|
56
|
-
interface Env {
|
|
50
|
+
export type EnvAny = keyof Env;
|
|
51
|
+
export type EnvString = { [K in EnvAny]: Env[K] extends BooleanString | IntegerString | NumberString ? never : K; }[EnvAny];
|
|
52
|
+
export type EnvBoolean = { [K in EnvAny]: Env[K] extends BooleanString | undefined ? K : never; }[EnvAny];
|
|
53
|
+
export type EnvInteger = { [K in EnvAny]: Env[K] extends IntegerString | undefined ? K : never; }[EnvAny];
|
|
54
|
+
export type EnvNumber = { [K in EnvAny]: Env[K] extends NumberString | undefined ? K : never; }[EnvAny];
|
|
55
|
+
export type EnvArray = { [K in EnvAny]: Env[K] extends ArrayString | undefined ? K : never; }[EnvAny];
|
|
56
|
+
export interface Env {
|
|
57
57
|
NODE_ENV: 'test' | 'development' | 'production';
|
|
58
58
|
DOTENV_DEBUG?: BooleanString;
|
|
59
59
|
DOTENV_ENCODING?: string;
|
|
@@ -63,16 +63,16 @@ interface Env {
|
|
|
63
63
|
}
|
|
64
64
|
//#endregion
|
|
65
65
|
//#region src/lib/utils.d.ts
|
|
66
|
-
declare function envParseInteger(key: EnvInteger, defaultValue?: number): number;
|
|
67
|
-
declare function envParseInteger(key: EnvInteger, defaultValue: number | null): number | null;
|
|
68
|
-
declare function envParseNumber(key: EnvNumber, defaultValue?: number): number;
|
|
69
|
-
declare function envParseNumber(key: EnvNumber, defaultValue: number | null): number | null;
|
|
70
|
-
declare function envParseBoolean(key: EnvBoolean, defaultValue?: boolean): boolean;
|
|
71
|
-
declare function envParseBoolean(key: EnvBoolean, defaultValue: boolean | null): boolean | null;
|
|
72
|
-
declare function envParseString<K extends EnvString>(key: K, defaultValue?: Env[K]): NonNullable<Env[K]>;
|
|
73
|
-
declare function envParseString<K extends EnvString>(key: K, defaultValue: Env[K] | null): NonNullable<Env[K]> | null;
|
|
74
|
-
declare function envParseArray(key: EnvArray, defaultValue?: string[]): string[];
|
|
75
|
-
declare function envParseArray(key: EnvArray, defaultValue: string[] | null): string[] | null;
|
|
66
|
+
export declare function envParseInteger(key: EnvInteger, defaultValue?: number): number;
|
|
67
|
+
export declare function envParseInteger(key: EnvInteger, defaultValue: number | null): number | null;
|
|
68
|
+
export declare function envParseNumber(key: EnvNumber, defaultValue?: number): number;
|
|
69
|
+
export declare function envParseNumber(key: EnvNumber, defaultValue: number | null): number | null;
|
|
70
|
+
export declare function envParseBoolean(key: EnvBoolean, defaultValue?: boolean): boolean;
|
|
71
|
+
export declare function envParseBoolean(key: EnvBoolean, defaultValue: boolean | null): boolean | null;
|
|
72
|
+
export declare function envParseString<K extends EnvString>(key: K, defaultValue?: Env[K]): NonNullable<Env[K]>;
|
|
73
|
+
export declare function envParseString<K extends EnvString>(key: K, defaultValue: Env[K] | null): NonNullable<Env[K]> | null;
|
|
74
|
+
export declare function envParseArray(key: EnvArray, defaultValue?: string[]): string[];
|
|
75
|
+
export declare function envParseArray(key: EnvArray, defaultValue: string[] | null): string[] | null;
|
|
76
76
|
/**
|
|
77
77
|
* Checks if the value of the specified environment variable is null.
|
|
78
78
|
* @param key - The name of the environment variable.
|
|
@@ -80,7 +80,7 @@ declare function envParseArray(key: EnvArray, defaultValue: string[] | null): st
|
|
|
80
80
|
* - The string `"0"`
|
|
81
81
|
* - The string `"null"`, case insensitive
|
|
82
82
|
*/
|
|
83
|
-
declare function envIsNull(key: EnvAny): boolean;
|
|
83
|
+
export declare function envIsNull(key: EnvAny): boolean;
|
|
84
84
|
/**
|
|
85
85
|
* Checks if the value of the specified environment variable is undefined.
|
|
86
86
|
* @param key - The name of the environment variable.
|
|
@@ -88,7 +88,7 @@ declare function envIsNull(key: EnvAny): boolean;
|
|
|
88
88
|
* - `undefined`
|
|
89
89
|
* - An empty string (`""`)
|
|
90
90
|
*/
|
|
91
|
-
declare function envIsUndefined(key: EnvAny): boolean;
|
|
91
|
+
export declare function envIsUndefined(key: EnvAny): boolean;
|
|
92
92
|
/**
|
|
93
93
|
* Checks if the value of the specified environment variable is nullish.
|
|
94
94
|
* @param key - The name of the environment variable.
|
|
@@ -98,13 +98,13 @@ declare function envIsUndefined(key: EnvAny): boolean;
|
|
|
98
98
|
* - The string `"0"`
|
|
99
99
|
* - The string `"null"`, case insensitive
|
|
100
100
|
*/
|
|
101
|
-
declare function envIsNullish(key: EnvAny): boolean;
|
|
101
|
+
export declare function envIsNullish(key: EnvAny): boolean;
|
|
102
102
|
/**
|
|
103
103
|
* Checks if any of the specified environment variables is defined.
|
|
104
104
|
* @param key - The name of the environment variable.
|
|
105
105
|
* @returns Whether the value of any of the specified environment variables is a non-empty string
|
|
106
106
|
*/
|
|
107
|
-
declare function envIsDefined(...keys: readonly EnvAny[]): boolean;
|
|
107
|
+
export declare function envIsDefined(...keys: readonly EnvAny[]): boolean;
|
|
108
108
|
//#endregion
|
|
109
109
|
//#region src/index.d.ts
|
|
110
110
|
declare global {
|
|
@@ -113,5 +113,4 @@ declare global {
|
|
|
113
113
|
}
|
|
114
114
|
}
|
|
115
115
|
//#endregion
|
|
116
|
-
export { ArrayString, BooleanString, Env, EnvAny, EnvArray, EnvBoolean, EnvInteger, EnvNumber, EnvSetupOptions, EnvSetupResult, EnvString, IntegerString, NumberString, envIsDefined, envIsNull, envIsNullish, envIsUndefined, envParseArray, envParseBoolean, envParseInteger, envParseNumber, envParseString, setup };
|
|
117
116
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/esm/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/lib/env-loader.ts","../../src/lib/setup.ts","../../src/lib/types.ts","../../src/lib/utils.ts","../../src/index.ts"],"mappings":";;
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/lib/env-loader.ts","../../src/lib/setup.ts","../../src/lib/types.ts","../../src/lib/utils.ts","../../src/index.ts"],"mappings":";;UAMiB,yBAAyB,KAAK;;;;EAI9C;;;;EAIA;;;;;;;;;;;;;;;;;;;EAmBA,gBAAgB;;;;wBC5BD,MAAM,yBAAyB,MAAM,kBAAkB;iBA8BtD,wBAAwB,KAAK;;;;EAI7C,gBAAgB;;YAGL,iBAAiB;;;YC1CjB;YACA;YACA;YACA;EAAyB;;YAEzB,eAAe;YACf,eAAe,KAAK,SAAS,IAAI,WAAW,gBAAgB,gBAAgB,uBAAuB,KAAI;YACvG,gBAAgB,KAAK,SAAS,IAAI,WAAW,4BAA4B,aAAY;YACrF,gBAAgB,KAAK,SAAS,IAAI,WAAW,4BAA4B,aAAY;YACrF,eAAe,KAAK,SAAS,IAAI,WAAW,2BAA2B,aAAY;YACnF,cAAc,KAAK,SAAS,IAAI,WAAW,0BAA0B,aAAY;iBAE5E;EAChB;EACA,eAAe;EACf;EACA;EACA;EACA;;;;wBChBe,gBAAgB,KAAK,YAAY;wBACjC,gBAAgB,KAAK,YAAY;wBAajC,eAAe,KAAK,WAAW;wBAC/B,eAAe,KAAK,WAAW;wBAa/B,gBAAgB,KAAK,YAAY;wBACjC,gBAAgB,KAAK,YAAY;wBAajC,eAAe,UAAU,WAAW,KAAK,GAAG,eAAe,IAAI,KAAK,YAAY,IAAI;wBACpF,eAAe,UAAU,WAAW,KAAK,GAAG,cAAc,IAAI,YAAY,YAAY,IAAI;wBAW1F,cAAc,KAAK,UAAU;wBAC7B,cAAc,KAAK,UAAU;;;;;;;;wBAkB7B,UAAU,KAAK;;;;;;;;wBAYf,eAAe,KAAK;;;;;;;;;;wBAcpB,aAAa,KAAK;;;;;;wBASlB,gBAAgB,eAAe;;;QCxGvC;YACG;cACC,mBAAmB"}
|
package/dist/esm/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { a as envIsUndefined, c as envParseInteger, i as envIsNullish, l as envParseNumber, n as envIsDefined, o as envParseArray, r as envIsNull, s as envParseBoolean, t as setup, u as envParseString } from "./src-
|
|
1
|
+
import { a as envIsUndefined, c as envParseInteger, i as envIsNullish, l as envParseNumber, n as envIsDefined, o as envParseArray, r as envIsNull, s as envParseBoolean, t as setup, u as envParseString } from "./src-DbngwUYN.js";
|
|
2
2
|
|
|
3
3
|
export { envIsDefined, envIsNull, envIsNullish, envIsUndefined, envParseArray, envParseBoolean, envParseInteger, envParseNumber, envParseString, setup };
|
package/dist/esm/setup.js
CHANGED
|
@@ -1,27 +1,28 @@
|
|
|
1
1
|
import { fileURLToPath } from "node:url";
|
|
2
|
+
import { container } from "@sapphire/pieces";
|
|
2
3
|
import { config } from "dotenv";
|
|
3
4
|
import { expand } from "dotenv-expand";
|
|
4
|
-
import { basename, resolve } from "node:path";
|
|
5
|
+
import { basename, dirname, join, resolve } from "node:path";
|
|
5
6
|
|
|
6
7
|
//#region src/lib/env-loader.ts
|
|
7
|
-
const packageVersion = "2.
|
|
8
|
+
const packageVersion = "2.1.1";
|
|
9
|
+
function resolveDebugLogger() {
|
|
10
|
+
return container.logger ?? console;
|
|
11
|
+
}
|
|
8
12
|
function loadEnvFiles(options) {
|
|
9
|
-
const log = options?.debug ? (message) =>
|
|
13
|
+
const log = options?.debug ? (message) => resolveDebugLogger().debug(`[@wolfstar/env-utilities@${packageVersion}] ${message}`) : () => void 0;
|
|
10
14
|
/**
|
|
11
15
|
* @see {@linkplain https://github.com/facebook/create-react-app/blob/d960b9e38c062584ff6cfb1a70e1512509a966e7/packages/react-scripts/config/env.js#L18-L23}
|
|
12
16
|
*/
|
|
13
17
|
if (!process.env.NODE_ENV) throw new Error("The NODE_ENV environment variable is required but was not specified.");
|
|
14
18
|
const env = options?.env || process.env.NODE_ENV;
|
|
15
|
-
const
|
|
16
|
-
/**
|
|
17
|
-
* @see {@linkplain https://github.com/facebook/create-react-app/blob/d960b9e38c062584ff6cfb1a70e1512509a966e7/packages/react-scripts/config/env.js#L25-L34}
|
|
18
|
-
*/
|
|
19
|
+
const dotenvPaths = options?.path ? [options.path] : [resolve(process.cwd(), "src", ".env"), resolve(process.cwd(), ".env")];
|
|
19
20
|
const dotenvFiles = [
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
21
|
+
`.${env}.local`,
|
|
22
|
+
...process.env.NODE_ENV === "test" ? [] : [".local"],
|
|
23
|
+
`.${env}`,
|
|
24
|
+
""
|
|
25
|
+
].flatMap((suffix) => dotenvPaths.map((path) => appendSuffix(path, suffix)));
|
|
25
26
|
/**
|
|
26
27
|
* @see {@linkplain https://github.com/facebook/create-react-app/blob/d960b9e38c062584ff6cfb1a70e1512509a966e7/packages/react-scripts/config/env.js#L36-L49}
|
|
27
28
|
*/
|
|
@@ -62,6 +63,11 @@ function loadEnvFiles(options) {
|
|
|
62
63
|
}
|
|
63
64
|
return { parsed };
|
|
64
65
|
}
|
|
66
|
+
function appendSuffix(path, suffix) {
|
|
67
|
+
if (typeof path === "string") return `${path}${suffix}`;
|
|
68
|
+
const file = fileURLToPath(path);
|
|
69
|
+
return join(dirname(file), `${basename(file)}${suffix}`);
|
|
70
|
+
}
|
|
65
71
|
|
|
66
72
|
//#endregion
|
|
67
73
|
//#region src/lib/utils.ts
|
|
@@ -183,4 +189,4 @@ function setup(pathOrOptions) {
|
|
|
183
189
|
|
|
184
190
|
//#endregion
|
|
185
191
|
export { envIsUndefined as a, envParseInteger as c, envIsNullish as i, envParseNumber as l, envIsDefined as n, envParseArray as o, envIsNull as r, envParseBoolean as s, setup as t, envParseString as u };
|
|
186
|
-
//# sourceMappingURL=src-
|
|
192
|
+
//# sourceMappingURL=src-DbngwUYN.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"src-
|
|
1
|
+
{"version":3,"file":"src-DbngwUYN.js","names":[],"sources":["../../src/lib/utils.ts","../../src/lib/setup.ts"],"sourcesContent":["import type { Env, EnvAny, EnvArray, EnvBoolean, EnvInteger, EnvNumber, EnvString } from './types';\n\nexport function envParseInteger(key: EnvInteger, defaultValue?: number): number;\nexport function envParseInteger(key: EnvInteger, defaultValue: number | null): number | null;\nexport function envParseInteger(key: EnvInteger, defaultValue?: number | null): number | null {\n\tconst value = process.env[key];\n\tif (!value) {\n\t\tif (defaultValue === undefined) throw new ReferenceError(`[ENV] ${key} - The key must be an integer, but is empty or undefined.`);\n\t\treturn defaultValue;\n\t}\n\n\tconst integer = Number(value);\n\tif (Number.isInteger(integer)) return integer;\n\tthrow new TypeError(`[ENV] ${key} - The key must be an integer, but received '${value}'.`);\n}\n\nexport function envParseNumber(key: EnvNumber, defaultValue?: number): number;\nexport function envParseNumber(key: EnvNumber, defaultValue: number | null): number | null;\nexport function envParseNumber(key: EnvNumber, defaultValue?: number | null): number | null {\n\tconst value = process.env[key];\n\tif (!value) {\n\t\tif (defaultValue === undefined) throw new ReferenceError(`[ENV] ${key} - The key must be a number, but is empty or undefined.`);\n\t\treturn defaultValue;\n\t}\n\n\tconst integer = Number(value);\n\tif (!Number.isNaN(integer)) return integer;\n\tthrow new TypeError(`[ENV] ${key} - The key must be a number, but received '${value}'.`);\n}\n\nexport function envParseBoolean(key: EnvBoolean, defaultValue?: boolean): boolean;\nexport function envParseBoolean(key: EnvBoolean, defaultValue: boolean | null): boolean | null;\nexport function envParseBoolean(key: EnvBoolean, defaultValue?: boolean | null): boolean | null {\n\tconst value = process.env[key];\n\tif (!value) {\n\t\tif (defaultValue === undefined) throw new ReferenceError(`[ENV] ${key} - The key must be a boolean, but is empty or undefined.`);\n\t\treturn defaultValue;\n\t}\n\n\tif (value.length === 4 && value.toLowerCase() === 'true') return true;\n\tif (value.length === 5 && value.toLowerCase() === 'false') return false;\n\tthrow new TypeError(`[ENV] ${key} - The key must be a boolean, but received '${value}'.`);\n}\n\nexport function envParseString<K extends EnvString>(key: K, defaultValue?: Env[K]): NonNullable<Env[K]>;\nexport function envParseString<K extends EnvString>(key: K, defaultValue: Env[K] | null): NonNullable<Env[K]> | null;\nexport function envParseString<K extends EnvString>(key: K, defaultValue?: Env[K] | null): NonNullable<Env[K]> | null {\n\tconst value = process.env[key];\n\tif (!value) {\n\t\tif (defaultValue === undefined) throw new ReferenceError(`[ENV] ${key} - The key must be a string, but is empty or undefined.`);\n\t\treturn defaultValue!;\n\t}\n\n\treturn value!;\n}\n\nexport function envParseArray(key: EnvArray, defaultValue?: string[]): string[];\nexport function envParseArray(key: EnvArray, defaultValue: string[] | null): string[] | null;\nexport function envParseArray(key: EnvArray, defaultValue?: string[] | null): string[] | null {\n\tconst value = process.env[key];\n\tif (!value) {\n\t\tif (defaultValue === undefined) throw new ReferenceError(`[ENV] ${key} - The key must be an array, but is empty or undefined.`);\n\t\treturn defaultValue;\n\t}\n\n\treturn value.split(' ');\n}\n\n/**\n * Checks if the value of the specified environment variable is null.\n * @param key - The name of the environment variable.\n * @returns Whether the value of the specified environment variable is:\n * - The string `\"0\"`\n * - The string `\"null\"`, case insensitive\n */\nexport function envIsNull(key: EnvAny): boolean {\n\tconst value = process.env[key];\n\treturn typeof value === 'string' && (value === '0' || (value.length === 4 && value.toLowerCase() === 'null'));\n}\n\n/**\n * Checks if the value of the specified environment variable is undefined.\n * @param key - The name of the environment variable.\n * @returns Whether the value of the specified environment variable is:\n * - `undefined`\n * - An empty string (`\"\"`)\n */\nexport function envIsUndefined(key: EnvAny): boolean {\n\tconst value = process.env[key];\n\treturn value === undefined || value.length === 0;\n}\n\n/**\n * Checks if the value of the specified environment variable is nullish.\n * @param key - The name of the environment variable.\n * @returns Whether the value of the specified environment variable is:\n * - `undefined`\n * - An empty string (`\"\"`)\n * - The string `\"0\"`\n * - The string `\"null\"`, case insensitive\n */\nexport function envIsNullish(key: EnvAny): boolean {\n\treturn envIsUndefined(key) || envIsNull(key);\n}\n\n/**\n * Checks if any of the specified environment variables is defined.\n * @param key - The name of the environment variable.\n * @returns Whether the value of any of the specified environment variables is a non-empty string\n */\nexport function envIsDefined(...keys: readonly EnvAny[]): boolean {\n\treturn keys.every((key) => !envIsUndefined(key));\n}\n","import type { DotenvConfigOutput } from 'dotenv';\nimport { fileURLToPath } from 'node:url';\nimport { loadEnvFiles, type EnvLoaderOptions } from './env-loader';\nimport { envIsDefined, envParseBoolean } from './utils';\n\nexport function setup(pathOrOptions?: string | URL | EnvSetupOptions): DotenvConfigOutput {\n\t// Unless explicitly defined, set NODE_ENV as development:\n\tprocess.env.NODE_ENV ??= 'development';\n\n\tlet options: EnvLoaderOptions;\n\tif (typeof pathOrOptions === 'undefined') {\n\t\toptions = {};\n\t} else if (typeof pathOrOptions === 'string') {\n\t\toptions = { path: pathOrOptions };\n\t} else if (typeof pathOrOptions === 'object') {\n\t\tif (pathOrOptions instanceof URL) {\n\t\t\toptions = { path: fileURLToPath(pathOrOptions) };\n\t\t} else {\n\t\t\tconst { path, ...rest } = pathOrOptions;\n\t\t\toptions = { path: typeof path === 'undefined' ? path : typeof path === 'string' ? path : fileURLToPath(path), ...rest };\n\t\t}\n\t} else {\n\t\tthrow new TypeError('Expected undefined, string, URL, or EnvSetupOptions');\n\t}\n\n\treturn loadEnvFiles({\n\t\tdebug: envIsDefined('DOTENV_DEBUG') ? envParseBoolean('DOTENV_DEBUG') : undefined,\n\t\tencoding: process.env.DOTENV_ENCODING,\n\t\tenv: process.env.DOTENV_ENV,\n\t\tpath: process.env.DOTENV_PATH,\n\t\tprefix: process.env.DOTENV_PREFIX,\n\t\t...options\n\t});\n}\n\nexport interface EnvSetupOptions extends Omit<EnvLoaderOptions, 'path'> {\n\t/**\n\t * You may specify a custom path if your file containing environment variables is located elsewhere.\n\t */\n\tpath?: string | URL;\n}\n\nexport type EnvSetupResult = DotenvConfigOutput;\n"],"mappings":"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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wolfstar/env-utilities",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.1",
|
|
4
4
|
"description": "Functional utilities for reading and parsing environmental variables",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"api",
|
|
@@ -59,6 +59,7 @@
|
|
|
59
59
|
"provenance": true
|
|
60
60
|
},
|
|
61
61
|
"dependencies": {
|
|
62
|
+
"@sapphire/pieces": "^4.4.1",
|
|
62
63
|
"dotenv": "^17.4.2",
|
|
63
64
|
"dotenv-expand": "^13.0.0"
|
|
64
65
|
},
|
|
@@ -66,7 +67,7 @@
|
|
|
66
67
|
"@redstardev/unplugin-version-injector": "^0.0.2",
|
|
67
68
|
"@vitest/coverage-v8": "^4.1.11",
|
|
68
69
|
"golar": "^0.1.10",
|
|
69
|
-
"tsdown": "^0.
|
|
70
|
+
"tsdown": "^0.23.0",
|
|
70
71
|
"typescript": "~7.0.2",
|
|
71
72
|
"vitest": "^4.1.11"
|
|
72
73
|
},
|