@adonisjs/env 4.0.0-1 → 4.0.0-2
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 +20 -14
- package/build/src/parser.d.ts +3 -1
- package/build/src/parser.js +10 -3
- package/build/src/validator.js +4 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -25,7 +25,7 @@ import { EnvLoader } from '@adonisjs/env'
|
|
|
25
25
|
const lookupPath = new URL('./', import.meta.url)
|
|
26
26
|
const loader = new EnvLoader(lookupPath)
|
|
27
27
|
|
|
28
|
-
const { envContents, currentEnvContents } = loader.load()
|
|
28
|
+
const { envContents, currentEnvContents } = await loader.load()
|
|
29
29
|
```
|
|
30
30
|
|
|
31
31
|
### `envContents`
|
|
@@ -39,7 +39,7 @@ const { envContents, currentEnvContents } = loader.load()
|
|
|
39
39
|
|
|
40
40
|
- The `currentEnvContents` contents are read from the `.env.[NODE_ENV]` file.
|
|
41
41
|
- If the current `NODE_ENV = 'development'`, then the contents of this variable will be from the `.env.development` file and so on.
|
|
42
|
-
- The contents of this file should
|
|
42
|
+
- The contents of this file should take precendence over the `.env` file.
|
|
43
43
|
|
|
44
44
|
## EnvParser
|
|
45
45
|
The `EnvParser` class is responsible for parsing the contents of the `.env` file(s) and converting them into an object.
|
|
@@ -49,7 +49,7 @@ import { EnvLoader, EnvParser } from '@adonisjs/env'
|
|
|
49
49
|
|
|
50
50
|
const lookupPath = new URL('./', import.meta.url)
|
|
51
51
|
const loader = new EnvLoader(lookupPath)
|
|
52
|
-
const { envContents, currentEnvContents } = loader.load()
|
|
52
|
+
const { envContents, currentEnvContents } = await loader.load()
|
|
53
53
|
|
|
54
54
|
const envParser = new EnvParser(envContents)
|
|
55
55
|
const currentEnvParser = new EnvParser(currentEnvContents)
|
|
@@ -60,6 +60,12 @@ console.log(currentEnvParser.parse()) // { key: value }
|
|
|
60
60
|
|
|
61
61
|
The return value of `parser.parse` is an object with key-value pair. The parser also has support for interpolation.
|
|
62
62
|
|
|
63
|
+
You can also instruct the parser to prefer existing `process.env` values when they exist. When `preferProcessEnv` is set to `true`, the value from the env contents will be discarded in favor of existing `process.env` value.
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
new EnvParser(envContents, { preferProcessEnv: true })
|
|
67
|
+
```
|
|
68
|
+
|
|
63
69
|
## Validating environment variables
|
|
64
70
|
Once you have the parsed objects, you can optionally validate them against a pre-defined schema. We recommend validation for the following reasons.
|
|
65
71
|
|
|
@@ -91,10 +97,19 @@ import { EnvLoader, EnvParser, Env } from '@adonisjs/env'
|
|
|
91
97
|
|
|
92
98
|
const lookupPath = new URL('./', import.meta.url)
|
|
93
99
|
const loader = new EnvLoader(lookupPath)
|
|
94
|
-
const { envContents, currentEnvContents } = loader.load()
|
|
100
|
+
const { envContents, currentEnvContents } = await loader.load()
|
|
95
101
|
|
|
96
|
-
|
|
102
|
+
/**
|
|
103
|
+
* Loop over all the current env parsed values and let
|
|
104
|
+
* them take precedence over the existing process.env
|
|
105
|
+
* values.
|
|
106
|
+
*/
|
|
97
107
|
const currentEnvValues = new EnvParser(currentEnvContents).parse()
|
|
108
|
+
Object.keys(currentEnvValues).forEach((key) => {
|
|
109
|
+
process.env[key] = currentEnvValues[key]
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
const envValues = new EnvParser(envContents, { preferProcessEnv: true }).parse()
|
|
98
113
|
|
|
99
114
|
/**
|
|
100
115
|
* Loop over all the parsed env values and set
|
|
@@ -112,15 +127,6 @@ Object.keys(envValues).forEach((key) => {
|
|
|
112
127
|
}
|
|
113
128
|
})
|
|
114
129
|
|
|
115
|
-
/**
|
|
116
|
-
* Loop over all the current env parsed values and make
|
|
117
|
-
* them take precedence over the existing process.env
|
|
118
|
-
* values.
|
|
119
|
-
*/
|
|
120
|
-
Object.keys(currentEnvValues).forEach((key) => {
|
|
121
|
-
process.env[key] = envValues[key]
|
|
122
|
-
})
|
|
123
|
-
|
|
124
130
|
// Now perform the validation
|
|
125
131
|
const validate = Env.rules({
|
|
126
132
|
PORT: Env.schema.number(),
|
package/build/src/parser.d.ts
CHANGED
package/build/src/parser.js
CHANGED
|
@@ -1,14 +1,21 @@
|
|
|
1
1
|
import dotenv from 'dotenv';
|
|
2
2
|
export class EnvParser {
|
|
3
3
|
#envContents;
|
|
4
|
-
|
|
4
|
+
#preferProcessEnv = false;
|
|
5
|
+
constructor(envContents, options) {
|
|
6
|
+
if (options?.preferProcessEnv) {
|
|
7
|
+
this.#preferProcessEnv = true;
|
|
8
|
+
}
|
|
5
9
|
this.#envContents = envContents;
|
|
6
10
|
}
|
|
7
11
|
#getValue(key, parsed) {
|
|
12
|
+
if (this.#preferProcessEnv && process.env[key]) {
|
|
13
|
+
return process.env[key];
|
|
14
|
+
}
|
|
8
15
|
if (parsed[key]) {
|
|
9
16
|
return this.#interpolate(parsed[key], parsed);
|
|
10
17
|
}
|
|
11
|
-
return '';
|
|
18
|
+
return process.env[key] || '';
|
|
12
19
|
}
|
|
13
20
|
#interpolateMustache(token, parsed) {
|
|
14
21
|
const closingBrace = token.indexOf('}');
|
|
@@ -53,7 +60,7 @@ export class EnvParser {
|
|
|
53
60
|
parse() {
|
|
54
61
|
const envCollection = dotenv.parse(this.#envContents.trim());
|
|
55
62
|
return Object.keys(envCollection).reduce((result, key) => {
|
|
56
|
-
result[key] = this.#
|
|
63
|
+
result[key] = this.#getValue(key, envCollection);
|
|
57
64
|
return result;
|
|
58
65
|
}, {});
|
|
59
66
|
}
|
package/build/src/validator.js
CHANGED
|
@@ -7,18 +7,18 @@ export class EnvValidator {
|
|
|
7
7
|
this.#error = new InvalidEnvVariablesException();
|
|
8
8
|
}
|
|
9
9
|
validate(values) {
|
|
10
|
-
const
|
|
10
|
+
const cause = [];
|
|
11
11
|
const validated = Object.keys(this.#schema).reduce((result, key) => {
|
|
12
12
|
try {
|
|
13
13
|
result[key] = this.#schema[key](key, values[key]);
|
|
14
14
|
}
|
|
15
15
|
catch (error) {
|
|
16
|
-
|
|
16
|
+
cause.push(`- ${error.message}`);
|
|
17
17
|
}
|
|
18
18
|
return result;
|
|
19
19
|
}, { ...values });
|
|
20
|
-
if (
|
|
21
|
-
this.#error.
|
|
20
|
+
if (cause.length) {
|
|
21
|
+
this.#error.cause = cause.join('\n');
|
|
22
22
|
throw this.#error;
|
|
23
23
|
}
|
|
24
24
|
return validated;
|