@karmaniverous/get-dotenv 2.6.8 → 3.0.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/lib/getDotenv.js CHANGED
@@ -2,9 +2,10 @@
2
2
  import { expand } from 'dotenv-expand';
3
3
  import fs from 'fs-extra';
4
4
  import path from 'path';
5
- import { v4 as uuid } from 'uuid';
5
+ import { nanoid } from 'nanoid';
6
6
 
7
7
  // lib imports
8
+ import { dotenvDefaults } from './dotenvDefaults.js';
8
9
  import { readDotenv, readDotenvSync } from './readDotenv.js';
9
10
 
10
11
  /**
@@ -12,19 +13,21 @@ import { readDotenv, readDotenvSync } from './readDotenv.js';
12
13
  *
13
14
  * @typedef {Object} OptionsType
14
15
  *
15
- * @property {string} [dotenvToken] - token indicating a dotenv file (default: '.env')
16
+ * @property {string} [dotenvToken] - token indicating a dotenv file
16
17
  * @property {string} [dynamicPath] - path to file exporting an object keyed to dynamic variable functions
17
18
  * @property {string} [env] - target environment
18
- * @property {bool} [excludeDynamic] - exclude dynamic variables (default: false)
19
- * @property {bool} [excludeEnv] - exclude environment-specific variables (default: false)
20
- * @property {bool} [excludeGlobal] - exclude global & dynamic variables (default: false)
21
- * @property {bool} [excludePrivate] - exclude private variables (default: false)
22
- * @property {bool} [excludePublic] - exclude public variables (default: false)
23
- * @property {bool} [loadProcess] - load dotenv to process.env (default: false)
24
- * @property {bool} [log] - log result to console (default: false)
19
+ * @property {bool} [excludeDynamic] - exclude dynamic variables
20
+ * @property {bool} [excludeEnv] - exclude environment-specific variables
21
+ * @property {bool} [excludeGlobal] - exclude global & dynamic variables
22
+ * @property {bool} [excludePrivate] - exclude private variables
23
+ * @property {bool} [excludePublic] - exclude public variables
24
+ * @property {bool} [loadProcess] - load dotenv to process.env
25
+ * @property {bool} [log] - log result to logger
26
+ * @property {function} [logger] - logger function
25
27
  * @property {string} [outputPath] - if populated, writes consolidated .env file to this path (follows {@link https://github.com/motdotla/dotenv-expand/blob/master/tests/.env dotenv-expand rules})
26
- * @property {string[]} [paths] - array of input directory paths (default ['./'])
27
- * @property {string} [privateToken] - token indicating private variables (default: 'local').
28
+ * @property {string[]} [paths] - array of input directory paths
29
+ * @property {string} [privateToken] - token indicating private variables
30
+ * @property {object} [vars] - explicit variables to include
28
31
  */
29
32
 
30
33
  /**
@@ -37,58 +40,64 @@ import { readDotenv, readDotenvSync } from './readDotenv.js';
37
40
  *
38
41
  * @returns {Promise<object>} The combined parsed dotenv object.
39
42
  */
40
- export const getDotenv = async ({
41
- dotenvToken = '.env',
42
- env,
43
- dynamicPath,
44
- excludeDynamic = false,
45
- excludeEnv = false,
46
- excludeGlobal = false,
47
- excludePrivate = false,
48
- excludePublic = false,
49
- loadProcess = false,
50
- log = false,
51
- outputPath,
52
- paths = ['./'],
53
- privateToken = 'local',
54
- } = {}) => {
43
+ export const getDotenv = async (options = {}) => {
44
+ // Apply defaults.
45
+ let {
46
+ dotenvToken,
47
+ env,
48
+ dynamicPath,
49
+ excludeDynamic,
50
+ excludeEnv,
51
+ excludeGlobal,
52
+ excludePrivate,
53
+ excludePublic,
54
+ loadProcess,
55
+ log,
56
+ logger,
57
+ outputPath,
58
+ paths,
59
+ privateToken,
60
+ vars = {},
61
+ } = { ...dotenvDefaults, ...options };
62
+
55
63
  // Read .env files.
56
- const parsed = await paths.reduce(
57
- async (e, p) => ({
58
- ...(await e),
59
- ...(excludePublic
64
+ const loaded = await paths.reduce(async (e, p) => {
65
+ let publicGlobal =
66
+ excludePublic || excludeGlobal
67
+ ? {}
68
+ : readDotenv(path.resolve(p, dotenvToken));
69
+ let publicEnv =
70
+ excludePublic || excludeEnv
60
71
  ? {}
61
- : {
62
- ...(excludeGlobal
63
- ? {}
64
- : await readDotenv(path.resolve(p, dotenvToken))),
65
- ...(env && !excludeEnv
66
- ? await readDotenv(path.resolve(p, `${dotenvToken}.${env}`))
67
- : {}),
68
- }),
69
- ...(excludePrivate
72
+ : readDotenv(path.resolve(p, `${dotenvToken}.${env}`));
73
+ let privateGlobal =
74
+ excludePrivate || excludeGlobal
70
75
  ? {}
71
- : {
72
- ...(excludeGlobal
73
- ? {}
74
- : await readDotenv(
75
- path.resolve(p, `${dotenvToken}.${privateToken}`)
76
- )),
77
- ...(env && !excludeEnv
78
- ? await readDotenv(
79
- path.resolve(p, `${dotenvToken}.${env}.${privateToken}`)
80
- )
81
- : {}),
82
- }),
83
- }),
84
- {}
85
- );
76
+ : readDotenv(path.resolve(p, `${dotenvToken}.${privateToken}`));
77
+ let privateEnv =
78
+ excludePrivate || excludeEnv
79
+ ? {}
80
+ : readDotenv(path.resolve(p, `${dotenvToken}.${env}.${privateToken}`));
81
+
82
+ [e, publicGlobal, publicEnv, privateGlobal, privateEnv] = await Promise.all(
83
+ [e, publicGlobal, publicEnv, privateGlobal, privateEnv]
84
+ );
85
+
86
+ return {
87
+ ...e,
88
+ ...publicGlobal,
89
+ ...publicEnv,
90
+ ...privateGlobal,
91
+ ...privateEnv,
92
+ };
93
+ }, {});
86
94
 
87
- const outputKey = uuid();
95
+ const outputKey = nanoid();
88
96
  const { parsed: dotenv } = expand({
89
97
  ignoreProcessEnv: true,
90
98
  parsed: {
91
- ...parsed,
99
+ ...loaded,
100
+ ...vars,
92
101
  ...(outputPath ? { [outputKey]: outputPath } : {}),
93
102
  },
94
103
  });
@@ -126,15 +135,15 @@ export const getDotenv = async ({
126
135
  }
127
136
 
128
137
  // Log result.
129
- if (log) console.log(dotenv);
138
+ if (log) logger(dotenv);
130
139
 
131
140
  // Load process.env.
132
141
  if (loadProcess)
133
142
  Object.assign(process.env, dotenv, {
134
143
  getdotenvOptions: JSON.stringify({
135
144
  dotenvToken,
136
- dynamicPath,
137
145
  env,
146
+ dynamicPath,
138
147
  excludeDynamic,
139
148
  excludeEnv,
140
149
  excludeGlobal,
@@ -160,58 +169,61 @@ export const getDotenv = async ({
160
169
  *
161
170
  * @returns {Object} The combined parsed dotenv object.
162
171
  */
163
- export const getDotenvSync = ({
164
- dotenvToken = '.env',
165
- dynamicPath,
166
- env,
167
- excludeDynamic = false,
168
- excludeEnv = false,
169
- excludeGlobal = false,
170
- excludePrivate = false,
171
- excludePublic = false,
172
- loadProcess = false,
173
- log = false,
174
- outputPath,
175
- paths = ['./'],
176
- privateToken = 'local',
177
- } = {}) => {
172
+ export const getDotenvSync = (options = {}) => {
173
+ // Apply defaults.
174
+ let {
175
+ dotenvToken,
176
+ env,
177
+ dynamicPath,
178
+ excludeDynamic,
179
+ excludeEnv,
180
+ excludeGlobal,
181
+ excludePrivate,
182
+ excludePublic,
183
+ loadProcess,
184
+ log,
185
+ outputPath,
186
+ paths,
187
+ privateToken,
188
+ vars,
189
+ } = { ...dotenvDefaults, ...options };
190
+
178
191
  // Read .env files.
179
- const parsed = paths.reduce(
180
- (e, p) => ({
181
- ...e,
182
- ...(excludePublic
192
+ const loaded = paths.reduce((e, p) => {
193
+ let publicGlobal =
194
+ excludePublic || excludeGlobal
183
195
  ? {}
184
- : {
185
- ...(excludeGlobal
186
- ? {}
187
- : readDotenvSync(path.resolve(p, dotenvToken))),
188
- ...(env && !excludeEnv
189
- ? readDotenvSync(path.resolve(p, `${dotenvToken}.${env}`))
190
- : {}),
191
- }),
192
- ...(excludePrivate
196
+ : readDotenvSync(path.resolve(p, dotenvToken));
197
+ let publicEnv =
198
+ excludePublic || excludeEnv
193
199
  ? {}
194
- : {
195
- ...(excludeGlobal
196
- ? {}
197
- : readDotenvSync(
198
- path.resolve(p, `${dotenvToken}.${privateToken}`)
199
- )),
200
- ...(env && !excludeEnv
201
- ? readDotenvSync(
202
- path.resolve(p, `${dotenvToken}.${env}.${privateToken}`)
203
- )
204
- : {}),
205
- }),
206
- }),
207
- {}
208
- );
200
+ : readDotenvSync(path.resolve(p, `${dotenvToken}.${env}`));
201
+ let privateGlobal =
202
+ excludePrivate || excludeGlobal
203
+ ? {}
204
+ : readDotenvSync(path.resolve(p, `${dotenvToken}.${privateToken}`));
205
+ let privateEnv =
206
+ excludePrivate || excludeEnv
207
+ ? {}
208
+ : readDotenvSync(
209
+ path.resolve(p, `${dotenvToken}.${env}.${privateToken}`)
210
+ );
211
+
212
+ return {
213
+ ...e,
214
+ ...publicGlobal,
215
+ ...publicEnv,
216
+ ...privateGlobal,
217
+ ...privateEnv,
218
+ };
219
+ }, {});
209
220
 
210
- const outputKey = uuid();
221
+ const outputKey = nanoid();
211
222
  const { parsed: dotenv } = expand({
212
223
  ignoreProcessEnv: true,
213
224
  parsed: {
214
- ...parsed,
225
+ ...loaded,
226
+ ...vars,
215
227
  ...(outputPath ? { [outputKey]: outputPath } : {}),
216
228
  },
217
229
  });
package/lib/index.js CHANGED
@@ -1,4 +1,3 @@
1
1
  export { dotenvExpand } from './dotenvExpand.js';
2
- export { getAwsSsoCredentials } from './getAwsSsoCredentials.js';
3
2
  export { getDotenv, getDotenvSync } from './getDotenv.js';
4
- export { parseBranch } from './parseBranch.js';
3
+ export { getCli } from './getCli.js';
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "bin": {
4
4
  "getdotenv": "bin/getdotenv/index.js"
5
5
  },
6
- "version": "2.6.8",
6
+ "version": "3.0.1",
7
7
  "publishConfig": {
8
8
  "access": "public"
9
9
  },
@@ -31,14 +31,15 @@
31
31
  ],
32
32
  "license": "BSD-3-Clause",
33
33
  "dependencies": {
34
+ "boolean": "^3.2.0",
34
35
  "commander": "^11.0.0",
35
- "cross-spawn": "^7.0.3",
36
36
  "dotenv": "^16.3.1",
37
37
  "dotenv-expand": "^10.0.0",
38
+ "execa": "^7.1.1",
38
39
  "fs-extra": "^11.1.1",
39
- "git-branch": "^2.0.1",
40
- "string-argv": "^0.3.2",
41
- "uuid": "^9.0.0"
40
+ "lodash.frompairs": "^4.0.1",
41
+ "lodash.isstring": "^4.0.1",
42
+ "nanoid": "^4.0.2"
42
43
  },
43
44
  "devDependencies": {
44
45
  "@babel/cli": "^7.22.5",
@@ -46,10 +47,10 @@
46
47
  "@babel/eslint-parser": "^7.22.5",
47
48
  "@babel/preset-env": "^7.22.5",
48
49
  "@babel/register": "^7.22.5",
49
- "@types/node": "^20.3.1",
50
+ "@types/node": "^20.3.3",
50
51
  "chai": "^4.3.7",
51
52
  "concat-md": "^0.5.1",
52
- "eslint": "^8.43.0",
53
+ "eslint": "^8.44.0",
53
54
  "eslint-plugin-mocha": "^10.1.0",
54
55
  "eslint-plugin-promise": "^6.1.1",
55
56
  "jsdoc-to-markdown": "^8.0.0",
@@ -1,33 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.getAwsSsoCredentials = void 0;
7
- var _crossSpawn = _interopRequireDefault(require("cross-spawn"));
8
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
9
- // npm imports
10
-
11
- const getAwsSsoCredentials = localProfile => {
12
- if (!(localProfile !== null && localProfile !== void 0 && localProfile.length)) {
13
- delete process.env.AWS_ACCESS_KEY_ID;
14
- delete process.env.AWS_SECRET_ACCESS_KEY;
15
- delete process.env.AWS_SESSION_TOKEN;
16
- return;
17
- }
18
- const {
19
- status,
20
- stderr,
21
- stdout
22
- } = _crossSpawn.default.sync('aws', ['configure', 'export-credentials', '--profile', localProfile]);
23
- if (status) throw new Error(stderr.toString());
24
- const {
25
- AccessKeyId,
26
- SecretAccessKey,
27
- SessionToken
28
- } = JSON.parse(stdout.toString());
29
- process.env.AWS_ACCESS_KEY_ID = AccessKeyId;
30
- process.env.AWS_SECRET_ACCESS_KEY = SecretAccessKey;
31
- process.env.AWS_SESSION_TOKEN = SessionToken;
32
- };
33
- exports.getAwsSsoCredentials = getAwsSsoCredentials;
@@ -1,21 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.parseBranch = void 0;
7
- var _gitBranch = _interopRequireDefault(require("git-branch"));
8
- var _lodash = _interopRequireDefault(require("lodash"));
9
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
- // npm imports
11
-
12
- const parseBranch = branchName => {
13
- var _branchName$match;
14
- try {
15
- branchName ??= _gitBranch.default.sync() ?? '';
16
- } catch {
17
- branchName ??= '';
18
- }
19
- return _lodash.default.omitBy(((_branchName$match = branchName.match(/^(?:(?<branchType>[^/\s]+)\/(?:(?<branchLabel>[^/\s]+)))?(?:\/?(?<envToken>[^/\s]+))?$/)) === null || _branchName$match === void 0 ? void 0 : _branchName$match.groups) ?? {}, _lodash.default.isNil);
20
- };
21
- exports.parseBranch = parseBranch;
@@ -1,28 +0,0 @@
1
- // npm imports
2
- import spawn from 'cross-spawn';
3
-
4
- export const getAwsSsoCredentials = (localProfile) => {
5
- if (!localProfile?.length) {
6
- delete process.env.AWS_ACCESS_KEY_ID;
7
- delete process.env.AWS_SECRET_ACCESS_KEY;
8
- delete process.env.AWS_SESSION_TOKEN;
9
- return;
10
- }
11
-
12
- const { status, stderr, stdout } = spawn.sync('aws', [
13
- 'configure',
14
- 'export-credentials',
15
- '--profile',
16
- localProfile,
17
- ]);
18
-
19
- if (status) throw new Error(stderr.toString());
20
-
21
- const { AccessKeyId, SecretAccessKey, SessionToken } = JSON.parse(
22
- stdout.toString()
23
- );
24
-
25
- process.env.AWS_ACCESS_KEY_ID = AccessKeyId;
26
- process.env.AWS_SECRET_ACCESS_KEY = SecretAccessKey;
27
- process.env.AWS_SESSION_TOKEN = SessionToken;
28
- };
@@ -1,18 +0,0 @@
1
- // npm imports
2
- import branch from 'git-branch';
3
- import _ from 'lodash';
4
-
5
- export const parseBranch = (branchName) => {
6
- try {
7
- branchName ??= branch.sync() ?? '';
8
- } catch {
9
- branchName ??= '';
10
- }
11
-
12
- return _.omitBy(
13
- branchName.match(
14
- /^(?:(?<branchType>[^/\s]+)\/(?:(?<branchLabel>[^/\s]+)))?(?:\/?(?<envToken>[^/\s]+))?$/
15
- )?.groups ?? {},
16
- _.isNil
17
- );
18
- };