@karmaniverous/get-dotenv 0.2.3 → 0.3.1
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/.env +0 -1
- package/README.md +18 -0
- package/bin/getdotenv/index.js +7 -2
- package/dist/default/lib/getDotenv/getDotenv.js +23 -0
- package/lib/getDotenv/getDotenv.js +19 -0
- package/package.json +1 -1
- package/.env.dev +0 -1
- package/.env.dev.secret +0 -1
- package/.env.secret +0 -1
- package/.env.test +0 -1
package/.env
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
APP_SETTING=root_app_setting
|
package/README.md
CHANGED
|
@@ -6,6 +6,7 @@ Load environment variables with a cascade of environment-aware dotenv files. You
|
|
|
6
6
|
- Specify the directory containing your dotenv files.
|
|
7
7
|
- Specify the token that identifies dotenv files (e.g. '.env').
|
|
8
8
|
- Specify the token that identifies private vatiables (e.g. 'local').
|
|
9
|
+
- Define dynamic variables progressively in terms of other variables and other logic.
|
|
9
10
|
- Load variables for a specific environment or none.
|
|
10
11
|
- Exclude public or private variables.
|
|
11
12
|
- Extract variables to an object, to `process.env`, or both.
|
|
@@ -40,6 +41,19 @@ const dotenvSync = await getDotenvSync(options);
|
|
|
40
41
|
```
|
|
41
42
|
|
|
42
43
|
See [OptionsType](#optionstype--object) below for configuration options.
|
|
44
|
+
|
|
45
|
+
## Dynamic Processing
|
|
46
|
+
|
|
47
|
+
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
|
+
|
|
49
|
+
```js
|
|
50
|
+
export default {
|
|
51
|
+
SOME_DYNAMIC_VARIABLE: (dotenv) => someLogic(dotenv),
|
|
52
|
+
ANOTHER_DYNAMIC_VARIABLE: (dotenv) => someOtherLogic,
|
|
53
|
+
};
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Each function takes the current `dotenv` variable package as an argument. These variables will be processed progressively, meaning each successive one will have access to the previous ones. They can also override existing variables.
|
|
43
57
|
|
|
44
58
|
# Command Line Interface
|
|
45
59
|
|
|
@@ -55,6 +69,8 @@ dotenv files. You can:
|
|
|
55
69
|
* Specify a default environment, override the default with an existing
|
|
56
70
|
environment variable, and override both with a direct setting.
|
|
57
71
|
* Exclude public or private variables.
|
|
72
|
+
* Define dynamic variables progressively in terms of other variables and
|
|
73
|
+
other logic.
|
|
58
74
|
* Execute a shell command after loading variables.
|
|
59
75
|
* Place the shell command inside the invocation to support npm script
|
|
60
76
|
arguments for other options.
|
|
@@ -68,6 +84,7 @@ Options:
|
|
|
68
84
|
-v, --variable <string> environment from variable
|
|
69
85
|
-r, --exclude-private exclude private variables (default: false)
|
|
70
86
|
-u, --exclude-public exclude public variables (default: false)
|
|
87
|
+
-y, --dynamic-path <string> dynamic variables path
|
|
71
88
|
-c, --command <string> shell command string
|
|
72
89
|
-l, --log log extracted variables (default: false)
|
|
73
90
|
-h, --help display help for command
|
|
@@ -129,6 +146,7 @@ get-dotenv options type
|
|
|
129
146
|
| Name | Type | Description |
|
|
130
147
|
| --- | --- | --- |
|
|
131
148
|
| [dotenvToken] | <code>string</code> | token indicating a dotenv file (default: '.env') |
|
|
149
|
+
| [dynamicPath] | <code>string</code> | path to file exporting an object keyed to dynamic variable functions |
|
|
132
150
|
| [env] | <code>string</code> | target environment |
|
|
133
151
|
| [excludePrivate] | <code>bool</code> | exclude private variables (default: false) |
|
|
134
152
|
| [excludePublic] | <code>bool</code> | exclude public variables (default: false) |
|
package/bin/getdotenv/index.js
CHANGED
|
@@ -5,7 +5,7 @@ import spawn from 'cross-spawn';
|
|
|
5
5
|
import { parseArgsStringToArgv } from 'string-argv';
|
|
6
6
|
|
|
7
7
|
// Import package exports.
|
|
8
|
-
import {
|
|
8
|
+
import { getDotenv } from '@karmaniverous/get-dotenv';
|
|
9
9
|
|
|
10
10
|
// Create CLI.
|
|
11
11
|
import { program } from 'commander';
|
|
@@ -25,6 +25,8 @@ program
|
|
|
25
25
|
`* Specify a default environment, override the default with an existing`,
|
|
26
26
|
` environment variable, and override both with a direct setting.`,
|
|
27
27
|
`* Exclude public or private variables.`,
|
|
28
|
+
`* Define dynamic variables progressively in terms of other variables and`,
|
|
29
|
+
` other logic.`,
|
|
28
30
|
`* Execute a shell command after loading variables.`,
|
|
29
31
|
`* Place the shell command inside the invocation to support npm script`,
|
|
30
32
|
` arguments for other options.`,
|
|
@@ -50,6 +52,7 @@ program
|
|
|
50
52
|
.option('-v, --variable <string>', 'environment from variable')
|
|
51
53
|
.option('-r, --exclude-private', 'exclude private variables (default: false)')
|
|
52
54
|
.option('-u, --exclude-public', 'exclude public variables (default: false)')
|
|
55
|
+
.option('-y, --dynamic-path <string>', 'dynamic variables path')
|
|
53
56
|
.option('-c, --command <string>', 'shell command string')
|
|
54
57
|
.option('-l, --log', 'log extracted variables (default: false)');
|
|
55
58
|
|
|
@@ -62,6 +65,7 @@ const {
|
|
|
62
65
|
environment,
|
|
63
66
|
excludePrivate,
|
|
64
67
|
excludePublic,
|
|
68
|
+
dynamicPath,
|
|
65
69
|
log,
|
|
66
70
|
paths,
|
|
67
71
|
privateToken,
|
|
@@ -74,12 +78,13 @@ if (command && program.args.length) program.error('command specified twice');
|
|
|
74
78
|
const env = environment ?? process.env[variable] ?? defaultEnvironment;
|
|
75
79
|
|
|
76
80
|
// Load dotenvs.
|
|
77
|
-
|
|
81
|
+
await getDotenv({
|
|
78
82
|
dotenvToken,
|
|
79
83
|
env,
|
|
80
84
|
excludePrivate,
|
|
81
85
|
excludePublic,
|
|
82
86
|
loadProcess: true,
|
|
87
|
+
dynamicPath,
|
|
83
88
|
log,
|
|
84
89
|
paths,
|
|
85
90
|
privateToken,
|
|
@@ -7,13 +7,19 @@ exports.getDotenvSync = exports.getDotenv = void 0;
|
|
|
7
7
|
var _path = _interopRequireDefault(require("path"));
|
|
8
8
|
var _dotenvExpand = require("dotenv-expand");
|
|
9
9
|
var _readDotenv = require("../readDotEnv/readDotenv.js");
|
|
10
|
+
var _url = require("url");
|
|
10
11
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
12
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
13
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
14
|
+
const _dirname = (0, _url.fileURLToPath)(new _url.URL('.', import.meta.url));
|
|
15
|
+
|
|
11
16
|
/**
|
|
12
17
|
* get-dotenv options type
|
|
13
18
|
*
|
|
14
19
|
* @typedef {Object} OptionsType
|
|
15
20
|
*
|
|
16
21
|
* @property {string} [dotenvToken] - token indicating a dotenv file (default: '.env')
|
|
22
|
+
* @property {string} [dynamicPath] - path to file exporting an object keyed to dynamic variable functions
|
|
17
23
|
* @property {string} [env] - target environment
|
|
18
24
|
* @property {bool} [excludePrivate] - exclude private variables (default: false)
|
|
19
25
|
* @property {bool} [excludePublic] - exclude public variables (default: false)
|
|
@@ -37,6 +43,7 @@ const getDotenv = async function () {
|
|
|
37
43
|
let {
|
|
38
44
|
dotenvToken = '.env',
|
|
39
45
|
env,
|
|
46
|
+
dynamicPath,
|
|
40
47
|
excludePrivate = false,
|
|
41
48
|
excludePublic = false,
|
|
42
49
|
loadProcess = false,
|
|
@@ -57,6 +64,18 @@ const getDotenv = async function () {
|
|
|
57
64
|
})
|
|
58
65
|
}), []));
|
|
59
66
|
|
|
67
|
+
// Process dynamic variables.
|
|
68
|
+
if (dynamicPath) {
|
|
69
|
+
const {
|
|
70
|
+
default: dynamic
|
|
71
|
+
} = await Promise.resolve(`${_path.default.relative(_dirname, dynamicPath).replace(/\\/g, '/')}`).then(s => _interopRequireWildcard(require(s)));
|
|
72
|
+
Object.keys(dynamic).forEach(key => {
|
|
73
|
+
Object.assign(dotenv, {
|
|
74
|
+
[key]: dynamic[key](dotenv)
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
60
79
|
// Log result.
|
|
61
80
|
if (log) console.log(dotenv);
|
|
62
81
|
|
|
@@ -78,6 +97,7 @@ exports.getDotenv = getDotenv;
|
|
|
78
97
|
const getDotenvSync = function () {
|
|
79
98
|
let {
|
|
80
99
|
dotenvToken = '.env',
|
|
100
|
+
dynamicPath,
|
|
81
101
|
env,
|
|
82
102
|
excludePrivate = false,
|
|
83
103
|
excludePublic = false,
|
|
@@ -99,6 +119,9 @@ const getDotenvSync = function () {
|
|
|
99
119
|
})
|
|
100
120
|
}), []));
|
|
101
121
|
|
|
122
|
+
// Throw error if dynamicPath is set.
|
|
123
|
+
if (dynamicPath) throw new Error('dynamicPath not supported in sync mode');
|
|
124
|
+
|
|
102
125
|
// Log result.
|
|
103
126
|
if (log) console.log(dotenv);
|
|
104
127
|
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
2
|
import { expand } from 'dotenv-expand';
|
|
3
3
|
import { readDotenv, readDotenvSync } from '../readDotEnv/readDotenv.js';
|
|
4
|
+
import { URL, fileURLToPath } from 'url';
|
|
5
|
+
const __dirname = fileURLToPath(new URL('.', import.meta.url));
|
|
4
6
|
|
|
5
7
|
/**
|
|
6
8
|
* get-dotenv options type
|
|
@@ -8,6 +10,7 @@ import { readDotenv, readDotenvSync } from '../readDotEnv/readDotenv.js';
|
|
|
8
10
|
* @typedef {Object} OptionsType
|
|
9
11
|
*
|
|
10
12
|
* @property {string} [dotenvToken] - token indicating a dotenv file (default: '.env')
|
|
13
|
+
* @property {string} [dynamicPath] - path to file exporting an object keyed to dynamic variable functions
|
|
11
14
|
* @property {string} [env] - target environment
|
|
12
15
|
* @property {bool} [excludePrivate] - exclude private variables (default: false)
|
|
13
16
|
* @property {bool} [excludePublic] - exclude public variables (default: false)
|
|
@@ -30,6 +33,7 @@ import { readDotenv, readDotenvSync } from '../readDotEnv/readDotenv.js';
|
|
|
30
33
|
export const getDotenv = async ({
|
|
31
34
|
dotenvToken = '.env',
|
|
32
35
|
env,
|
|
36
|
+
dynamicPath,
|
|
33
37
|
excludePrivate = false,
|
|
34
38
|
excludePublic = false,
|
|
35
39
|
loadProcess = false,
|
|
@@ -67,6 +71,17 @@ export const getDotenv = async ({
|
|
|
67
71
|
)
|
|
68
72
|
);
|
|
69
73
|
|
|
74
|
+
// Process dynamic variables.
|
|
75
|
+
if (dynamicPath) {
|
|
76
|
+
const { default: dynamic } = await import(
|
|
77
|
+
path.relative(__dirname, dynamicPath).replace(/\\/g, '/')
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
Object.keys(dynamic).forEach((key) => {
|
|
81
|
+
Object.assign(dotenv, { [key]: dynamic[key](dotenv) });
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
70
85
|
// Log result.
|
|
71
86
|
if (log) console.log(dotenv);
|
|
72
87
|
|
|
@@ -87,6 +102,7 @@ export const getDotenv = async ({
|
|
|
87
102
|
*/
|
|
88
103
|
export const getDotenvSync = ({
|
|
89
104
|
dotenvToken = '.env',
|
|
105
|
+
dynamicPath,
|
|
90
106
|
env,
|
|
91
107
|
excludePrivate = false,
|
|
92
108
|
excludePublic = false,
|
|
@@ -125,6 +141,9 @@ export const getDotenvSync = ({
|
|
|
125
141
|
)
|
|
126
142
|
);
|
|
127
143
|
|
|
144
|
+
// Throw error if dynamicPath is set.
|
|
145
|
+
if (dynamicPath) throw new Error('dynamicPath not supported in sync mode');
|
|
146
|
+
|
|
128
147
|
// Log result.
|
|
129
148
|
if (log) console.log(dotenv);
|
|
130
149
|
|
package/package.json
CHANGED
package/.env.dev
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
ENV_SETTING=root_dev_setting
|
package/.env.dev.secret
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
ENV_SECRET=root_dev_secret
|
package/.env.secret
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
APP_SECRET=root_app_secret
|
package/.env.test
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
ENV_SETTING=root_test_setting
|