@karmaniverous/get-dotenv 2.4.0 → 2.4.2

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
@@ -77,7 +77,7 @@ dotenv files. You can:
77
77
  * Load variables for a specific environment or none.
78
78
  * Specify a default environment, override the default with an existing
79
79
  environment variable, and override both with a direct setting.
80
- * Drive the current git branch to an environment variable.
80
+ * Derive the default environment from the current git branch
81
81
  * Exclude public, private, global, environment-specific, or dynamic variables.
82
82
  * Execute a &&-delimited series of shell commands after loading variables,
83
83
  optionally ignoring errors.
@@ -90,8 +90,8 @@ Options:
90
90
  -p, --paths <strings...> space-delimited paths to dotenv directory (default './')
91
91
  -y, --dynamic-path <string> dynamic variables path
92
92
  -o, --output-path <string> consolidated output file (follows dotenv-expand rules using loaded env vars)
93
- -b, --git-branch <string> environment variable to populate with current git branch
94
93
  -d, --default-environment <string> default environment (prefix with $ to use environment variable)
94
+ -b, --branch-to-default derive default environment from the current git branch (default: false)
95
95
  -e, --environment <string> designated environment (prefix with $ to use environment variable)
96
96
  -n, --exclude-env exclude environment-specific variables (default: false)
97
97
  -g, --exclude-global exclude global & dynamic variables (default: false)
@@ -169,7 +169,6 @@ get-dotenv options type
169
169
  | [excludeGlobal] | <code>bool</code> | exclude global & dynamic variables (default: false) |
170
170
  | [excludePrivate] | <code>bool</code> | exclude private variables (default: false) |
171
171
  | [excludePublic] | <code>bool</code> | exclude public variables (default: false) |
172
- | [gitBranch] | <code>string</code> | environment variable to populate with current git branch |
173
172
  | [loadProcess] | <code>bool</code> | load dotenv to process.env (default: false) |
174
173
  | [log] | <code>bool</code> | log result to console (default: false) |
175
174
  | [outputPath] | <code>string</code> | if populated, writes consolidated .env file to this path (follows [dotenv-expand rules](https://github.com/motdotla/dotenv-expand/blob/master/tests/.env)) |
@@ -6,7 +6,7 @@ import _ from 'lodash';
6
6
  import { parseArgsStringToArgv } from 'string-argv';
7
7
 
8
8
  // Import package exports.
9
- import { getDotenv } from '@karmaniverous/get-dotenv';
9
+ import { getDotenv, parseBranch } from '@karmaniverous/get-dotenv';
10
10
 
11
11
  // Create CLI.
12
12
  import { program } from 'commander';
@@ -32,7 +32,7 @@ program
32
32
  `* Load variables for a specific environment or none.`,
33
33
  `* Specify a default environment, override the default with an existing`,
34
34
  ` environment variable, and override both with a direct setting.`,
35
- `* Drive the current git branch to an environment variable.`,
35
+ `* Derive the default environment from the current git branch`,
36
36
  `* Exclude public, private, global, environment-specific, or dynamic variables.`,
37
37
  `* Execute a &&-delimited series of shell commands after loading variables,`,
38
38
  ` optionally ignoring errors.`,
@@ -55,13 +55,13 @@ program
55
55
  'consolidated output file (follows dotenv-expand rules using loaded env vars)'
56
56
  )
57
57
  .option(
58
- '-b, --git-branch <string>',
59
- 'environment variable to populate with current git branch',
58
+ '-d, --default-environment <string>',
59
+ 'default environment (prefix with $ to use environment variable)',
60
60
  envMerge
61
61
  )
62
62
  .option(
63
- '-d, --default-environment <string>',
64
- 'default environment (prefix with $ to use environment variable)',
63
+ '-b, --branch-to-default',
64
+ 'derive default environment from the current git branch (default: false)',
65
65
  envMerge
66
66
  )
67
67
  .option(
@@ -98,6 +98,7 @@ program
98
98
  // Parse CLI options from command line.
99
99
  program.parse();
100
100
  const {
101
+ branchToDefault,
101
102
  command,
102
103
  defaultEnvironment,
103
104
  dotenvToken,
@@ -107,7 +108,6 @@ const {
107
108
  excludeGlobal,
108
109
  excludePrivate,
109
110
  excludePublic,
110
- gitBranch,
111
111
  log,
112
112
  outputPath,
113
113
  paths,
@@ -117,8 +117,12 @@ const {
117
117
 
118
118
  if (command && program.args.length) program.error('command specified twice');
119
119
 
120
+ if (branchToDefault) {
121
+ var { envToken } = parseBranch();
122
+ }
123
+
120
124
  // Get environment.
121
- const env = environment ?? defaultEnvironment;
125
+ const env = environment ?? envToken ?? defaultEnvironment;
122
126
 
123
127
  // Load dotenvs.
124
128
  await getDotenv({
@@ -128,7 +132,6 @@ await getDotenv({
128
132
  excludeGlobal,
129
133
  excludePrivate,
130
134
  excludePublic,
131
- gitBranch,
132
135
  loadProcess: true,
133
136
  dynamicPath,
134
137
  log,
@@ -6,10 +6,9 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.getDotenvSync = exports.getDotenv = void 0;
7
7
  var _dotenvExpand = require("dotenv-expand");
8
8
  var _fsExtra = _interopRequireDefault(require("fs-extra"));
9
- var _gitBranch = _interopRequireDefault(require("git-branch"));
10
9
  var _path = _interopRequireDefault(require("path"));
11
10
  var _uuid = require("uuid");
12
- var _readDotenv = require("../readDotEnv/readDotenv.js");
11
+ var _readDotenv = require("./readDotenv.js");
13
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
14
13
  // npm imports
15
14
 
@@ -28,7 +27,6 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
28
27
  * @property {bool} [excludeGlobal] - exclude global & dynamic variables (default: false)
29
28
  * @property {bool} [excludePrivate] - exclude private variables (default: false)
30
29
  * @property {bool} [excludePublic] - exclude public variables (default: false)
31
- * @property {string} [gitBranch] - environment variable to populate with current git branch
32
30
  * @property {bool} [loadProcess] - load dotenv to process.env (default: false)
33
31
  * @property {bool} [log] - log result to console (default: false)
34
32
  * @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})
@@ -56,7 +54,6 @@ const getDotenv = async function () {
56
54
  excludeGlobal = false,
57
55
  excludePrivate = false,
58
56
  excludePublic = false,
59
- gitBranch,
60
57
  loadProcess = false,
61
58
  log = false,
62
59
  outputPath,
@@ -88,9 +85,6 @@ const getDotenv = async function () {
88
85
  }
89
86
  });
90
87
 
91
- // Add git branch.
92
- if (gitBranch) dotenv[gitBranch] = await (0, _gitBranch.default)();
93
-
94
88
  // Process dynamic variables.
95
89
  if (dynamicPath && !excludeDynamic) {
96
90
  const dynamic = new Function(`return ${(await _fsExtra.default.readFile(dynamicPath)).toString()}`)()(dotenv);
@@ -127,7 +121,6 @@ const getDotenv = async function () {
127
121
  excludeGlobal,
128
122
  excludePrivate,
129
123
  excludePublic,
130
- gitBranch,
131
124
  loadProcess,
132
125
  log,
133
126
  outputPath,
@@ -158,7 +151,6 @@ const getDotenvSync = function () {
158
151
  excludeGlobal = false,
159
152
  excludePrivate = false,
160
153
  excludePublic = false,
161
- gitBranch,
162
154
  loadProcess = false,
163
155
  log = false,
164
156
  outputPath,
@@ -190,9 +182,6 @@ const getDotenvSync = function () {
190
182
  }
191
183
  });
192
184
 
193
- // Add git branch.
194
- if (gitBranch) dotenv[gitBranch] = _gitBranch.default.sync();
195
-
196
185
  // Process dynamic variables.
197
186
  if (dynamicPath && !excludeDynamic) {
198
187
  const dynamic = new Function(`return ${_fsExtra.default.readFileSync(dynamicPath).toString()}`)()(dotenv);
@@ -229,7 +218,6 @@ const getDotenvSync = function () {
229
218
  excludeGlobal,
230
219
  excludePrivate,
231
220
  excludePublic,
232
- gitBranch,
233
221
  loadProcess,
234
222
  log,
235
223
  outputPath,
@@ -15,4 +15,11 @@ Object.defineProperty(exports, "getDotenvSync", {
15
15
  return _getDotenv.getDotenvSync;
16
16
  }
17
17
  });
18
- var _getDotenv = require("./getDotenv/getDotenv.js");
18
+ Object.defineProperty(exports, "parseBranch", {
19
+ enumerable: true,
20
+ get: function () {
21
+ return _parseBranch.parseBranch;
22
+ }
23
+ });
24
+ var _getDotenv = require("./getDotenv.js");
25
+ var _parseBranch = require("./parseBranch.js");
@@ -0,0 +1,20 @@
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
+ try {
14
+ branchName ??= _gitBranch.default.sync();
15
+ } catch {
16
+ branchName ??= '';
17
+ }
18
+ return _lodash.default.omitBy(branchName.match(/^(?<branchType>[^/\s]+)(?:\/(?<branchLabel>[^/\s]+)(?:\/(?<envToken>[^/\s]+))?)?$/)?.groups ?? {}, _lodash.default.isNil);
19
+ };
20
+ exports.parseBranch = parseBranch;
@@ -1,12 +1,11 @@
1
1
  // npm imports
2
2
  import { expand } from 'dotenv-expand';
3
3
  import fs from 'fs-extra';
4
- import branch from 'git-branch';
5
4
  import path from 'path';
6
5
  import { v4 as uuid } from 'uuid';
7
6
 
8
7
  // lib imports
9
- import { readDotenv, readDotenvSync } from '../readDotEnv/readDotenv.js';
8
+ import { readDotenv, readDotenvSync } from './readDotenv.js';
10
9
 
11
10
  /**
12
11
  * get-dotenv options type
@@ -21,7 +20,6 @@ import { readDotenv, readDotenvSync } from '../readDotEnv/readDotenv.js';
21
20
  * @property {bool} [excludeGlobal] - exclude global & dynamic variables (default: false)
22
21
  * @property {bool} [excludePrivate] - exclude private variables (default: false)
23
22
  * @property {bool} [excludePublic] - exclude public variables (default: false)
24
- * @property {string} [gitBranch] - environment variable to populate with current git branch
25
23
  * @property {bool} [loadProcess] - load dotenv to process.env (default: false)
26
24
  * @property {bool} [log] - log result to console (default: false)
27
25
  * @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})
@@ -48,7 +46,6 @@ export const getDotenv = async ({
48
46
  excludeGlobal = false,
49
47
  excludePrivate = false,
50
48
  excludePublic = false,
51
- gitBranch,
52
49
  loadProcess = false,
53
50
  log = false,
54
51
  outputPath,
@@ -96,9 +93,6 @@ export const getDotenv = async ({
96
93
  },
97
94
  });
98
95
 
99
- // Add git branch.
100
- if (gitBranch) dotenv[gitBranch] = await branch();
101
-
102
96
  // Process dynamic variables.
103
97
  if (dynamicPath && !excludeDynamic) {
104
98
  const dynamic = new Function(
@@ -146,7 +140,6 @@ export const getDotenv = async ({
146
140
  excludeGlobal,
147
141
  excludePrivate,
148
142
  excludePublic,
149
- gitBranch,
150
143
  loadProcess,
151
144
  log,
152
145
  outputPath,
@@ -176,7 +169,6 @@ export const getDotenvSync = ({
176
169
  excludeGlobal = false,
177
170
  excludePrivate = false,
178
171
  excludePublic = false,
179
- gitBranch,
180
172
  loadProcess = false,
181
173
  log = false,
182
174
  outputPath,
@@ -224,9 +216,6 @@ export const getDotenvSync = ({
224
216
  },
225
217
  });
226
218
 
227
- // Add git branch.
228
- if (gitBranch) dotenv[gitBranch] = branch.sync();
229
-
230
219
  // Process dynamic variables.
231
220
  if (dynamicPath && !excludeDynamic) {
232
221
  const dynamic = new Function(
@@ -274,7 +263,6 @@ export const getDotenvSync = ({
274
263
  excludeGlobal,
275
264
  excludePrivate,
276
265
  excludePublic,
277
- gitBranch,
278
266
  loadProcess,
279
267
  log,
280
268
  outputPath,
package/lib/index.js CHANGED
@@ -1 +1,2 @@
1
- export { getDotenv, getDotenvSync } from './getDotenv/getDotenv.js';
1
+ export { getDotenv, getDotenvSync } from './getDotenv.js';
2
+ export { parseBranch } from './parseBranch.js';
@@ -0,0 +1,18 @@
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
+ };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "bin": {
4
4
  "getdotenv": "bin/getdotenv/index.js"
5
5
  },
6
- "version": "2.4.0",
6
+ "version": "2.4.2",
7
7
  "publishConfig": {
8
8
  "access": "public"
9
9
  },
File without changes