@karmaniverous/get-dotenv 0.3.1 → 0.3.3
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 +7 -4
- package/dist/default/lib/getDotenv/getDotenv.js +21 -5
- package/lib/getDotenv/getDotenv.js +65 -55
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -44,12 +44,15 @@ See [OptionsType](#optionstype--object) below for configuration options.
|
|
|
44
44
|
|
|
45
45
|
## Dynamic Processing
|
|
46
46
|
|
|
47
|
+
This package supports the full [`dotenv-expand`](https://www.npmjs.com/package/dotenv-expand) syntax.
|
|
48
|
+
|
|
47
49
|
For the async form only (`getDotenv`, not `getDotenvSync), use the `dynamicPath` option to add a relative path to a module with a default export like this:
|
|
48
50
|
|
|
49
51
|
```js
|
|
50
52
|
export default {
|
|
51
53
|
SOME_DYNAMIC_VARIABLE: (dotenv) => someLogic(dotenv),
|
|
52
|
-
ANOTHER_DYNAMIC_VARIABLE: (dotenv) =>
|
|
54
|
+
ANOTHER_DYNAMIC_VARIABLE: (dotenv) =>
|
|
55
|
+
someOtherLogic(dotenv.SOME_DYNAMIC_VARIABLE),
|
|
53
56
|
};
|
|
54
57
|
```
|
|
55
58
|
|
|
@@ -95,7 +98,7 @@ Options:
|
|
|
95
98
|
## Functions
|
|
96
99
|
|
|
97
100
|
<dl>
|
|
98
|
-
<dt><a href="#getDotenv">getDotenv([options])</a> ⇒ <code>
|
|
101
|
+
<dt><a href="#getDotenv">getDotenv([options])</a> ⇒ <code>Promise.<object></code></dt>
|
|
99
102
|
<dd><p>Asynchronously process dotenv files of the form .env[.<ENV>][.<PRIVATE_TOKEN>]</p>
|
|
100
103
|
</dd>
|
|
101
104
|
<dt><a href="#getDotenvSync">getDotenvSync([options])</a> ⇒ <code>Object</code></dt>
|
|
@@ -113,11 +116,11 @@ Options:
|
|
|
113
116
|
|
|
114
117
|
<a name="getDotenv"></a>
|
|
115
118
|
|
|
116
|
-
## getDotenv([options]) ⇒ <code>
|
|
119
|
+
## getDotenv([options]) ⇒ <code>Promise.<object></code>
|
|
117
120
|
Asynchronously process dotenv files of the form .env[.<ENV>][.<PRIVATE_TOKEN>]
|
|
118
121
|
|
|
119
122
|
**Kind**: global function
|
|
120
|
-
**Returns**: <code>
|
|
123
|
+
**Returns**: <code>Promise.<object></code> - The combined parsed dotenv object.
|
|
121
124
|
|
|
122
125
|
| Param | Type | Description |
|
|
123
126
|
| --- | --- | --- |
|
|
@@ -37,7 +37,7 @@ const _dirname = (0, _url.fileURLToPath)(new _url.URL('.', import.meta.url));
|
|
|
37
37
|
*
|
|
38
38
|
* @param {OptionsType} [options] - options object
|
|
39
39
|
*
|
|
40
|
-
* @returns {
|
|
40
|
+
* @returns {Promise<object>} The combined parsed dotenv object.
|
|
41
41
|
*/
|
|
42
42
|
const getDotenv = async function () {
|
|
43
43
|
let {
|
|
@@ -52,7 +52,7 @@ const getDotenv = async function () {
|
|
|
52
52
|
privateToken = 'local'
|
|
53
53
|
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
54
54
|
// Read .env files.
|
|
55
|
-
const
|
|
55
|
+
const parsed = await paths.reduce(async (e, p) => ({
|
|
56
56
|
...(await e),
|
|
57
57
|
...(excludePublic ? {} : {
|
|
58
58
|
...(await (0, _readDotenv.readDotenv)(_path.default.resolve(p, dotenvToken))),
|
|
@@ -62,7 +62,15 @@ const getDotenv = async function () {
|
|
|
62
62
|
...(await (0, _readDotenv.readDotenv)(_path.default.resolve(p, `${dotenvToken}.${privateToken}`))),
|
|
63
63
|
...(env ? await (0, _readDotenv.readDotenv)(_path.default.resolve(p, `${dotenvToken}.${env}.${privateToken}`)) : {})
|
|
64
64
|
})
|
|
65
|
-
}), [])
|
|
65
|
+
}), []);
|
|
66
|
+
const {
|
|
67
|
+
error,
|
|
68
|
+
parsed: dotenv
|
|
69
|
+
} = (0, _dotenvExpand.expand)({
|
|
70
|
+
ignoreProcessEnv: true,
|
|
71
|
+
parsed
|
|
72
|
+
});
|
|
73
|
+
if (error) throw new Error(error);
|
|
66
74
|
|
|
67
75
|
// Process dynamic variables.
|
|
68
76
|
if (dynamicPath) {
|
|
@@ -107,7 +115,7 @@ const getDotenvSync = function () {
|
|
|
107
115
|
privateToken = 'local'
|
|
108
116
|
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
109
117
|
// Read .env files.
|
|
110
|
-
const
|
|
118
|
+
const parsed = paths.reduce((e, p) => ({
|
|
111
119
|
...e,
|
|
112
120
|
...(excludePublic ? {} : {
|
|
113
121
|
...(0, _readDotenv.readDotenvSync)(_path.default.resolve(p, dotenvToken)),
|
|
@@ -117,7 +125,15 @@ const getDotenvSync = function () {
|
|
|
117
125
|
...(0, _readDotenv.readDotenvSync)(_path.default.resolve(p, `${dotenvToken}.${privateToken}`)),
|
|
118
126
|
...(env ? (0, _readDotenv.readDotenvSync)(_path.default.resolve(p, `${dotenvToken}.${env}.${privateToken}`)) : {})
|
|
119
127
|
})
|
|
120
|
-
}), [])
|
|
128
|
+
}), []);
|
|
129
|
+
const {
|
|
130
|
+
error,
|
|
131
|
+
parsed: dotenv
|
|
132
|
+
} = (0, _dotenvExpand.expand)({
|
|
133
|
+
ignoreProcessEnv: true,
|
|
134
|
+
parsed
|
|
135
|
+
});
|
|
136
|
+
if (error) throw new Error(error);
|
|
121
137
|
|
|
122
138
|
// Throw error if dynamicPath is set.
|
|
123
139
|
if (dynamicPath) throw new Error('dynamicPath not supported in sync mode');
|
|
@@ -28,7 +28,7 @@ const __dirname = fileURLToPath(new URL('.', import.meta.url));
|
|
|
28
28
|
*
|
|
29
29
|
* @param {OptionsType} [options] - options object
|
|
30
30
|
*
|
|
31
|
-
* @returns {
|
|
31
|
+
* @returns {Promise<object>} The combined parsed dotenv object.
|
|
32
32
|
*/
|
|
33
33
|
export const getDotenv = async ({
|
|
34
34
|
dotenvToken = '.env',
|
|
@@ -42,35 +42,40 @@ export const getDotenv = async ({
|
|
|
42
42
|
privateToken = 'local',
|
|
43
43
|
} = {}) => {
|
|
44
44
|
// Read .env files.
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
[]
|
|
71
|
-
)
|
|
45
|
+
const parsed = await paths.reduce(
|
|
46
|
+
async (e, p) => ({
|
|
47
|
+
...(await e),
|
|
48
|
+
...(excludePublic
|
|
49
|
+
? {}
|
|
50
|
+
: {
|
|
51
|
+
...(await readDotenv(path.resolve(p, dotenvToken))),
|
|
52
|
+
...(env
|
|
53
|
+
? await readDotenv(path.resolve(p, `${dotenvToken}.${env}`))
|
|
54
|
+
: {}),
|
|
55
|
+
}),
|
|
56
|
+
...(excludePrivate
|
|
57
|
+
? {}
|
|
58
|
+
: {
|
|
59
|
+
...(await readDotenv(
|
|
60
|
+
path.resolve(p, `${dotenvToken}.${privateToken}`)
|
|
61
|
+
)),
|
|
62
|
+
...(env
|
|
63
|
+
? await readDotenv(
|
|
64
|
+
path.resolve(p, `${dotenvToken}.${env}.${privateToken}`)
|
|
65
|
+
)
|
|
66
|
+
: {}),
|
|
67
|
+
}),
|
|
68
|
+
}),
|
|
69
|
+
[]
|
|
72
70
|
);
|
|
73
71
|
|
|
72
|
+
const { error, parsed: dotenv } = expand({
|
|
73
|
+
ignoreProcessEnv: true,
|
|
74
|
+
parsed,
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
if (error) throw new Error(error);
|
|
78
|
+
|
|
74
79
|
// Process dynamic variables.
|
|
75
80
|
if (dynamicPath) {
|
|
76
81
|
const { default: dynamic } = await import(
|
|
@@ -112,35 +117,40 @@ export const getDotenvSync = ({
|
|
|
112
117
|
privateToken = 'local',
|
|
113
118
|
} = {}) => {
|
|
114
119
|
// Read .env files.
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
[]
|
|
141
|
-
)
|
|
120
|
+
const parsed = paths.reduce(
|
|
121
|
+
(e, p) => ({
|
|
122
|
+
...e,
|
|
123
|
+
...(excludePublic
|
|
124
|
+
? {}
|
|
125
|
+
: {
|
|
126
|
+
...readDotenvSync(path.resolve(p, dotenvToken)),
|
|
127
|
+
...(env
|
|
128
|
+
? readDotenvSync(path.resolve(p, `${dotenvToken}.${env}`))
|
|
129
|
+
: {}),
|
|
130
|
+
}),
|
|
131
|
+
...(excludePrivate
|
|
132
|
+
? {}
|
|
133
|
+
: {
|
|
134
|
+
...readDotenvSync(
|
|
135
|
+
path.resolve(p, `${dotenvToken}.${privateToken}`)
|
|
136
|
+
),
|
|
137
|
+
...(env
|
|
138
|
+
? readDotenvSync(
|
|
139
|
+
path.resolve(p, `${dotenvToken}.${env}.${privateToken}`)
|
|
140
|
+
)
|
|
141
|
+
: {}),
|
|
142
|
+
}),
|
|
143
|
+
}),
|
|
144
|
+
[]
|
|
142
145
|
);
|
|
143
146
|
|
|
147
|
+
const { error, parsed: dotenv } = expand({
|
|
148
|
+
ignoreProcessEnv: true,
|
|
149
|
+
parsed,
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
if (error) throw new Error(error);
|
|
153
|
+
|
|
144
154
|
// Throw error if dynamicPath is set.
|
|
145
155
|
if (dynamicPath) throw new Error('dynamicPath not supported in sync mode');
|
|
146
156
|
|