@karmaniverous/get-dotenv 0.2.2 → 0.3.0

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 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
 
@@ -129,6 +143,7 @@ get-dotenv options type
129
143
  | Name | Type | Description |
130
144
  | --- | --- | --- |
131
145
  | [dotenvToken] | <code>string</code> | token indicating a dotenv file (default: '.env') |
146
+ | [dynamicPath] | <code>string</code> | path to file exporting an object keyed to dynamic variable functions |
132
147
  | [env] | <code>string</code> | target environment |
133
148
  | [excludePrivate] | <code>bool</code> | exclude private variables (default: false) |
134
149
  | [excludePublic] | <code>bool</code> | exclude public variables (default: false) |
@@ -8,12 +8,15 @@ var _path = _interopRequireDefault(require("path"));
8
8
  var _dotenvExpand = require("dotenv-expand");
9
9
  var _readDotenv = require("../readDotEnv/readDotenv.js");
10
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
+ 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); }
12
+ 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; }
11
13
  /**
12
14
  * get-dotenv options type
13
15
  *
14
16
  * @typedef {Object} OptionsType
15
17
  *
16
18
  * @property {string} [dotenvToken] - token indicating a dotenv file (default: '.env')
19
+ * @property {string} [dynamicPath] - path to file exporting an object keyed to dynamic variable functions
17
20
  * @property {string} [env] - target environment
18
21
  * @property {bool} [excludePrivate] - exclude private variables (default: false)
19
22
  * @property {bool} [excludePublic] - exclude public variables (default: false)
@@ -37,6 +40,7 @@ const getDotenv = async function () {
37
40
  let {
38
41
  dotenvToken = '.env',
39
42
  env,
43
+ dynamicPath,
40
44
  excludePrivate = false,
41
45
  excludePublic = false,
42
46
  loadProcess = false,
@@ -57,6 +61,18 @@ const getDotenv = async function () {
57
61
  })
58
62
  }), []));
59
63
 
64
+ // Process dynamic variables.
65
+ if (dynamicPath) {
66
+ const {
67
+ default: dynamic
68
+ } = await Promise.resolve(`${dynamicPath}`).then(s => _interopRequireWildcard(require(s)));
69
+ Object.keys(dynamic).forEach(key => {
70
+ Object.assign(dotenv, {
71
+ [key]: dynamic[key](dotenv)
72
+ });
73
+ });
74
+ }
75
+
60
76
  // Log result.
61
77
  if (log) console.log(dotenv);
62
78
 
@@ -78,6 +94,7 @@ exports.getDotenv = getDotenv;
78
94
  const getDotenvSync = function () {
79
95
  let {
80
96
  dotenvToken = '.env',
97
+ dynamicPath,
81
98
  env,
82
99
  excludePrivate = false,
83
100
  excludePublic = false,
@@ -99,6 +116,9 @@ const getDotenvSync = function () {
99
116
  })
100
117
  }), []));
101
118
 
119
+ // Throw error if dynamicPath is set.
120
+ if (dynamicPath) throw new Error('dynamicPath not supported in sync mode');
121
+
102
122
  // Log result.
103
123
  if (log) console.log(dotenv);
104
124
 
@@ -8,6 +8,7 @@ import { readDotenv, readDotenvSync } from '../readDotEnv/readDotenv.js';
8
8
  * @typedef {Object} OptionsType
9
9
  *
10
10
  * @property {string} [dotenvToken] - token indicating a dotenv file (default: '.env')
11
+ * @property {string} [dynamicPath] - path to file exporting an object keyed to dynamic variable functions
11
12
  * @property {string} [env] - target environment
12
13
  * @property {bool} [excludePrivate] - exclude private variables (default: false)
13
14
  * @property {bool} [excludePublic] - exclude public variables (default: false)
@@ -30,6 +31,7 @@ import { readDotenv, readDotenvSync } from '../readDotEnv/readDotenv.js';
30
31
  export const getDotenv = async ({
31
32
  dotenvToken = '.env',
32
33
  env,
34
+ dynamicPath,
33
35
  excludePrivate = false,
34
36
  excludePublic = false,
35
37
  loadProcess = false,
@@ -67,6 +69,15 @@ export const getDotenv = async ({
67
69
  )
68
70
  );
69
71
 
72
+ // Process dynamic variables.
73
+ if (dynamicPath) {
74
+ const { default: dynamic } = await import(dynamicPath);
75
+
76
+ Object.keys(dynamic).forEach((key) => {
77
+ Object.assign(dotenv, { [key]: dynamic[key](dotenv) });
78
+ });
79
+ }
80
+
70
81
  // Log result.
71
82
  if (log) console.log(dotenv);
72
83
 
@@ -87,6 +98,7 @@ export const getDotenv = async ({
87
98
  */
88
99
  export const getDotenvSync = ({
89
100
  dotenvToken = '.env',
101
+ dynamicPath,
90
102
  env,
91
103
  excludePrivate = false,
92
104
  excludePublic = false,
@@ -125,6 +137,9 @@ export const getDotenvSync = ({
125
137
  )
126
138
  );
127
139
 
140
+ // Throw error if dynamicPath is set.
141
+ if (dynamicPath) throw new Error('dynamicPath not supported in sync mode');
142
+
128
143
  // Log result.
129
144
  if (log) console.log(dotenv);
130
145
 
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "bin": {
4
4
  "getdotenv": "bin/getdotenv/index.js"
5
5
  },
6
- "version": "0.2.2",
6
+ "version": "0.3.0",
7
7
  "publishConfig": {
8
8
  "access": "public"
9
9
  },
@@ -62,6 +62,7 @@
62
62
  "require": "./dist/default/lib/index.js"
63
63
  }
64
64
  },
65
+ "main": "./lib/index.js",
65
66
  "mocha": {
66
67
  "exclude": [
67
68
  "./dist/**",
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