@karmaniverous/get-dotenv 0.3.4-3 → 1.0.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/README.md
CHANGED
|
@@ -46,17 +46,21 @@ See [OptionsType](#optionstype--object) below for configuration options.
|
|
|
46
46
|
|
|
47
47
|
This package supports the full [`dotenv-expand`](https://www.npmjs.com/package/dotenv-expand) syntax.
|
|
48
48
|
|
|
49
|
-
|
|
49
|
+
Yse the `dynamicPath` option to add a relative path to a Javascript file with a function expression like this:
|
|
50
50
|
|
|
51
51
|
```js
|
|
52
|
-
|
|
52
|
+
(dotenv) => ({
|
|
53
53
|
SOME_DYNAMIC_VARIABLE: (dotenv) => someLogic(dotenv),
|
|
54
54
|
ANOTHER_DYNAMIC_VARIABLE: (dotenv) =>
|
|
55
55
|
someOtherLogic(dotenv.SOME_DYNAMIC_VARIABLE),
|
|
56
|
-
};
|
|
56
|
+
});
|
|
57
57
|
```
|
|
58
58
|
|
|
59
|
-
|
|
59
|
+
This function should take the expanded `dotenv` object as an argument and return an object. Each object key will be evaluated progressively.
|
|
60
|
+
|
|
61
|
+
If the corresponding value is a function, it will be executed with the current state of `dotenv` as its single argument and the result applied back to the `dotenv` object. Otherwise, the value will just be applied back to `dotenv`.
|
|
62
|
+
|
|
63
|
+
Since keys will be evaluated progressively, each successive key function will have access to any previous ones. These keys can also override existing variables.
|
|
60
64
|
|
|
61
65
|
# Command Line Interface
|
|
62
66
|
|
|
@@ -4,13 +4,15 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.getDotenvSync = exports.getDotenv = void 0;
|
|
7
|
-
var _path = _interopRequireDefault(require("path"));
|
|
8
7
|
var _dotenvExpand = require("dotenv-expand");
|
|
8
|
+
var _fsExtra = _interopRequireDefault(require("fs-extra"));
|
|
9
|
+
var _path = _interopRequireDefault(require("path"));
|
|
9
10
|
var _readDotenv = require("../readDotEnv/readDotenv.js");
|
|
10
|
-
var _dirname2 = _interopRequireDefault(require("./dirname.js"));
|
|
11
11
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
// npm imports
|
|
13
|
+
|
|
14
|
+
// lib imports
|
|
15
|
+
|
|
14
16
|
/**
|
|
15
17
|
* get-dotenv options type
|
|
16
18
|
*
|
|
@@ -72,31 +74,7 @@ const getDotenv = async function () {
|
|
|
72
74
|
|
|
73
75
|
// Process dynamic variables.
|
|
74
76
|
if (dynamicPath) {
|
|
75
|
-
const {
|
|
76
|
-
default: dynamic
|
|
77
|
-
} = await Promise.resolve(`${function transformExtension(filepath, extMapping) {
|
|
78
|
-
if (!filepath.startsWith('./') && !filepath.startsWith('../')) {
|
|
79
|
-
// Package import
|
|
80
|
-
return filepath;
|
|
81
|
-
}
|
|
82
|
-
const idx = filepath.lastIndexOf('.');
|
|
83
|
-
if (idx === -1 || filepath.includes('/', idx)) {
|
|
84
|
-
// No extension
|
|
85
|
-
const newExt = extMapping[''];
|
|
86
|
-
if (newExt) {
|
|
87
|
-
return filepath + newExt;
|
|
88
|
-
}
|
|
89
|
-
return filepath;
|
|
90
|
-
}
|
|
91
|
-
for (let [origExt, newExt] of Object.entries(extMapping).sort((a, b) => b[0].length - a[0].length)) {
|
|
92
|
-
if (filepath.endsWith(origExt)) {
|
|
93
|
-
return filepath.slice(0, -origExt.length) + newExt;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
return filepath;
|
|
97
|
-
}(_path.default.relative(_dirname2.default, dynamicPath).replace(/\\/g, '/'), {
|
|
98
|
-
".cjs": ".js"
|
|
99
|
-
})}`).then(s => _interopRequireWildcard(require(s)));
|
|
77
|
+
const dynamic = new Function(`return ${(await _fsExtra.default.readFile(dynamicPath)).toString()}`)()(dotenv);
|
|
100
78
|
Object.keys(dynamic).forEach(key => {
|
|
101
79
|
Object.assign(dotenv, {
|
|
102
80
|
[key]: dynamic[key](dotenv)
|
|
@@ -155,8 +133,15 @@ const getDotenvSync = function () {
|
|
|
155
133
|
});
|
|
156
134
|
if (error) throw new Error(error);
|
|
157
135
|
|
|
158
|
-
//
|
|
159
|
-
if (dynamicPath)
|
|
136
|
+
// Process dynamic variables.
|
|
137
|
+
if (dynamicPath) {
|
|
138
|
+
const dynamic = new Function(`return ${_fsExtra.default.readFileSync(dynamicPath).toString()}`)()(dotenv);
|
|
139
|
+
Object.keys(dynamic).forEach(key => {
|
|
140
|
+
Object.assign(dotenv, {
|
|
141
|
+
[key]: typeof dynamic[key] === 'function' ? dynamic[key](dotenv) : dynamic[key]
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
}
|
|
160
145
|
|
|
161
146
|
// Log result.
|
|
162
147
|
if (log) console.log(dotenv);
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
// npm imports
|
|
2
2
|
import { expand } from 'dotenv-expand';
|
|
3
|
+
import fs from 'fs-extra';
|
|
4
|
+
import path from 'path';
|
|
5
|
+
|
|
6
|
+
// lib imports
|
|
3
7
|
import { readDotenv, readDotenvSync } from '../readDotEnv/readDotenv.js';
|
|
4
|
-
import __dirname from './dirname.cjs';
|
|
5
8
|
|
|
6
9
|
/**
|
|
7
10
|
* get-dotenv options type
|
|
@@ -77,10 +80,9 @@ export const getDotenv = async ({
|
|
|
77
80
|
|
|
78
81
|
// Process dynamic variables.
|
|
79
82
|
if (dynamicPath) {
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
);
|
|
83
|
-
|
|
83
|
+
const dynamic = new Function(
|
|
84
|
+
`return ${(await fs.readFile(dynamicPath)).toString()}`
|
|
85
|
+
)()(dotenv);
|
|
84
86
|
Object.keys(dynamic).forEach((key) => {
|
|
85
87
|
Object.assign(dotenv, { [key]: dynamic[key](dotenv) });
|
|
86
88
|
});
|
|
@@ -150,8 +152,20 @@ export const getDotenvSync = ({
|
|
|
150
152
|
|
|
151
153
|
if (error) throw new Error(error);
|
|
152
154
|
|
|
153
|
-
//
|
|
154
|
-
if (dynamicPath)
|
|
155
|
+
// Process dynamic variables.
|
|
156
|
+
if (dynamicPath) {
|
|
157
|
+
const dynamic = new Function(
|
|
158
|
+
`return ${fs.readFileSync(dynamicPath).toString()}`
|
|
159
|
+
)()(dotenv);
|
|
160
|
+
Object.keys(dynamic).forEach((key) => {
|
|
161
|
+
Object.assign(dotenv, {
|
|
162
|
+
[key]:
|
|
163
|
+
typeof dynamic[key] === 'function'
|
|
164
|
+
? dynamic[key](dotenv)
|
|
165
|
+
: dynamic[key],
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
}
|
|
155
169
|
|
|
156
170
|
// Log result.
|
|
157
171
|
if (log) console.log(dotenv);
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"bin": {
|
|
4
4
|
"getdotenv": "bin/getdotenv/index.js"
|
|
5
5
|
},
|
|
6
|
-
"version": "0.
|
|
6
|
+
"version": "1.0.0-0",
|
|
7
7
|
"publishConfig": {
|
|
8
8
|
"access": "public"
|
|
9
9
|
},
|
|
@@ -46,7 +46,6 @@
|
|
|
46
46
|
"@babel/preset-env": "^7.20.2",
|
|
47
47
|
"@babel/register": "^7.21.0",
|
|
48
48
|
"@types/node": "^18.15.3",
|
|
49
|
-
"babel-plugin-replace-import-extension": "^1.1.3",
|
|
50
49
|
"chai": "^4.3.7",
|
|
51
50
|
"concat-md": "^0.5.1",
|
|
52
51
|
"eslint": "^8.36.0",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
module.exports = __dirname;
|