@karmaniverous/get-dotenv 2.0.3 → 2.1.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/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] : {};
@@ -101,6 +103,14 @@ const getDotenv = async function () {
101
103
 
102
104
  // Load process.env.
103
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
+ }, ''), {
112
+ encoding: 'utf-8'
113
+ });
104
114
  return dotenv;
105
115
  };
106
116
 
@@ -126,6 +136,7 @@ const getDotenvSync = function () {
126
136
  excludePublic = false,
127
137
  loadProcess = false,
128
138
  log = false,
139
+ outputPath,
129
140
  paths = ['./'],
130
141
  privateToken = 'local'
131
142
  } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
@@ -165,6 +176,14 @@ const getDotenvSync = function () {
165
176
 
166
177
  // Load process.env.
167
178
  if (loadProcess) Object.assign(process.env, dotenv);
179
+
180
+ // Write output file.
181
+ if (outputPath) _fsExtra.default.writeFileSync(outputPath, Object.keys(dotenv).reduce((contents, key) => {
182
+ const value = dotenv[key] ?? '';
183
+ return `${contents}${key}=${value.includes('\n') ? `"${value}"` : value}\n`;
184
+ }, ''), {
185
+ encoding: 'utf-8'
186
+ });
168
187
  return dotenv;
169
188
  };
170
189
  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
  } = {}) => {
@@ -108,6 +110,19 @@ export const getDotenv = async ({
108
110
  // Load process.env.
109
111
  if (loadProcess) Object.assign(process.env, dotenv);
110
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
+ { encoding: 'utf-8' }
124
+ );
125
+
111
126
  return dotenv;
112
127
  };
113
128
 
@@ -131,6 +146,7 @@ export const getDotenvSync = ({
131
146
  excludePublic = false,
132
147
  loadProcess = false,
133
148
  log = false,
149
+ outputPath,
134
150
  paths = ['./'],
135
151
  privateToken = 'local',
136
152
  } = {}) => {
@@ -194,5 +210,18 @@ export const getDotenvSync = ({
194
210
  // Load process.env.
195
211
  if (loadProcess) Object.assign(process.env, dotenv);
196
212
 
213
+ // Write output file.
214
+ if (outputPath)
215
+ fs.writeFileSync(
216
+ outputPath,
217
+ Object.keys(dotenv).reduce((contents, key) => {
218
+ const value = dotenv[key] ?? '';
219
+ return `${contents}${key}=${
220
+ value.includes('\n') ? `"${value}"` : value
221
+ }\n`;
222
+ }, ''),
223
+ { encoding: 'utf-8' }
224
+ );
225
+
197
226
  return dotenv;
198
227
  };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "bin": {
4
4
  "getdotenv": "bin/getdotenv/index.js"
5
5
  },
6
- "version": "2.0.3",
6
+ "version": "2.1.1",
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