@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 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 precede the `.env` file.
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
- const envValues = new EnvParser(envContents).parse()
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(),
@@ -1,6 +1,8 @@
1
1
  import { DotenvParseOutput } from 'dotenv';
2
2
  export declare class EnvParser {
3
3
  #private;
4
- constructor(envContents: string);
4
+ constructor(envContents: string, options?: {
5
+ preferProcessEnv: boolean;
6
+ });
5
7
  parse(): DotenvParseOutput;
6
8
  }
@@ -1,14 +1,21 @@
1
1
  import dotenv from 'dotenv';
2
2
  export class EnvParser {
3
3
  #envContents;
4
- constructor(envContents) {
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.#interpolate(envCollection[key], envCollection);
63
+ result[key] = this.#getValue(key, envCollection);
57
64
  return result;
58
65
  }, {});
59
66
  }
@@ -7,18 +7,18 @@ export class EnvValidator {
7
7
  this.#error = new InvalidEnvVariablesException();
8
8
  }
9
9
  validate(values) {
10
- const help = [];
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
- help.push(`- ${error.message}`);
16
+ cause.push(`- ${error.message}`);
17
17
  }
18
18
  return result;
19
19
  }, { ...values });
20
- if (help.length) {
21
- this.#error.help = help.join('\n');
20
+ if (cause.length) {
21
+ this.#error.cause = cause.join('\n');
22
22
  throw this.#error;
23
23
  }
24
24
  return validated;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adonisjs/env",
3
- "version": "4.0.0-1",
3
+ "version": "4.0.0-2",
4
4
  "description": "Environment variable manager for Node.js",
5
5
  "main": "build/index.js",
6
6
  "type": "module",