@karmaniverous/get-dotenv 2.5.0 → 2.6.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/bin/getdotenv/index.js +6 -12
- package/dist/default/lib/dotenvExpand.js +47 -0
- package/dist/default/lib/index.js +7 -0
- package/lib/dotenvExpand.js +49 -0
- package/lib/index.js +1 -0
- package/package.json +1 -1
package/bin/getdotenv/index.js
CHANGED
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
// Import npm packages.
|
|
4
4
|
import spawn from 'cross-spawn';
|
|
5
|
-
import _ from 'lodash';
|
|
6
5
|
import { parseArgsStringToArgv } from 'string-argv';
|
|
7
6
|
|
|
8
7
|
// Import package exports.
|
|
9
8
|
import {
|
|
9
|
+
dotenvExpand,
|
|
10
10
|
getAwsSsoCredentials,
|
|
11
11
|
getDotenv,
|
|
12
12
|
parseBranch,
|
|
@@ -15,11 +15,6 @@ import {
|
|
|
15
15
|
// Create CLI.
|
|
16
16
|
import { program } from 'commander';
|
|
17
17
|
|
|
18
|
-
const envMerge = (value) =>
|
|
19
|
-
!_.isUndefined(value) && value.startsWith('$')
|
|
20
|
-
? process.env[value.slice(1)]
|
|
21
|
-
: value;
|
|
22
|
-
|
|
23
18
|
// CLI description.
|
|
24
19
|
program
|
|
25
20
|
.name('getdotenv')
|
|
@@ -62,17 +57,17 @@ program
|
|
|
62
57
|
.option(
|
|
63
58
|
'-d, --default-environment <string>',
|
|
64
59
|
'default environment (prefix with $ to use environment variable)',
|
|
65
|
-
|
|
60
|
+
dotenvExpand
|
|
66
61
|
)
|
|
67
62
|
.option(
|
|
68
63
|
'-b, --branch-to-default',
|
|
69
64
|
'derive default environment from the current git branch (default: false)',
|
|
70
|
-
|
|
65
|
+
dotenvExpand
|
|
71
66
|
)
|
|
72
67
|
.option(
|
|
73
68
|
'-e, --environment <string>',
|
|
74
69
|
'designated environment (prefix with $ to use environment variable)',
|
|
75
|
-
|
|
70
|
+
dotenvExpand
|
|
76
71
|
)
|
|
77
72
|
.option(
|
|
78
73
|
'-n, --exclude-env',
|
|
@@ -101,8 +96,7 @@ program
|
|
|
101
96
|
)
|
|
102
97
|
.option(
|
|
103
98
|
'-a, --aws-sso-profile <string>',
|
|
104
|
-
'local AWS SSO profile (follows dotenv-expand rules)'
|
|
105
|
-
envMerge
|
|
99
|
+
'local AWS SSO profile (follows dotenv-expand rules)'
|
|
106
100
|
);
|
|
107
101
|
|
|
108
102
|
// Parse CLI options from command line.
|
|
@@ -152,7 +146,7 @@ await getDotenv({
|
|
|
152
146
|
});
|
|
153
147
|
|
|
154
148
|
// Get AWS SSO credentials.
|
|
155
|
-
if (awsSsoProfile) getAwsSsoCredentials(awsSsoProfile);
|
|
149
|
+
if (awsSsoProfile) getAwsSsoCredentials(dotenvExpand(awsSsoProfile));
|
|
156
150
|
|
|
157
151
|
// Execute shell command.
|
|
158
152
|
if (command || program.args.length) {
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.dotenvExpand = void 0;
|
|
7
|
+
// like String.prototype.search but returns the last index
|
|
8
|
+
const _searchLast = (str, rgx) => {
|
|
9
|
+
const matches = Array.from(str.matchAll(rgx));
|
|
10
|
+
return matches.length > 0 ? matches.slice(-1)[0].index : -1;
|
|
11
|
+
};
|
|
12
|
+
const _interpolate = envValue => {
|
|
13
|
+
// find the last unescaped dollar sign in the
|
|
14
|
+
// value so that we can evaluate it
|
|
15
|
+
const lastUnescapedDollarSignIndex = _searchLast(envValue, /(?!(?<=\\))\$/g);
|
|
16
|
+
|
|
17
|
+
// If we couldn't match any unescaped dollar sign
|
|
18
|
+
// let's return the string as is
|
|
19
|
+
if (lastUnescapedDollarSignIndex === -1) return envValue;
|
|
20
|
+
|
|
21
|
+
// This is the right-most group of variables in the string
|
|
22
|
+
const rightMostGroup = envValue.slice(lastUnescapedDollarSignIndex);
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* This finds the inner most variable/group divided
|
|
26
|
+
* by variable name and default value (if present)
|
|
27
|
+
* (
|
|
28
|
+
* (?!(?<=\\))\$ // only match dollar signs that are not escaped
|
|
29
|
+
* {? // optional opening curly brace
|
|
30
|
+
* ([\w]+) // match the variable name
|
|
31
|
+
* (?::-([^}\\]*))? // match an optional default value
|
|
32
|
+
* }? // optional closing curly brace
|
|
33
|
+
* )
|
|
34
|
+
*/
|
|
35
|
+
const matchGroup = /((?!(?<=\\))\${?([\w]+)(?::-([^}\\]*))?}?)/;
|
|
36
|
+
const match = rightMostGroup.match(matchGroup);
|
|
37
|
+
if (match != null) {
|
|
38
|
+
const [, group, variableName, defaultValue] = match;
|
|
39
|
+
return _interpolate(envValue.replace(group, process.env[variableName] || defaultValue || ''));
|
|
40
|
+
}
|
|
41
|
+
return envValue;
|
|
42
|
+
};
|
|
43
|
+
const _resolveEscapeSequences = value => {
|
|
44
|
+
return value.replace(/\\\$/g, '$');
|
|
45
|
+
};
|
|
46
|
+
const dotenvExpand = value => value ? _resolveEscapeSequences(_interpolate(value)) : value;
|
|
47
|
+
exports.dotenvExpand = dotenvExpand;
|
|
@@ -3,6 +3,12 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
+
Object.defineProperty(exports, "dotenvExpand", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function () {
|
|
9
|
+
return _dotenvExpand.dotenvExpand;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
6
12
|
Object.defineProperty(exports, "getAwsSsoCredentials", {
|
|
7
13
|
enumerable: true,
|
|
8
14
|
get: function () {
|
|
@@ -27,6 +33,7 @@ Object.defineProperty(exports, "parseBranch", {
|
|
|
27
33
|
return _parseBranch.parseBranch;
|
|
28
34
|
}
|
|
29
35
|
});
|
|
36
|
+
var _dotenvExpand = require("./dotenvExpand.js");
|
|
30
37
|
var _getAwsSsoCredentials = require("./getAwsSsoCredentials.js");
|
|
31
38
|
var _getDotenv = require("./getDotenv.js");
|
|
32
39
|
var _parseBranch = require("./parseBranch.js");
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// like String.prototype.search but returns the last index
|
|
2
|
+
const _searchLast = (str, rgx) => {
|
|
3
|
+
const matches = Array.from(str.matchAll(rgx));
|
|
4
|
+
return matches.length > 0 ? matches.slice(-1)[0].index : -1;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
const _interpolate = (envValue) => {
|
|
8
|
+
// find the last unescaped dollar sign in the
|
|
9
|
+
// value so that we can evaluate it
|
|
10
|
+
const lastUnescapedDollarSignIndex = _searchLast(envValue, /(?!(?<=\\))\$/g);
|
|
11
|
+
|
|
12
|
+
// If we couldn't match any unescaped dollar sign
|
|
13
|
+
// let's return the string as is
|
|
14
|
+
if (lastUnescapedDollarSignIndex === -1) return envValue;
|
|
15
|
+
|
|
16
|
+
// This is the right-most group of variables in the string
|
|
17
|
+
const rightMostGroup = envValue.slice(lastUnescapedDollarSignIndex);
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* This finds the inner most variable/group divided
|
|
21
|
+
* by variable name and default value (if present)
|
|
22
|
+
* (
|
|
23
|
+
* (?!(?<=\\))\$ // only match dollar signs that are not escaped
|
|
24
|
+
* {? // optional opening curly brace
|
|
25
|
+
* ([\w]+) // match the variable name
|
|
26
|
+
* (?::-([^}\\]*))? // match an optional default value
|
|
27
|
+
* }? // optional closing curly brace
|
|
28
|
+
* )
|
|
29
|
+
*/
|
|
30
|
+
const matchGroup = /((?!(?<=\\))\${?([\w]+)(?::-([^}\\]*))?}?)/;
|
|
31
|
+
const match = rightMostGroup.match(matchGroup);
|
|
32
|
+
|
|
33
|
+
if (match != null) {
|
|
34
|
+
const [, group, variableName, defaultValue] = match;
|
|
35
|
+
|
|
36
|
+
return _interpolate(
|
|
37
|
+
envValue.replace(group, process.env[variableName] || defaultValue || '')
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return envValue;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const _resolveEscapeSequences = (value) => {
|
|
45
|
+
return value.replace(/\\\$/g, '$');
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export const dotenvExpand = (value) =>
|
|
49
|
+
value ? _resolveEscapeSequences(_interpolate(value)) : value;
|
package/lib/index.js
CHANGED