@karmaniverous/get-dotenv 2.0.2 → 2.1.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/README.md CHANGED
@@ -71,22 +71,23 @@ Load environment variables with a cascade of environment-aware
71
71
  dotenv files. You can:
72
72
 
73
73
  * Specify the directories containing your dotenv files.
74
- * Specify the token that identifies dotenv files (e.g. '.env').
75
- * Specify the token that identifies private vatiables (e.g. '.local').
74
+ * Define dynamic variables progressively in terms of other variables and
75
+ other logic.
76
+ * Specify a consolidated output file path.
76
77
  * Load variables for a specific environment or none.
77
78
  * Specify a default environment, override the default with an existing
78
79
  environment variable, and override both with a direct setting.
79
- * Exclude public or private variables.
80
- * Exclude global & dynamic or environment-specific variables.
81
- * Define dynamic variables progressively in terms of other variables and
82
- other logic.
80
+ * Exclude public, private, global, environment-specific, or dynamic variables.
83
81
  * Execute a &&-delimited series of shell commands after loading variables.
84
82
  * Place the shell commands inside the invocation to support npm script
85
83
  arguments for other options.
84
+ * Specify the token that identifies dotenv files (e.g. '.env').
85
+ * Specify the token that identifies private vatiables (e.g. '.local').
86
86
 
87
87
  Options:
88
88
  -p, --paths <strings...> space-delimited paths to dotenv directory (default './')
89
89
  -y, --dynamic-path <string> dynamic variables path
90
+ -o, --output-path <string> consolidated output file
90
91
  -d, --defaultEnvironment <string> default environment (prefix with $ to use environment variable)
91
92
  -e, --environment <string> designated environment (prefix with $ to use environment variable)
92
93
  -n, --exclude-env exclude environment-specific variables (default: false)
@@ -166,7 +167,8 @@ get-dotenv options type
166
167
  | [excludePublic] | <code>bool</code> | exclude public variables (default: false) |
167
168
  | [loadProcess] | <code>bool</code> | load dotenv to process.env (default: false) |
168
169
  | [log] | <code>bool</code> | log result to console (default: false) |
169
- | [paths] | <code>Array.&lt;string&gt;</code> | array of target directory paths (default ['./']) |
170
+ | [outputPath] | <code>string</code> | if populated, writes consolidated .env file to this path |
171
+ | [paths] | <code>Array.&lt;string&gt;</code> | array of input directory paths (default ['./']) |
170
172
  | [privateToken] | <code>string</code> | token indicating private variables (default: 'local'). |
171
173
 
172
174
 
@@ -26,18 +26,18 @@ program
26
26
  `dotenv files. You can:`,
27
27
  ``,
28
28
  `* Specify the directories containing your dotenv files.`,
29
- `* Specify the token that identifies dotenv files (e.g. '.env').`,
30
- `* Specify the token that identifies private vatiables (e.g. '.local').`,
29
+ `* Define dynamic variables progressively in terms of other variables and`,
30
+ ` other logic.`,
31
+ `* Specify a consolidated output file path.`,
31
32
  `* Load variables for a specific environment or none.`,
32
33
  `* Specify a default environment, override the default with an existing`,
33
34
  ` environment variable, and override both with a direct setting.`,
34
- `* Exclude public or private variables.`,
35
- `* Exclude global & dynamic or environment-specific variables.`,
36
- `* Define dynamic variables progressively in terms of other variables and`,
37
- ` other logic.`,
35
+ `* Exclude public, private, global, environment-specific, or dynamic variables.`,
38
36
  `* Execute a &&-delimited series of shell commands after loading variables.`,
39
37
  `* Place the shell commands inside the invocation to support npm script`,
40
38
  ` arguments for other options.`,
39
+ `* Specify the token that identifies dotenv files (e.g. '.env').`,
40
+ `* Specify the token that identifies private vatiables (e.g. '.local').`,
41
41
  ].join('\n')
42
42
  );
43
43
 
@@ -48,6 +48,7 @@ program
48
48
  "space-delimited paths to dotenv directory (default './')"
49
49
  )
50
50
  .option('-y, --dynamic-path <string>', 'dynamic variables path')
51
+ .option('-o, --output-path <string>', 'consolidated output file')
51
52
  .option(
52
53
  '-d, --defaultEnvironment <string>',
53
54
  'default environment (prefix with $ to use environment variable)',
@@ -93,6 +94,7 @@ const {
93
94
  excludePublic,
94
95
  dynamicPath,
95
96
  log,
97
+ outputPath,
96
98
  paths,
97
99
  privateToken,
98
100
  } = program.opts();
@@ -113,6 +115,7 @@ await getDotenv({
113
115
  loadProcess: true,
114
116
  dynamicPath,
115
117
  log,
118
+ outputPath,
116
119
  paths,
117
120
  privateToken,
118
121
  });
@@ -28,7 +28,8 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
28
28
  * @property {bool} [excludePublic] - exclude public variables (default: false)
29
29
  * @property {bool} [loadProcess] - load dotenv to process.env (default: false)
30
30
  * @property {bool} [log] - log result to console (default: false)
31
- * @property {string[]} [paths] - array of target directory paths (default ['./'])
31
+ * @property {string} [outputPath] - if populated, writes consolidated .env file to this path
32
+ * @property {string[]} [paths] - array of input directory paths (default ['./'])
32
33
  * @property {string} [privateToken] - token indicating private variables (default: 'local').
33
34
  */
34
35
 
@@ -54,6 +55,7 @@ const getDotenv = async function () {
54
55
  excludePublic = false,
55
56
  loadProcess = false,
56
57
  log = false,
58
+ outputPath,
57
59
  paths = ['./'],
58
60
  privateToken = 'local'
59
61
  } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
@@ -89,7 +91,9 @@ const getDotenv = async function () {
89
91
  } catch ({
90
92
  message
91
93
  }) {
92
- throw new Error(`Dynamic processing failed for variable '${key}'. ${message}`);
94
+ Object.assign(dotenv, {
95
+ [key]: `ERROR: ${message}`
96
+ });
93
97
  }
94
98
  });
95
99
  }
@@ -99,6 +103,12 @@ const getDotenv = async function () {
99
103
 
100
104
  // Load process.env.
101
105
  if (loadProcess) Object.assign(process.env, dotenv);
106
+
107
+ // Write output file.
108
+ if (outputPath) await _fsExtra.default.writeFile(outputPath, Object.keys(dotenv).reduce((contents, key) => {
109
+ const value = dotenv[key] ?? '';
110
+ return `${contents}${key}=${value.includes('\n') ? `"${value}"` : value}\n`;
111
+ }, ''));
102
112
  return dotenv;
103
113
  };
104
114
 
@@ -124,6 +134,7 @@ const getDotenvSync = function () {
124
134
  excludePublic = false,
125
135
  loadProcess = false,
126
136
  log = false,
137
+ outputPath,
127
138
  paths = ['./'],
128
139
  privateToken = 'local'
129
140
  } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
@@ -163,6 +174,12 @@ const getDotenvSync = function () {
163
174
 
164
175
  // Load process.env.
165
176
  if (loadProcess) Object.assign(process.env, dotenv);
177
+
178
+ // Write output file.
179
+ if (outputPath) _fsExtra.default.writeFileSync(outputPath, Object.keys(dotenv).reduce((contents, key) => {
180
+ const value = dotenv[key] ?? '';
181
+ return `${contents}${key}=${value.includes('\n') ? `"${value}"` : value}\n`;
182
+ }, ''));
166
183
  return dotenv;
167
184
  };
168
185
  exports.getDotenvSync = getDotenvSync;
@@ -21,7 +21,8 @@ import { readDotenv, readDotenvSync } from '../readDotEnv/readDotenv.js';
21
21
  * @property {bool} [excludePublic] - exclude public variables (default: false)
22
22
  * @property {bool} [loadProcess] - load dotenv to process.env (default: false)
23
23
  * @property {bool} [log] - log result to console (default: false)
24
- * @property {string[]} [paths] - array of target directory paths (default ['./'])
24
+ * @property {string} [outputPath] - if populated, writes consolidated .env file to this path
25
+ * @property {string[]} [paths] - array of input directory paths (default ['./'])
25
26
  * @property {string} [privateToken] - token indicating private variables (default: 'local').
26
27
  */
27
28
 
@@ -46,6 +47,7 @@ export const getDotenv = async ({
46
47
  excludePublic = false,
47
48
  loadProcess = false,
48
49
  log = false,
50
+ outputPath,
49
51
  paths = ['./'],
50
52
  privateToken = 'local',
51
53
  } = {}) => {
@@ -97,9 +99,7 @@ export const getDotenv = async ({
97
99
  try {
98
100
  Object.assign(dotenv, { [key]: dynamic[key](dotenv) });
99
101
  } catch ({ message }) {
100
- throw new Error(
101
- `Dynamic processing failed for variable '${key}'. ${message}`
102
- );
102
+ Object.assign(dotenv, { [key]: `ERROR: ${message}` });
103
103
  }
104
104
  });
105
105
  }
@@ -110,6 +110,18 @@ export const getDotenv = async ({
110
110
  // Load process.env.
111
111
  if (loadProcess) Object.assign(process.env, dotenv);
112
112
 
113
+ // Write output file.
114
+ if (outputPath)
115
+ await fs.writeFile(
116
+ outputPath,
117
+ Object.keys(dotenv).reduce((contents, key) => {
118
+ const value = dotenv[key] ?? '';
119
+ return `${contents}${key}=${
120
+ value.includes('\n') ? `"${value}"` : value
121
+ }\n`;
122
+ }, '')
123
+ );
124
+
113
125
  return dotenv;
114
126
  };
115
127
 
@@ -133,6 +145,7 @@ export const getDotenvSync = ({
133
145
  excludePublic = false,
134
146
  loadProcess = false,
135
147
  log = false,
148
+ outputPath,
136
149
  paths = ['./'],
137
150
  privateToken = 'local',
138
151
  } = {}) => {
@@ -196,5 +209,17 @@ export const getDotenvSync = ({
196
209
  // Load process.env.
197
210
  if (loadProcess) Object.assign(process.env, dotenv);
198
211
 
212
+ // Write output file.
213
+ if (outputPath)
214
+ fs.writeFileSync(
215
+ outputPath,
216
+ Object.keys(dotenv).reduce((contents, key) => {
217
+ const value = dotenv[key] ?? '';
218
+ return `${contents}${key}=${
219
+ value.includes('\n') ? `"${value}"` : value
220
+ }\n`;
221
+ }, '')
222
+ );
223
+
199
224
  return dotenv;
200
225
  };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "bin": {
4
4
  "getdotenv": "bin/getdotenv/index.js"
5
5
  },
6
- "version": "2.0.2",
6
+ "version": "2.1.0",
7
7
  "publishConfig": {
8
8
  "access": "public"
9
9
  },
package/test.env ADDED
@@ -0,0 +1,7 @@
1
+ APP_SETTING=root_app_setting
2
+ DYNAMIC_APP_SETTING_1=root_app_setting
3
+ DYNAMIC_APP_SETTING_2=abcroot_app_setting123
4
+ MULTILINE="a
5
+ b
6
+ c"
7
+ APP_SECRET=root_app_secret