molex-env 0.1.1 → 0.2.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 +8 -2
- package/package.json +1 -1
- package/src/index.js +20 -13
- package/src/lib/core.js +15 -1
package/README.md
CHANGED
|
@@ -22,11 +22,14 @@ npm install molex-env
|
|
|
22
22
|
|
|
23
23
|
## Quick start
|
|
24
24
|
```js
|
|
25
|
+
// simplest usage
|
|
26
|
+
require('molex-env').load();
|
|
27
|
+
|
|
28
|
+
// more detailed usage
|
|
25
29
|
const { load } = require('molex-env');
|
|
26
30
|
|
|
27
31
|
const result = load({
|
|
28
32
|
profile: 'prod',
|
|
29
|
-
export: true,
|
|
30
33
|
strict: true,
|
|
31
34
|
schema: {
|
|
32
35
|
PORT: 'number',
|
|
@@ -37,6 +40,7 @@ const result = load({
|
|
|
37
40
|
|
|
38
41
|
console.log(result.parsed.PORT);
|
|
39
42
|
console.log(result.origins.SERVICE_URL);
|
|
43
|
+
console.log(process.menv.PORT);
|
|
40
44
|
```
|
|
41
45
|
|
|
42
46
|
## Setup
|
|
@@ -72,8 +76,9 @@ Load, merge, parse, and validate .menv files.
|
|
|
72
76
|
- schema: allowed keys, types, defaults, required
|
|
73
77
|
- strict: reject unknown keys, duplicates, and invalid lines
|
|
74
78
|
- cast: true | false | { boolean, number, json, date }
|
|
75
|
-
-
|
|
79
|
+
- exportEnv: write values to process.env
|
|
76
80
|
- override: override existing process.env values
|
|
81
|
+
- attach: attach parsed values to process.menv (default true)
|
|
77
82
|
- freeze: deep-freeze parsed config (default true)
|
|
78
83
|
- onWarning: function(info) for non-strict duplicates
|
|
79
84
|
|
|
@@ -166,6 +171,7 @@ load({ freeze: false });
|
|
|
166
171
|
## Notes
|
|
167
172
|
- Use .menv.local for machine-specific values
|
|
168
173
|
- Use strict mode to detect surprises early
|
|
174
|
+
- Parsed values are attached to process.menv by default (disable with attach: false)
|
|
169
175
|
|
|
170
176
|
## Example project
|
|
171
177
|
An example app is included in examples/basic.
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -3,17 +3,24 @@
|
|
|
3
3
|
const { load, parse } = require('./lib/core');
|
|
4
4
|
const { watch } = require('./lib/watch');
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
function loadEntry(options)
|
|
7
|
+
{
|
|
8
|
+
return load(options);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Watch resolved .menv files and reload on change.
|
|
13
|
+
* @param {object} options
|
|
14
|
+
* @param {(err: Error|null, result?: {parsed: object, origins: object, files: string[]}) => void} onChange
|
|
15
|
+
* @returns {{close: () => void}}
|
|
16
|
+
*/
|
|
17
|
+
function watchEntry(options, onChange)
|
|
18
|
+
{
|
|
19
|
+
return watch(options, onChange, loadEntry);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
module.exports = Object.assign(loadEntry, {
|
|
23
|
+
load: loadEntry,
|
|
8
24
|
parse,
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
* @param {object} options
|
|
12
|
-
* @param {(err: Error|null, result?: {parsed: object, origins: object, files: string[]}) => void} onChange
|
|
13
|
-
* @returns {{close: () => void}}
|
|
14
|
-
*/
|
|
15
|
-
watch(options, onChange)
|
|
16
|
-
{
|
|
17
|
-
return watch(options, onChange, load);
|
|
18
|
-
}
|
|
19
|
-
};
|
|
25
|
+
watch: watchEntry
|
|
26
|
+
});
|
package/src/lib/core.js
CHANGED
|
@@ -30,7 +30,7 @@ function buildState()
|
|
|
30
30
|
*/
|
|
31
31
|
function exportToEnv(values, options)
|
|
32
32
|
{
|
|
33
|
-
|
|
33
|
+
if (!options.exportEnv) return;
|
|
34
34
|
for (const [key, value] of Object.entries(values))
|
|
35
35
|
{
|
|
36
36
|
if (!options.override && Object.prototype.hasOwnProperty.call(process.env, key))
|
|
@@ -41,6 +41,18 @@ function exportToEnv(values, options)
|
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
+
/**
|
|
45
|
+
* Attach parsed values to process.menv unless disabled.
|
|
46
|
+
* @param {object} values
|
|
47
|
+
* @param {object} options
|
|
48
|
+
* @returns {void}
|
|
49
|
+
*/
|
|
50
|
+
function attachToProcess(values, options)
|
|
51
|
+
{
|
|
52
|
+
if (options.attach === false) return;
|
|
53
|
+
process.menv = values;
|
|
54
|
+
}
|
|
55
|
+
|
|
44
56
|
/**
|
|
45
57
|
* Load .menv files and return parsed values with origins.
|
|
46
58
|
* @param {object} options
|
|
@@ -81,6 +93,8 @@ function load(options = {})
|
|
|
81
93
|
deepFreeze(state.values);
|
|
82
94
|
}
|
|
83
95
|
|
|
96
|
+
attachToProcess(state.values, options);
|
|
97
|
+
|
|
84
98
|
return {
|
|
85
99
|
parsed: state.values,
|
|
86
100
|
origins: state.origins,
|