@karmaniverous/get-dotenv 2.6.8 → 3.0.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/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,20 @@ 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
28
30
  */
29
31
 
30
32
  /**
@@ -37,54 +39,58 @@ import { readDotenv, readDotenvSync } from './readDotenv.js';
37
39
  *
38
40
  * @returns {Promise<object>} The combined parsed dotenv object.
39
41
  */
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
- } = {}) => {
42
+ export const getDotenv = async (options = {}) => {
43
+ // Apply defaults.
44
+ let {
45
+ dotenvToken,
46
+ env,
47
+ dynamicPath,
48
+ excludeDynamic,
49
+ excludeEnv,
50
+ excludeGlobal,
51
+ excludePrivate,
52
+ excludePublic,
53
+ loadProcess,
54
+ log,
55
+ logger,
56
+ outputPath,
57
+ paths,
58
+ privateToken,
59
+ } = { ...dotenvDefaults, ...options };
60
+
55
61
  // Read .env files.
56
- const parsed = await paths.reduce(
57
- async (e, p) => ({
58
- ...(await e),
59
- ...(excludePublic
62
+ const parsed = await paths.reduce(async (e, p) => {
63
+ let publicGlobal =
64
+ excludePublic || excludeGlobal
65
+ ? {}
66
+ : readDotenv(path.resolve(p, dotenvToken));
67
+ let publicEnv =
68
+ excludePublic || excludeEnv
60
69
  ? {}
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
70
+ : readDotenv(path.resolve(p, `${dotenvToken}.${env}`));
71
+ let privateGlobal =
72
+ excludePrivate || excludeGlobal
70
73
  ? {}
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
- );
74
+ : readDotenv(path.resolve(p, `${dotenvToken}.${privateToken}`));
75
+ let privateEnv =
76
+ excludePrivate || excludeEnv
77
+ ? {}
78
+ : readDotenv(path.resolve(p, `${dotenvToken}.${env}.${privateToken}`));
79
+
80
+ [e, publicGlobal, publicEnv, privateGlobal, privateEnv] = await Promise.all(
81
+ [e, publicGlobal, publicEnv, privateGlobal, privateEnv]
82
+ );
83
+
84
+ return {
85
+ ...e,
86
+ ...publicGlobal,
87
+ ...publicEnv,
88
+ ...privateGlobal,
89
+ ...privateEnv,
90
+ };
91
+ }, {});
86
92
 
87
- const outputKey = uuid();
93
+ const outputKey = nanoid();
88
94
  const { parsed: dotenv } = expand({
89
95
  ignoreProcessEnv: true,
90
96
  parsed: {
@@ -126,15 +132,15 @@ export const getDotenv = async ({
126
132
  }
127
133
 
128
134
  // Log result.
129
- if (log) console.log(dotenv);
135
+ if (log) logger(dotenv);
130
136
 
131
137
  // Load process.env.
132
138
  if (loadProcess)
133
139
  Object.assign(process.env, dotenv, {
134
140
  getdotenvOptions: JSON.stringify({
135
141
  dotenvToken,
136
- dynamicPath,
137
142
  env,
143
+ dynamicPath,
138
144
  excludeDynamic,
139
145
  excludeEnv,
140
146
  excludeGlobal,
@@ -160,54 +166,55 @@ export const getDotenv = async ({
160
166
  *
161
167
  * @returns {Object} The combined parsed dotenv object.
162
168
  */
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
- } = {}) => {
169
+ export const getDotenvSync = (options = {}) => {
170
+ // Apply defaults.
171
+ let {
172
+ dotenvToken,
173
+ env,
174
+ dynamicPath,
175
+ excludeDynamic,
176
+ excludeEnv,
177
+ excludeGlobal,
178
+ excludePrivate,
179
+ excludePublic,
180
+ loadProcess,
181
+ log,
182
+ outputPath,
183
+ paths,
184
+ privateToken,
185
+ } = { ...dotenvDefaults, ...options };
186
+
178
187
  // Read .env files.
179
- const parsed = paths.reduce(
180
- (e, p) => ({
181
- ...e,
182
- ...(excludePublic
188
+ const parsed = paths.reduce((e, p) => {
189
+ let publicGlobal =
190
+ excludePublic || excludeGlobal
183
191
  ? {}
184
- : {
185
- ...(excludeGlobal
186
- ? {}
187
- : readDotenvSync(path.resolve(p, dotenvToken))),
188
- ...(env && !excludeEnv
189
- ? readDotenvSync(path.resolve(p, `${dotenvToken}.${env}`))
190
- : {}),
191
- }),
192
- ...(excludePrivate
192
+ : readDotenvSync(path.resolve(p, dotenvToken));
193
+ let publicEnv =
194
+ excludePublic || excludeEnv
193
195
  ? {}
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
- );
196
+ : readDotenvSync(path.resolve(p, `${dotenvToken}.${env}`));
197
+ let privateGlobal =
198
+ excludePrivate || excludeGlobal
199
+ ? {}
200
+ : readDotenvSync(path.resolve(p, `${dotenvToken}.${privateToken}`));
201
+ let privateEnv =
202
+ excludePrivate || excludeEnv
203
+ ? {}
204
+ : readDotenvSync(
205
+ path.resolve(p, `${dotenvToken}.${env}.${privateToken}`)
206
+ );
207
+
208
+ return {
209
+ ...e,
210
+ ...publicGlobal,
211
+ ...publicEnv,
212
+ ...privateGlobal,
213
+ ...privateEnv,
214
+ };
215
+ }, {});
209
216
 
210
- const outputKey = uuid();
217
+ const outputKey = nanoid();
211
218
  const { parsed: dotenv } = expand({
212
219
  ignoreProcessEnv: true,
213
220
  parsed: {
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.0",
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
- };