@adonisjs/env 4.2.0-4 → 4.2.0-6
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 +1 -1
- package/build/chunk-NSHYMAZE.js +95 -0
- package/build/index.d.ts +247 -5
- package/build/index.js +251 -13
- package/build/src/editor.d.ts +3 -2
- package/build/src/editor.js +67 -67
- package/package.json +29 -19
- package/build/src/debug.d.ts +0 -3
- package/build/src/debug.js +0 -10
- package/build/src/env.d.ts +0 -82
- package/build/src/env.js +0 -80
- package/build/src/exceptions.d.ts +0 -28
- package/build/src/exceptions.js +0 -18
- package/build/src/loader.d.ts +0 -36
- package/build/src/loader.js +0 -134
- package/build/src/parser.d.ts +0 -51
- package/build/src/parser.js +0 -166
- package/build/src/processor.d.ts +0 -12
- package/build/src/processor.js +0 -68
- package/build/src/validator.d.ts +0 -25
- package/build/src/validator.js +0 -48
package/build/src/editor.js
CHANGED
|
@@ -1,67 +1,67 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
EnvLoader
|
|
3
|
+
} from "../chunk-NSHYMAZE.js";
|
|
4
|
+
|
|
5
|
+
// src/editor.ts
|
|
6
|
+
import splitLines from "split-lines";
|
|
7
|
+
import lodash from "@poppinss/utils/lodash";
|
|
8
|
+
import { writeFile } from "node:fs/promises";
|
|
9
|
+
var EnvEditor = class _EnvEditor {
|
|
10
|
+
#appRoot;
|
|
11
|
+
#loader;
|
|
12
|
+
#files = [];
|
|
13
|
+
/**
|
|
14
|
+
* Creates an instance of env editor and loads .env files
|
|
15
|
+
* contents.
|
|
16
|
+
*/
|
|
17
|
+
static async create(appRoot) {
|
|
18
|
+
const editor = new _EnvEditor(appRoot);
|
|
19
|
+
await editor.load();
|
|
20
|
+
return editor;
|
|
21
|
+
}
|
|
22
|
+
constructor(appRoot) {
|
|
23
|
+
this.#appRoot = appRoot;
|
|
24
|
+
this.#loader = new EnvLoader(this.#appRoot, true);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Loads .env files for editing. Only ".env" and ".env.example"
|
|
28
|
+
* files are picked for editing.
|
|
29
|
+
*/
|
|
30
|
+
async load() {
|
|
31
|
+
const envFiles = await this.#loader.load();
|
|
32
|
+
this.#files = envFiles.filter(
|
|
33
|
+
(envFile) => envFile.fileExists && (envFile.path.endsWith(".env") || envFile.path.endsWith(".env.example"))
|
|
34
|
+
).map((envFile) => {
|
|
35
|
+
return {
|
|
36
|
+
contents: splitLines(envFile.contents.trim()),
|
|
37
|
+
path: envFile.path
|
|
38
|
+
};
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Add key-value pair to the dot-env files.
|
|
43
|
+
*/
|
|
44
|
+
add(key, value) {
|
|
45
|
+
this.#files.forEach((file) => {
|
|
46
|
+
let entryIndex = file.contents.findIndex((line) => line.startsWith(`${key}=`));
|
|
47
|
+
entryIndex = entryIndex === -1 ? file.contents.length : entryIndex;
|
|
48
|
+
lodash.set(file.contents, entryIndex, `${key}=${String(value)}`);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
toJSON() {
|
|
52
|
+
return this.#files;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Save changes to the disk
|
|
56
|
+
*/
|
|
57
|
+
async save() {
|
|
58
|
+
await Promise.all(
|
|
59
|
+
this.#files.map((file) => {
|
|
60
|
+
return writeFile(file.path, file.contents.join("\n"));
|
|
61
|
+
})
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
export {
|
|
66
|
+
EnvEditor
|
|
67
|
+
};
|
package/package.json
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adonisjs/env",
|
|
3
|
-
"version": "4.2.0-
|
|
3
|
+
"version": "4.2.0-6",
|
|
4
4
|
"description": "Environment variable manager for Node.js",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"files": [
|
|
8
|
-
"build
|
|
9
|
-
"build/index.d.ts",
|
|
10
|
-
"build/index.js"
|
|
8
|
+
"build"
|
|
11
9
|
],
|
|
12
10
|
"exports": {
|
|
13
11
|
".": "./build/index.js",
|
|
@@ -21,7 +19,7 @@
|
|
|
21
19
|
"test": "cross-env NODE_DEBUG=adonisjs:env c8 npm run quick:test",
|
|
22
20
|
"clean": "del-cli build",
|
|
23
21
|
"typecheck": "tsc --noEmit",
|
|
24
|
-
"compile": "npm run lint && npm run clean &&
|
|
22
|
+
"compile": "npm run lint && npm run clean && tsup-node",
|
|
25
23
|
"build": "npm run compile",
|
|
26
24
|
"release": "np",
|
|
27
25
|
"version": "npm run build",
|
|
@@ -41,27 +39,28 @@
|
|
|
41
39
|
"@adonisjs/eslint-config": "^1.1.8",
|
|
42
40
|
"@adonisjs/prettier-config": "^1.1.8",
|
|
43
41
|
"@adonisjs/tsconfig": "^1.1.8",
|
|
44
|
-
"@commitlint/cli": "^17.
|
|
45
|
-
"@commitlint/config-conventional": "^17.
|
|
46
|
-
"@japa/assert": "^2.0.0
|
|
47
|
-
"@japa/expect-type": "^2.0.0
|
|
48
|
-
"@japa/file-system": "^2.0.0
|
|
49
|
-
"@japa/runner": "^3.0.
|
|
50
|
-
"@swc/core": "
|
|
51
|
-
"@types/node": "^20.
|
|
42
|
+
"@commitlint/cli": "^17.8.0",
|
|
43
|
+
"@commitlint/config-conventional": "^17.8.0",
|
|
44
|
+
"@japa/assert": "^2.0.0",
|
|
45
|
+
"@japa/expect-type": "^2.0.0",
|
|
46
|
+
"@japa/file-system": "^2.0.0",
|
|
47
|
+
"@japa/runner": "^3.0.2",
|
|
48
|
+
"@swc/core": "1.3.82",
|
|
49
|
+
"@types/node": "^20.8.6",
|
|
52
50
|
"c8": "^8.0.1",
|
|
53
51
|
"cross-env": "^7.0.3",
|
|
54
|
-
"del-cli": "^5.
|
|
55
|
-
"eslint": "^8.
|
|
52
|
+
"del-cli": "^5.1.0",
|
|
53
|
+
"eslint": "^8.51.0",
|
|
56
54
|
"github-label-sync": "^2.3.1",
|
|
57
55
|
"husky": "^8.0.3",
|
|
58
56
|
"np": "^8.0.4",
|
|
59
|
-
"prettier": "^3.0.
|
|
57
|
+
"prettier": "^3.0.3",
|
|
60
58
|
"ts-node": "^10.9.1",
|
|
61
|
-
"
|
|
59
|
+
"tsup": "^7.2.0",
|
|
60
|
+
"typescript": "^5.2.2"
|
|
62
61
|
},
|
|
63
62
|
"dependencies": {
|
|
64
|
-
"@poppinss/utils": "^6.5.0
|
|
63
|
+
"@poppinss/utils": "^6.5.0",
|
|
65
64
|
"@poppinss/validator-lite": "^1.0.3",
|
|
66
65
|
"dotenv": "^16.3.1",
|
|
67
66
|
"split-lines": "^3.0.0"
|
|
@@ -101,5 +100,16 @@
|
|
|
101
100
|
"eslintConfig": {
|
|
102
101
|
"extends": "@adonisjs/eslint-config/package"
|
|
103
102
|
},
|
|
104
|
-
"prettier": "@adonisjs/prettier-config"
|
|
103
|
+
"prettier": "@adonisjs/prettier-config",
|
|
104
|
+
"tsup": {
|
|
105
|
+
"entry": [
|
|
106
|
+
"./index.ts",
|
|
107
|
+
"./src/editor.ts"
|
|
108
|
+
],
|
|
109
|
+
"outDir": "./build",
|
|
110
|
+
"clean": true,
|
|
111
|
+
"format": "esm",
|
|
112
|
+
"dts": true,
|
|
113
|
+
"target": "esnext"
|
|
114
|
+
}
|
|
105
115
|
}
|
package/build/src/debug.d.ts
DELETED
package/build/src/debug.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* @adonisjs/env
|
|
3
|
-
*
|
|
4
|
-
* (c) AdonisJS
|
|
5
|
-
*
|
|
6
|
-
* For the full copyright and license information, please view the LICENSE
|
|
7
|
-
* file that was distributed with this source code.
|
|
8
|
-
*/
|
|
9
|
-
import { debuglog } from 'node:util';
|
|
10
|
-
export default debuglog('adonisjs:env');
|
package/build/src/env.d.ts
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
/// <reference types="@types/node" resolution-mode="require"/>
|
|
2
|
-
import { type ValidateFn } from '@poppinss/validator-lite';
|
|
3
|
-
import { EnvValidator } from './validator.js';
|
|
4
|
-
/**
|
|
5
|
-
* A wrapper over "process.env" with types information.
|
|
6
|
-
*
|
|
7
|
-
* ```ts
|
|
8
|
-
* const validate = Env.rules({
|
|
9
|
-
* PORT: Env.schema.number()
|
|
10
|
-
* })
|
|
11
|
-
*
|
|
12
|
-
* const validatedEnvVars = validate(process.env)
|
|
13
|
-
*
|
|
14
|
-
* const env = new EnvValues(validatedEnvVars)
|
|
15
|
-
* env.get('PORT') // type === number
|
|
16
|
-
* ```
|
|
17
|
-
*/
|
|
18
|
-
export declare class Env<EnvValues extends Record<string, any>> {
|
|
19
|
-
#private;
|
|
20
|
-
constructor(values: EnvValues);
|
|
21
|
-
/**
|
|
22
|
-
* Create an instance of the env class by validating the
|
|
23
|
-
* environment variables. Also, the `.env` files are
|
|
24
|
-
* loaded from the appRoot
|
|
25
|
-
*/
|
|
26
|
-
static create<Schema extends {
|
|
27
|
-
[key: string]: ValidateFn<unknown>;
|
|
28
|
-
}>(appRoot: URL, schema: Schema): Promise<Env<{
|
|
29
|
-
[K in keyof Schema]: ReturnType<Schema[K]>;
|
|
30
|
-
}>>;
|
|
31
|
-
/**
|
|
32
|
-
* The schema builder for defining validation rules
|
|
33
|
-
*/
|
|
34
|
-
static schema: {
|
|
35
|
-
number: typeof import("@poppinss/validator-lite/build/src/schema/number.js").number;
|
|
36
|
-
string: typeof import("@poppinss/validator-lite/build/src/schema/string.js").string;
|
|
37
|
-
boolean: typeof import("@poppinss/validator-lite/build/src/schema/boolean.js").boolean;
|
|
38
|
-
enum: typeof import("@poppinss/validator-lite/build/src/schema/oneOf.js").oneOf;
|
|
39
|
-
};
|
|
40
|
-
/**
|
|
41
|
-
* Define the validation rules for validating environment
|
|
42
|
-
* variables. The return value is an instance of the
|
|
43
|
-
* env validator
|
|
44
|
-
*/
|
|
45
|
-
static rules<T extends {
|
|
46
|
-
[key: string]: ValidateFn<unknown>;
|
|
47
|
-
}>(schema: T): EnvValidator<T>;
|
|
48
|
-
/**
|
|
49
|
-
* Get the value of an environment variable by key. The values are
|
|
50
|
-
* lookedup inside the validated environment and "process.env"
|
|
51
|
-
* is used as a fallback.
|
|
52
|
-
*
|
|
53
|
-
* The second param is the default value, which is returned when
|
|
54
|
-
* the environment variable does not exist.
|
|
55
|
-
*
|
|
56
|
-
* ```ts
|
|
57
|
-
* Env.get('PORT')
|
|
58
|
-
*
|
|
59
|
-
* // With default value
|
|
60
|
-
* Env.get('PORT', 3000)
|
|
61
|
-
* ```
|
|
62
|
-
*/
|
|
63
|
-
get<K extends keyof EnvValues>(key: K): EnvValues[K];
|
|
64
|
-
get<K extends keyof EnvValues>(key: K, defaultValue: Exclude<EnvValues[K], undefined>): Exclude<EnvValues[K], undefined>;
|
|
65
|
-
get(key: string): string | undefined;
|
|
66
|
-
get(key: string, defaultValue: string): string;
|
|
67
|
-
/**
|
|
68
|
-
* Update/set value of an environment variable.
|
|
69
|
-
*
|
|
70
|
-
* The value is not casted/validated using the validator, so make sure
|
|
71
|
-
* to set the correct data type.
|
|
72
|
-
*
|
|
73
|
-
* ```ts
|
|
74
|
-
* Env.set('PORT', 3000)
|
|
75
|
-
*
|
|
76
|
-
* Env.get('PORT') === 3000 // true
|
|
77
|
-
* process.env.PORT === '3000' // true
|
|
78
|
-
* ```
|
|
79
|
-
*/
|
|
80
|
-
set<K extends keyof EnvValues>(key: K, value: EnvValues[K]): void;
|
|
81
|
-
set(key: string, value: string): void;
|
|
82
|
-
}
|
package/build/src/env.js
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* @adonisjs/env
|
|
3
|
-
*
|
|
4
|
-
* (c) AdonisJS
|
|
5
|
-
*
|
|
6
|
-
* For the full copyright and license information, please view the LICENSE
|
|
7
|
-
* file that was distributed with this source code.
|
|
8
|
-
*/
|
|
9
|
-
import { schema as envSchema } from '@poppinss/validator-lite';
|
|
10
|
-
import { EnvValidator } from './validator.js';
|
|
11
|
-
import { EnvProcessor } from './processor.js';
|
|
12
|
-
/**
|
|
13
|
-
* A wrapper over "process.env" with types information.
|
|
14
|
-
*
|
|
15
|
-
* ```ts
|
|
16
|
-
* const validate = Env.rules({
|
|
17
|
-
* PORT: Env.schema.number()
|
|
18
|
-
* })
|
|
19
|
-
*
|
|
20
|
-
* const validatedEnvVars = validate(process.env)
|
|
21
|
-
*
|
|
22
|
-
* const env = new EnvValues(validatedEnvVars)
|
|
23
|
-
* env.get('PORT') // type === number
|
|
24
|
-
* ```
|
|
25
|
-
*/
|
|
26
|
-
export class Env {
|
|
27
|
-
/**
|
|
28
|
-
* A cache of env values
|
|
29
|
-
*/
|
|
30
|
-
#values;
|
|
31
|
-
constructor(values) {
|
|
32
|
-
this.#values = values;
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Create an instance of the env class by validating the
|
|
36
|
-
* environment variables. Also, the `.env` files are
|
|
37
|
-
* loaded from the appRoot
|
|
38
|
-
*/
|
|
39
|
-
static async create(appRoot, schema) {
|
|
40
|
-
const values = await new EnvProcessor(appRoot).process();
|
|
41
|
-
const validator = this.rules(schema);
|
|
42
|
-
return new Env(validator.validate(values));
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* The schema builder for defining validation rules
|
|
46
|
-
*/
|
|
47
|
-
static schema = envSchema;
|
|
48
|
-
/**
|
|
49
|
-
* Define the validation rules for validating environment
|
|
50
|
-
* variables. The return value is an instance of the
|
|
51
|
-
* env validator
|
|
52
|
-
*/
|
|
53
|
-
static rules(schema) {
|
|
54
|
-
const validator = new EnvValidator(schema);
|
|
55
|
-
return validator;
|
|
56
|
-
}
|
|
57
|
-
get(key, defaultValue) {
|
|
58
|
-
/**
|
|
59
|
-
* Return cached value
|
|
60
|
-
*/
|
|
61
|
-
if (this.#values[key] !== undefined) {
|
|
62
|
-
return this.#values[key];
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Get value from "process.env" and update the cache
|
|
66
|
-
*/
|
|
67
|
-
const envValue = process.env[key];
|
|
68
|
-
if (envValue) {
|
|
69
|
-
return envValue;
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Return default value when unable to lookup any other value
|
|
73
|
-
*/
|
|
74
|
-
return defaultValue;
|
|
75
|
-
}
|
|
76
|
-
set(key, value) {
|
|
77
|
-
this.#values[key] = value;
|
|
78
|
-
process.env[key] = value;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
/// <reference types="@types/node" resolution-mode="require"/>
|
|
2
|
-
/**
|
|
3
|
-
* Exception raised when one or more env variables
|
|
4
|
-
* are invalid
|
|
5
|
-
*/
|
|
6
|
-
export declare const E_INVALID_ENV_VARIABLES: {
|
|
7
|
-
new (message?: string | undefined, options?: (ErrorOptions & {
|
|
8
|
-
code?: string | undefined;
|
|
9
|
-
status?: number | undefined;
|
|
10
|
-
}) | undefined): {
|
|
11
|
-
help: string;
|
|
12
|
-
name: string;
|
|
13
|
-
code?: string | undefined;
|
|
14
|
-
status: number;
|
|
15
|
-
toString(): string;
|
|
16
|
-
readonly [Symbol.toStringTag]: string;
|
|
17
|
-
message: string;
|
|
18
|
-
stack?: string | undefined;
|
|
19
|
-
cause?: unknown;
|
|
20
|
-
};
|
|
21
|
-
message: string;
|
|
22
|
-
code: string;
|
|
23
|
-
help?: string | undefined;
|
|
24
|
-
status?: number | undefined;
|
|
25
|
-
captureStackTrace(targetObject: object, constructorOpt?: Function | undefined): void;
|
|
26
|
-
prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined;
|
|
27
|
-
stackTraceLimit: number;
|
|
28
|
-
};
|
package/build/src/exceptions.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* @adonisjs/env
|
|
3
|
-
*
|
|
4
|
-
* (c) AdonisJS
|
|
5
|
-
*
|
|
6
|
-
* For the full copyright and license information, please view the LICENSE
|
|
7
|
-
* file that was distributed with this source code.
|
|
8
|
-
*/
|
|
9
|
-
import { Exception } from '@poppinss/utils';
|
|
10
|
-
/**
|
|
11
|
-
* Exception raised when one or more env variables
|
|
12
|
-
* are invalid
|
|
13
|
-
*/
|
|
14
|
-
export const E_INVALID_ENV_VARIABLES = class EnvValidationException extends Exception {
|
|
15
|
-
static message = 'Validation failed for one or more environment variables';
|
|
16
|
-
static code = 'E_INVALID_ENV_VARIABLES';
|
|
17
|
-
help = '';
|
|
18
|
-
};
|
package/build/src/loader.d.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
/// <reference types="@types/node" resolution-mode="require"/>
|
|
2
|
-
/**
|
|
3
|
-
* Read the contents of one or more dot-env files. Following is how the files
|
|
4
|
-
* are read.
|
|
5
|
-
*
|
|
6
|
-
* - Load file from the "ENV_PATH" environment file.
|
|
7
|
-
* (Raise error if file is missing)
|
|
8
|
-
*
|
|
9
|
-
* - If "ENV_PATH" is not defined, then find ".env" file in the app root.
|
|
10
|
-
* (Ignore if file is missing)
|
|
11
|
-
*
|
|
12
|
-
* - Find ".env.[NODE_ENV]" file in the app root.
|
|
13
|
-
* (Ignore if file is missing)
|
|
14
|
-
*
|
|
15
|
-
* ```ts
|
|
16
|
-
* const loader = new EnvLoader(new URL('./', import.meta.url))
|
|
17
|
-
*
|
|
18
|
-
* const { envContents, currentEnvContents } = await loader.load()
|
|
19
|
-
*
|
|
20
|
-
* // envContents: Contents of .env or file specified via ENV_PATH
|
|
21
|
-
* // currentEnvContents: Contents of .env.[NODE_ENV] file
|
|
22
|
-
* ```
|
|
23
|
-
*/
|
|
24
|
-
export declare class EnvLoader {
|
|
25
|
-
#private;
|
|
26
|
-
constructor(appRoot: string | URL, loadExampleFile?: boolean);
|
|
27
|
-
/**
|
|
28
|
-
* Load contents of the main dot-env file and the current
|
|
29
|
-
* environment dot-env file
|
|
30
|
-
*/
|
|
31
|
-
load(): Promise<{
|
|
32
|
-
contents: string;
|
|
33
|
-
path: string;
|
|
34
|
-
fileExists: boolean;
|
|
35
|
-
}[]>;
|
|
36
|
-
}
|
package/build/src/loader.js
DELETED
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* @adonisjs/env
|
|
3
|
-
*
|
|
4
|
-
* (c) AdonisJS
|
|
5
|
-
*
|
|
6
|
-
* For the full copyright and license information, please view the LICENSE
|
|
7
|
-
* file that was distributed with this source code.
|
|
8
|
-
*/
|
|
9
|
-
import { fileURLToPath } from 'node:url';
|
|
10
|
-
import { readFile } from 'node:fs/promises';
|
|
11
|
-
import { isAbsolute, join } from 'node:path';
|
|
12
|
-
import debug from './debug.js';
|
|
13
|
-
/**
|
|
14
|
-
* Read the contents of one or more dot-env files. Following is how the files
|
|
15
|
-
* are read.
|
|
16
|
-
*
|
|
17
|
-
* - Load file from the "ENV_PATH" environment file.
|
|
18
|
-
* (Raise error if file is missing)
|
|
19
|
-
*
|
|
20
|
-
* - If "ENV_PATH" is not defined, then find ".env" file in the app root.
|
|
21
|
-
* (Ignore if file is missing)
|
|
22
|
-
*
|
|
23
|
-
* - Find ".env.[NODE_ENV]" file in the app root.
|
|
24
|
-
* (Ignore if file is missing)
|
|
25
|
-
*
|
|
26
|
-
* ```ts
|
|
27
|
-
* const loader = new EnvLoader(new URL('./', import.meta.url))
|
|
28
|
-
*
|
|
29
|
-
* const { envContents, currentEnvContents } = await loader.load()
|
|
30
|
-
*
|
|
31
|
-
* // envContents: Contents of .env or file specified via ENV_PATH
|
|
32
|
-
* // currentEnvContents: Contents of .env.[NODE_ENV] file
|
|
33
|
-
* ```
|
|
34
|
-
*/
|
|
35
|
-
export class EnvLoader {
|
|
36
|
-
#appRoot;
|
|
37
|
-
#loadExampleFile;
|
|
38
|
-
constructor(appRoot, loadExampleFile = false) {
|
|
39
|
-
this.#appRoot = typeof appRoot === 'string' ? appRoot : fileURLToPath(appRoot);
|
|
40
|
-
this.#loadExampleFile = loadExampleFile;
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Optionally read a file from the disk
|
|
44
|
-
*/
|
|
45
|
-
async #loadFile(filePath) {
|
|
46
|
-
try {
|
|
47
|
-
const contents = await readFile(filePath, 'utf-8');
|
|
48
|
-
return { contents, fileExists: true };
|
|
49
|
-
}
|
|
50
|
-
catch (error) {
|
|
51
|
-
/* c8 ignore next 3 */
|
|
52
|
-
if (error.code !== 'ENOENT') {
|
|
53
|
-
throw error;
|
|
54
|
-
}
|
|
55
|
-
return { contents: '', fileExists: false };
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Load contents of the main dot-env file and the current
|
|
60
|
-
* environment dot-env file
|
|
61
|
-
*/
|
|
62
|
-
async load() {
|
|
63
|
-
const ENV_PATH = process.env.ENV_PATH;
|
|
64
|
-
const NODE_ENV = process.env.NODE_ENV;
|
|
65
|
-
const envFiles = [];
|
|
66
|
-
if (debug.enabled) {
|
|
67
|
-
debug('ENV_PATH variable is %s', ENV_PATH ? 'set' : 'not set');
|
|
68
|
-
debug('NODE_ENV variable is %s', NODE_ENV ? 'set' : 'not set');
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Base path to load .env files from
|
|
72
|
-
*/
|
|
73
|
-
const baseEnvPath = ENV_PATH
|
|
74
|
-
? isAbsolute(ENV_PATH)
|
|
75
|
-
? ENV_PATH
|
|
76
|
-
: join(this.#appRoot, ENV_PATH)
|
|
77
|
-
: this.#appRoot;
|
|
78
|
-
if (debug.enabled) {
|
|
79
|
-
debug('dot-env files base path "%s"', baseEnvPath);
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* 1st
|
|
83
|
-
* The top most priority is given to the ".env.[NODE_ENV].local" file
|
|
84
|
-
*/
|
|
85
|
-
if (NODE_ENV) {
|
|
86
|
-
const nodeEnvLocalFile = join(baseEnvPath, `.env.${NODE_ENV}.local`);
|
|
87
|
-
envFiles.push({
|
|
88
|
-
path: nodeEnvLocalFile,
|
|
89
|
-
...(await this.#loadFile(nodeEnvLocalFile)),
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* 2nd
|
|
94
|
-
* Next, we give priority to the ".env.local" file
|
|
95
|
-
*/
|
|
96
|
-
if (!NODE_ENV || !['test', 'testing'].includes(NODE_ENV)) {
|
|
97
|
-
const envLocalFile = join(baseEnvPath, '.env.local');
|
|
98
|
-
envFiles.push({
|
|
99
|
-
path: envLocalFile,
|
|
100
|
-
...(await this.#loadFile(envLocalFile)),
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* 3rd
|
|
105
|
-
* Next, we give priority to the ".env.[NODE_ENV]" file
|
|
106
|
-
*/
|
|
107
|
-
if (NODE_ENV) {
|
|
108
|
-
const nodeEnvFile = join(baseEnvPath, `.env.${NODE_ENV}`);
|
|
109
|
-
envFiles.push({
|
|
110
|
-
path: nodeEnvFile,
|
|
111
|
-
...(await this.#loadFile(nodeEnvFile)),
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* Finally, we push the contents of the ".env" file.
|
|
116
|
-
*/
|
|
117
|
-
const envFile = join(baseEnvPath, '.env');
|
|
118
|
-
envFiles.push({
|
|
119
|
-
path: envFile,
|
|
120
|
-
...(await this.#loadFile(envFile)),
|
|
121
|
-
});
|
|
122
|
-
/**
|
|
123
|
-
* Load example file
|
|
124
|
-
*/
|
|
125
|
-
if (this.#loadExampleFile) {
|
|
126
|
-
const envExampleFile = join(baseEnvPath, '.env.example');
|
|
127
|
-
envFiles.push({
|
|
128
|
-
path: envExampleFile,
|
|
129
|
-
...(await this.#loadFile(envExampleFile)),
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
return envFiles;
|
|
133
|
-
}
|
|
134
|
-
}
|
package/build/src/parser.d.ts
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { DotenvParseOutput } from 'dotenv';
|
|
2
|
-
/**
|
|
3
|
-
* Env parser parses the environment variables from a string formatted
|
|
4
|
-
* as a key-value pair seperated using an `=`. For example:
|
|
5
|
-
*
|
|
6
|
-
* ```dotenv
|
|
7
|
-
* PORT=3333
|
|
8
|
-
* HOST=127.0.0.1
|
|
9
|
-
* ```
|
|
10
|
-
*
|
|
11
|
-
* The variables can reference other environment variables as well using `$`.
|
|
12
|
-
* For example:
|
|
13
|
-
*
|
|
14
|
-
* ```dotenv
|
|
15
|
-
* PORT=3333
|
|
16
|
-
* REDIS_PORT=$PORT
|
|
17
|
-
* ```
|
|
18
|
-
*
|
|
19
|
-
* The variables using characters other than letters can wrap variable
|
|
20
|
-
* named inside a curly brace.
|
|
21
|
-
*
|
|
22
|
-
* ```dotenv
|
|
23
|
-
* APP-PORT=3333
|
|
24
|
-
* REDIS_PORT=${APP-PORT}
|
|
25
|
-
* ```
|
|
26
|
-
*
|
|
27
|
-
* You can escape the `$` sign with a backtick.
|
|
28
|
-
*
|
|
29
|
-
* ```dotenv
|
|
30
|
-
* REDIS_PASSWORD=foo\$123
|
|
31
|
-
* ```
|
|
32
|
-
*
|
|
33
|
-
* ## Usage
|
|
34
|
-
*
|
|
35
|
-
* ```ts
|
|
36
|
-
* const parser = new EnvParser(envContents)
|
|
37
|
-
* const output = parser.parse()
|
|
38
|
-
*
|
|
39
|
-
* // The output is a key-value pair
|
|
40
|
-
* ```
|
|
41
|
-
*/
|
|
42
|
-
export declare class EnvParser {
|
|
43
|
-
#private;
|
|
44
|
-
constructor(envContents: string, options?: {
|
|
45
|
-
ignoreProcessEnv: boolean;
|
|
46
|
-
});
|
|
47
|
-
/**
|
|
48
|
-
* Parse the env string to an object of environment variables.
|
|
49
|
-
*/
|
|
50
|
-
parse(): DotenvParseOutput;
|
|
51
|
-
}
|