@karmaniverous/get-dotenv 3.0.6 → 3.1.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 +100 -118
- package/bin/getdotenv/index.js +3 -3
- package/dist/default/lib/getDotenv.js +24 -18
- package/dist/default/lib/getDotenvCli.js +191 -0
- package/dist/default/lib/index.js +5 -5
- package/dist/default/lib/options.js +51 -0
- package/dist/default/lib/readDotenv.js +2 -2
- package/getdotenv.config.json +24 -0
- package/lib/getDotenv.js +28 -16
- package/lib/getDotenvCli.js +450 -0
- package/lib/index.js +1 -1
- package/lib/options.js +79 -0
- package/lib/readDotenv.js +2 -2
- package/package.json +5 -4
- package/dist/default/lib/dotenvDefaults.js +0 -20
- package/dist/default/lib/getCli.js +0 -199
- package/lib/dotenvDefaults.js +0 -13
- package/lib/getCli.js +0 -357
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getDotenvCli = void 0;
|
|
7
|
+
var _commander = require("commander");
|
|
8
|
+
var _execa = require("execa");
|
|
9
|
+
var _dotenvExpand = require("./dotenvExpand.js");
|
|
10
|
+
var _getDotenv = require("./getDotenv.js");
|
|
11
|
+
var _options = require("./options.js");
|
|
12
|
+
// npm imports
|
|
13
|
+
|
|
14
|
+
// lib imports
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* GetDotenv CLI Pre-hook Callback type. Transforms inbound options & executes side effects.
|
|
18
|
+
*
|
|
19
|
+
* @async
|
|
20
|
+
* @callback GetDotenvPreHookCallback
|
|
21
|
+
* @param {GetDotenvCliOptions} options - inbound GetDotenv CLI Options object
|
|
22
|
+
* @returns {GetDotenvCliOptions} transformed GetDotenv CLI Options object (undefined return value is ignored)
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* GetDotenv CLI Post-hook Callback type. Executes side effects within getdotenv context.
|
|
27
|
+
*
|
|
28
|
+
* @async
|
|
29
|
+
* @callback GetDotenvPostHookCallback
|
|
30
|
+
* @param {object} dotenv - dotenv object
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Generate a CLI for get-dotenv.
|
|
35
|
+
*
|
|
36
|
+
* @function getDotenvCli
|
|
37
|
+
* @param {object} [options] - options object
|
|
38
|
+
* @param {string} [options.alias] - cli alias (used for cli help)
|
|
39
|
+
* @param {string} [options.command] - {@link https://github.com/motdotla/dotenv-expand/blob/master/tests/.env dotenv-expanded} shell command string
|
|
40
|
+
* @param {bool} [options.debug] - debug mode
|
|
41
|
+
* @param {string} [options.defaultEnv] - default target environment
|
|
42
|
+
* @param {string} [options.description] - cli description (used for cli help)
|
|
43
|
+
* @param {string} [options.dotenvToken] - {@link https://github.com/motdotla/dotenv-expand/blob/master/tests/.env dotenv-expanded} token indicating a dotenv file
|
|
44
|
+
* @param {string} [options.dynamicPath] - path to file exporting an object keyed to dynamic variable functions
|
|
45
|
+
* @param {bool} [options.excludeDynamic] - exclude dynamic dotenv variables
|
|
46
|
+
* @param {bool} [options.excludeEnv] - exclude environment-specific dotenv variables
|
|
47
|
+
* @param {bool} [options.excludeGlobal] - exclude global dotenv variables
|
|
48
|
+
* @param {bool} [options.excludePrivate] - exclude private dotenv variables
|
|
49
|
+
* @param {bool} [options.excludePublic] - exclude public dotenv variables
|
|
50
|
+
* @param {bool} [options.loadProcess] - load variables to process.env
|
|
51
|
+
* @param {bool} [options.log] - log result to console
|
|
52
|
+
* @param {function} [options.logger] - logger function
|
|
53
|
+
* @param {string} [options.outputPath] - consolidated output file, {@link https://github.com/motdotla/dotenv-expand/blob/master/tests/.env dotenv-expanded} using loaded env vars
|
|
54
|
+
* @param {string} [options.paths] - {@link https://github.com/motdotla/dotenv-expand/blob/master/tests/.env dotenv-expanded} delimited list of input directory paths
|
|
55
|
+
* @param {string} [options.pathsDelimiter] - paths delimiter string
|
|
56
|
+
* @param {string} [options.pathsDelimiterPattern] - paths delimiter regex pattern
|
|
57
|
+
* @param {GetDotenvPreHookCallback} [config.preHook] - transforms cli options & executes side effects
|
|
58
|
+
* @param {string} [options.privateToken] - {@link https://github.com/motdotla/dotenv-expand/blob/master/tests/.env dotenv-expanded} token indicating private variables
|
|
59
|
+
* @param {GetDotenvPostHookCallback} [config.postHook] - executes side effects within getdotenv context
|
|
60
|
+
* @param {string} [options.vars] - {@link https://github.com/motdotla/dotenv-expand/blob/master/tests/.env dotenv-expanded} delimited list of explicit environment variable key-value pairs
|
|
61
|
+
* @param {string} [options.varsAssignor] - variable key-value assignor string
|
|
62
|
+
* @param {string} [options.varsAssignorPattern] - variable key-value assignor regex pattern
|
|
63
|
+
* @param {string} [options.varsDelimiter] - variable key-value pair delimiter string
|
|
64
|
+
* @param {string} [options.varsDelimiterPattern] - variable key-value pair delimiter regex pattern
|
|
65
|
+
* @returns {object} The CLI command.
|
|
66
|
+
*/
|
|
67
|
+
const getDotenvCli = function () {
|
|
68
|
+
let {
|
|
69
|
+
logger = console.log,
|
|
70
|
+
preHook,
|
|
71
|
+
postHook,
|
|
72
|
+
...cliOptions
|
|
73
|
+
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
74
|
+
let {
|
|
75
|
+
alias,
|
|
76
|
+
command,
|
|
77
|
+
debug,
|
|
78
|
+
defaultEnv,
|
|
79
|
+
description,
|
|
80
|
+
dotenvToken,
|
|
81
|
+
dynamicPath,
|
|
82
|
+
env,
|
|
83
|
+
excludeDynamic,
|
|
84
|
+
excludeEnv,
|
|
85
|
+
excludeGlobal,
|
|
86
|
+
excludePrivate,
|
|
87
|
+
excludePublic,
|
|
88
|
+
loadProcess,
|
|
89
|
+
log,
|
|
90
|
+
outputPath,
|
|
91
|
+
paths,
|
|
92
|
+
pathsDelimiter,
|
|
93
|
+
pathsDelimiterPattern,
|
|
94
|
+
privateToken,
|
|
95
|
+
vars,
|
|
96
|
+
varsAssignor,
|
|
97
|
+
varsAssignorPattern,
|
|
98
|
+
varsDelimiter,
|
|
99
|
+
varsDelimiterPattern
|
|
100
|
+
} = {
|
|
101
|
+
..._options.cliDefaultOptions,
|
|
102
|
+
...cliOptions
|
|
103
|
+
};
|
|
104
|
+
const excludeAll = excludeDynamic && (excludeEnv && excludeGlobal || excludePrivate && excludePublic);
|
|
105
|
+
return new _commander.Command().name(alias).description(description).enablePositionalOptions().passThroughOptions().option('-e, --env <string>', 'target environment', _dotenvExpand.dotenvExpand, env).option('-v, --vars <string>', `dotenv-expanded delimited key-value pairs: ${[['KEY1', 'VAL1'], ['KEY2', 'VAL2']].map(v => v.join(varsAssignor)).join(varsDelimiter)}`, _dotenvExpand.dotenvExpand, vars).option('-c, --command <string>', 'dotenv-expanded shell command string', _dotenvExpand.dotenvExpand, command).option('-o, --output-path <string>', 'consolidated output file, follows dotenv-expand rules using loaded env vars', _dotenvExpand.dotenvExpand, outputPath).addOption(new _commander.Option('-p, --load-process', `load variables to process.env ON${loadProcess ? ' (default)' : ''}`).conflicts('loadProcessOff')).addOption(new _commander.Option('-P, --load-process-off', `load variables to process.env OFF${!loadProcess ? ' (default)' : ''}`).conflicts('loadProcess')).addOption(new _commander.Option('-a, --exclude-all', `exclude all dotenv variables from loading ON${excludeAll ? ' (default)' : ''}`).conflicts('excludeAllOff')).addOption(new _commander.Option('-A, --exclude-all-off', `exclude all dotenv variables from loading OFF${!excludeAll ? ' (default)' : ''}`).conflicts('excludeAll')).addOption(new _commander.Option('-z, --exclude-dynamic', `exclude dynamic dotenv variables from loading ON${excludeDynamic ? ' (default)' : ''}`).conflicts('excludeDynamicOff')).addOption(new _commander.Option('-Z, --exclude-dynamic-off', `exclude dynamic dotenv variables from loading OFF${!excludeDynamic ? ' (default)' : ''}`).conflicts('excludeDynamic')).addOption(new _commander.Option('-n, --exclude-env', `exclude environment-specific dotenv variables from loading${excludeEnv ? ' (default)' : ''}`).conflicts('excludeEnvOff')).addOption(new _commander.Option('-N, --exclude-env-off', `exclude environment-specific dotenv variables from loading OFF${!excludeEnv ? ' (default)' : ''}`).conflicts('excludeEnv')).addOption(new _commander.Option('-g, --exclude-global', `exclude global dotenv variables from loading ON${excludeGlobal ? ' (default)' : ''}`).conflicts('excludeGlobalOff')).addOption(new _commander.Option('-G, --exclude-global-off', `exclude global dotenv variables from loading OFF${!excludeGlobal ? ' (default)' : ''}`).conflicts('excludeGlobal')).addOption(new _commander.Option('-r, --exclude-private', `exclude private dotenv variables from loading ON${excludePrivate ? ' (default)' : ''}`).conflicts('excludePrivateOff')).addOption(new _commander.Option('-R, --exclude-private-off', `exclude private dotenv variables from loading OFF${!excludePrivate ? ' (default)' : ''}`).conflicts('excludePrivate')).addOption(new _commander.Option('-u, --exclude-public', `exclude public dotenv variables from loading ON${excludePublic ? ' (default)' : ''}`).conflicts('excludePublicOff')).addOption(new _commander.Option('-U, --exclude-public-off', `exclude public dotenv variables from loading OFF${!excludePublic ? ' (default)' : ''}`).conflicts('excludePublic')).addOption(new _commander.Option('-l, --log', `console log loaded variables ON${log ? ' (default)' : ''}`).conflicts('logOff')).addOption(new _commander.Option('-L, --log-off', `console log loaded variables OFF${!log ? ' (default)' : ''}`).conflicts('log')).addOption(new _commander.Option('-d, --debug', `debug mode ON${debug ? ' (default)' : ''}`).conflicts('debugOff')).addOption(new _commander.Option('-D, --debug-off', `debug mode OFF${!debug ? ' (default)' : ''}`).conflicts('debug')).option('--default-env <string>', 'default target environment', _dotenvExpand.dotenvExpand, defaultEnv).option('--dotenv-token <string>', 'dotenv-expanded token indicating a dotenv file', _dotenvExpand.dotenvExpand, dotenvToken).option('--dynamic-path <string>', 'dynamic variables path', _dotenvExpand.dotenvExpand, dynamicPath).option('--paths <string>', 'dotenv-expanded delimited list of paths to dotenv directory', _dotenvExpand.dotenvExpand, paths).option('--paths-delimiter <string>', 'paths delimiter string', pathsDelimiter).option('--paths-delimiter-pattern <string>', 'paths delimiter regex pattern', pathsDelimiterPattern).option('--private-token <string>', 'dotenv-expanded token indicating private variables', _dotenvExpand.dotenvExpand, privateToken).option('--vars-delimiter <string>', 'vars delimiter string', varsDelimiter).option('--vars-delimiter-pattern <string>', 'vars delimiter regex pattern', varsDelimiterPattern).option('--vars-assignor <string>', 'vars assignment operator string', varsAssignor).option('--vars-assignor-pattern <string>', 'vars assignment operator regex pattern', varsAssignorPattern).addCommand(new _commander.Command().name('cmd').description('execute shell command string (default command)').configureHelp({
|
|
106
|
+
showGlobalOptions: true
|
|
107
|
+
}).enablePositionalOptions().passThroughOptions().action(async (options, _ref) => {
|
|
108
|
+
let {
|
|
109
|
+
args
|
|
110
|
+
} = _ref;
|
|
111
|
+
if (args.length) await (0, _execa.execaCommand)(args.join('\\ '), {
|
|
112
|
+
stdio: 'inherit',
|
|
113
|
+
shell: true
|
|
114
|
+
});
|
|
115
|
+
}), {
|
|
116
|
+
isDefault: true
|
|
117
|
+
}).hook('preSubcommand', async thisCommand => {
|
|
118
|
+
const rawOptions = thisCommand.opts();
|
|
119
|
+
if (rawOptions.debug) logger('\n*** raw options ***\n', {
|
|
120
|
+
rawOptions
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
// Load options.
|
|
124
|
+
let {
|
|
125
|
+
command,
|
|
126
|
+
debugOff,
|
|
127
|
+
excludeAllOff,
|
|
128
|
+
excludeDynamicOff,
|
|
129
|
+
excludeEnvOff,
|
|
130
|
+
excludeGlobalOff,
|
|
131
|
+
excludePrivateOff,
|
|
132
|
+
excludePublicOff,
|
|
133
|
+
loadProcessOff,
|
|
134
|
+
logOff,
|
|
135
|
+
...cliOptions
|
|
136
|
+
} = rawOptions;
|
|
137
|
+
|
|
138
|
+
// Resolve flags.
|
|
139
|
+
const resolveExclusion = (exclude, excludeOff, defaultValue) => exclude ? true : excludeOff ? false : defaultValue;
|
|
140
|
+
const resolveExclusionAll = (exclude, excludeOff, defaultValue) => excludeAll && !excludeOff ? true : excludeAllOff && !exclude ? false : defaultValue;
|
|
141
|
+
cliOptions.debug = resolveExclusion(cliOptions.debug, debugOff, debug);
|
|
142
|
+
cliOptions.excludeDynamic = resolveExclusionAll(cliOptions.excludeDynamic, excludeDynamicOff, excludeDynamic);
|
|
143
|
+
cliOptions.excludeEnv = resolveExclusionAll(cliOptions.excludeEnv, excludeEnvOff, excludeEnv);
|
|
144
|
+
cliOptions.excludeGlobal = resolveExclusionAll(cliOptions.excludeGlobal, excludeGlobalOff, excludeGlobal);
|
|
145
|
+
cliOptions.excludePrivate = resolveExclusionAll(cliOptions.excludePrivate, excludePrivateOff, excludePrivate);
|
|
146
|
+
cliOptions.excludePublic = resolveExclusionAll(cliOptions.excludePublic, excludePublicOff, excludePublic);
|
|
147
|
+
cliOptions.log = resolveExclusion(cliOptions.log, logOff, log);
|
|
148
|
+
cliOptions.loadProcess = resolveExclusion(cliOptions.loadProcess, loadProcessOff, loadProcess);
|
|
149
|
+
if (cliOptions.debug) logger('\n*** after default resolution ***\n', {
|
|
150
|
+
cliOptions
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
// Execute pre-hook.
|
|
154
|
+
if (preHook) {
|
|
155
|
+
cliOptions = (await preHook(cliOptions)) ?? cliOptions;
|
|
156
|
+
if (cliOptions.debug) logger('\n*** after pre-hook ***\n', cliOptions);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Get getdotenv options from parent command.
|
|
160
|
+
const parentGetdotenvOptions = process.env['getdotenvOptions'] ? JSON.parse(process.env['getdotenvOptions']) : {};
|
|
161
|
+
const cliGetdotenvOptions = (0, _options.cli2getdotenvOptions)(cliOptions);
|
|
162
|
+
const getdotenvOptions = {
|
|
163
|
+
...parentGetdotenvOptions,
|
|
164
|
+
...cliGetdotenvOptions
|
|
165
|
+
};
|
|
166
|
+
if (cliOptions.debug) logger('\n*** after getdotenv option resolution ***\n', {
|
|
167
|
+
parentGetdotenvOptions,
|
|
168
|
+
cliGetdotenvOptions,
|
|
169
|
+
getdotenvOptions
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
// Execute getdotenv.
|
|
173
|
+
const dotenv = await (0, _getDotenv.getDotenv)({
|
|
174
|
+
...getdotenvOptions,
|
|
175
|
+
logger
|
|
176
|
+
});
|
|
177
|
+
if (cliOptions.debug) console.log('\n*** after getdotenv execution ***\n', {
|
|
178
|
+
dotenv
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
// Execute post-hook.
|
|
182
|
+
if (postHook) await postHook(dotenv);
|
|
183
|
+
|
|
184
|
+
// Execute shell command.
|
|
185
|
+
if (command) await (0, _execa.execaCommand)(command.replace(/ /g, '\\ '), {
|
|
186
|
+
stdio: 'inherit',
|
|
187
|
+
shell: true
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
};
|
|
191
|
+
exports.getDotenvCli = getDotenvCli;
|
|
@@ -9,16 +9,16 @@ Object.defineProperty(exports, "dotenvExpand", {
|
|
|
9
9
|
return _dotenvExpand.dotenvExpand;
|
|
10
10
|
}
|
|
11
11
|
});
|
|
12
|
-
Object.defineProperty(exports, "
|
|
12
|
+
Object.defineProperty(exports, "getDotenv", {
|
|
13
13
|
enumerable: true,
|
|
14
14
|
get: function () {
|
|
15
|
-
return
|
|
15
|
+
return _getDotenv.getDotenv;
|
|
16
16
|
}
|
|
17
17
|
});
|
|
18
|
-
Object.defineProperty(exports, "
|
|
18
|
+
Object.defineProperty(exports, "getDotenvCli", {
|
|
19
19
|
enumerable: true,
|
|
20
20
|
get: function () {
|
|
21
|
-
return
|
|
21
|
+
return _getDotenvCli.getDotenvCli;
|
|
22
22
|
}
|
|
23
23
|
});
|
|
24
24
|
Object.defineProperty(exports, "getDotenvSync", {
|
|
@@ -29,4 +29,4 @@ Object.defineProperty(exports, "getDotenvSync", {
|
|
|
29
29
|
});
|
|
30
30
|
var _dotenvExpand = require("./dotenvExpand.js");
|
|
31
31
|
var _getDotenv = require("./getDotenv.js");
|
|
32
|
-
var
|
|
32
|
+
var _getDotenvCli = require("./getDotenvCli.js");
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getdotenvDefaultOptions = exports.cliDefaultOptions = exports.cli2getdotenvOptions = void 0;
|
|
7
|
+
var _fsExtra = _interopRequireDefault(require("fs-extra"));
|
|
8
|
+
var _lodash = _interopRequireDefault(require("lodash.frompairs"));
|
|
9
|
+
var _lodash2 = _interopRequireDefault(require("lodash.pick"));
|
|
10
|
+
var _pkgDir = require("pkg-dir");
|
|
11
|
+
var _path = require("path");
|
|
12
|
+
var _url = require("url");
|
|
13
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
14
|
+
// npm imports
|
|
15
|
+
|
|
16
|
+
const _dirname = (0, _path.dirname)((0, _url.fileURLToPath)(import.meta.url));
|
|
17
|
+
|
|
18
|
+
// Load default options.
|
|
19
|
+
const cliDefaultOptionsGlobalPath = (0, _path.resolve)(_dirname, '../getdotenv.config.json');
|
|
20
|
+
const cliDefaultOptionsGlobal = (await _fsExtra.default.exists(cliDefaultOptionsGlobalPath)) ? JSON.parse(await _fsExtra.default.readFile(cliDefaultOptionsGlobalPath)) : {};
|
|
21
|
+
const cliDefaultOptionsLocalPath = (0, _path.resolve)(await (0, _pkgDir.packageDirectory)(), 'getdotenv.config.json');
|
|
22
|
+
const cliDefaultOptionsLocal = (await _fsExtra.default.exists(cliDefaultOptionsLocalPath)) ? JSON.parse(await _fsExtra.default.readFile(cliDefaultOptionsLocalPath)) : {};
|
|
23
|
+
const cliDefaultOptions = {
|
|
24
|
+
...cliDefaultOptionsGlobal,
|
|
25
|
+
...cliDefaultOptionsLocal
|
|
26
|
+
};
|
|
27
|
+
exports.cliDefaultOptions = cliDefaultOptions;
|
|
28
|
+
const cli2getdotenvOptions = function () {
|
|
29
|
+
let {
|
|
30
|
+
env,
|
|
31
|
+
defaultEnv,
|
|
32
|
+
paths,
|
|
33
|
+
pathsDelimiter,
|
|
34
|
+
pathsDelimiterPattern,
|
|
35
|
+
vars,
|
|
36
|
+
varsAssignor,
|
|
37
|
+
varsAssignorPattern,
|
|
38
|
+
varsDelimiter,
|
|
39
|
+
varsDelimiterPattern,
|
|
40
|
+
...rest
|
|
41
|
+
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
42
|
+
return {
|
|
43
|
+
...(0, _lodash2.default)(rest, ['dotenvToken', 'dynamicPath', 'excludeDynamic', 'excludeEnv', 'excludeGlobal', 'excludePrivate', 'excludePublic', 'loadProcess', 'log', 'outputPath', 'privateToken']),
|
|
44
|
+
env: env ?? defaultEnv,
|
|
45
|
+
paths: (paths === null || paths === void 0 ? void 0 : paths.split(pathsDelimiterPattern ? RegExp(pathsDelimiterPattern) : pathsDelimiter)) ?? [],
|
|
46
|
+
vars: (0, _lodash.default)(vars === null || vars === void 0 ? void 0 : vars.split(varsDelimiterPattern ? RegExp(varsDelimiterPattern) : varsDelimiter).map(v => v.split(varsAssignorPattern ? RegExp(varsAssignorPattern) : varsAssignor)))
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
exports.cli2getdotenvOptions = cli2getdotenvOptions;
|
|
50
|
+
const getdotenvDefaultOptions = cli2getdotenvOptions(cliDefaultOptions);
|
|
51
|
+
exports.getdotenvDefaultOptions = getdotenvDefaultOptions;
|
|
@@ -20,7 +20,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
20
20
|
*/
|
|
21
21
|
const readDotenv = async path => {
|
|
22
22
|
try {
|
|
23
|
-
return (0, _dotenv.parse)(await _fsExtra.default.readFile(path));
|
|
23
|
+
return (await _fsExtra.default.exists(path)) ? (0, _dotenv.parse)(await _fsExtra.default.readFile(path)) : {};
|
|
24
24
|
} catch {
|
|
25
25
|
return {};
|
|
26
26
|
}
|
|
@@ -39,7 +39,7 @@ const readDotenv = async path => {
|
|
|
39
39
|
exports.readDotenv = readDotenv;
|
|
40
40
|
const readDotenvSync = path => {
|
|
41
41
|
try {
|
|
42
|
-
return (0, _dotenv.parse)(_fsExtra.default.readFileSync(path));
|
|
42
|
+
return _fsExtra.default.existsSync(path) ? (0, _dotenv.parse)(_fsExtra.default.readFileSync(path)) : {};
|
|
43
43
|
} catch {
|
|
44
44
|
return {};
|
|
45
45
|
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"alias": "getdotenv",
|
|
3
|
+
"debug": false,
|
|
4
|
+
"defaultEnv": "dev",
|
|
5
|
+
"description": "Base CLI.",
|
|
6
|
+
"dotenvToken": ".env",
|
|
7
|
+
"dynamicPath": "./env/dynamic.js",
|
|
8
|
+
"excludeDynamic": false,
|
|
9
|
+
"excludeEnv": false,
|
|
10
|
+
"excludeGlobal": false,
|
|
11
|
+
"excludePrivate": false,
|
|
12
|
+
"excludePublic": false,
|
|
13
|
+
"loadProcess": true,
|
|
14
|
+
"log": false,
|
|
15
|
+
"paths": "./ ./env",
|
|
16
|
+
"pathsDelimiter": " ",
|
|
17
|
+
"pathsDelimiterPattern": "\\s+",
|
|
18
|
+
"privateToken": "local",
|
|
19
|
+
"vars": "",
|
|
20
|
+
"varsAssignor": "=",
|
|
21
|
+
"varsAssignorPattern": "=",
|
|
22
|
+
"varsDelimiter": " ",
|
|
23
|
+
"varsDelimiterPattern": "\\s+"
|
|
24
|
+
}
|
package/lib/getDotenv.js
CHANGED
|
@@ -1,18 +1,27 @@
|
|
|
1
1
|
// npm imports
|
|
2
2
|
import { expand } from 'dotenv-expand';
|
|
3
3
|
import fs from 'fs-extra';
|
|
4
|
+
import pickBy from 'lodash.pickby';
|
|
4
5
|
import path from 'path';
|
|
5
6
|
import { nanoid } from 'nanoid';
|
|
6
7
|
|
|
7
8
|
// lib imports
|
|
8
|
-
import {
|
|
9
|
+
import { getdotenvDefaultOptions } from './options.js';
|
|
9
10
|
import { readDotenv, readDotenvSync } from './readDotenv.js';
|
|
10
11
|
|
|
12
|
+
const pruneVars = (getdotenvDefaultOptions, options) =>
|
|
13
|
+
pickBy(
|
|
14
|
+
{
|
|
15
|
+
...(getdotenvDefaultOptions.vars ?? {}),
|
|
16
|
+
...(options.vars ?? {}),
|
|
17
|
+
},
|
|
18
|
+
(v) => v?.length
|
|
19
|
+
);
|
|
20
|
+
|
|
11
21
|
/**
|
|
12
22
|
* get-dotenv options type
|
|
13
23
|
*
|
|
14
|
-
* @typedef {Object}
|
|
15
|
-
*
|
|
24
|
+
* @typedef {Object} GetDotenvOptions
|
|
16
25
|
* @property {string} [dotenvToken] - token indicating a dotenv file
|
|
17
26
|
* @property {string} [dynamicPath] - path to file exporting an object keyed to dynamic variable functions
|
|
18
27
|
* @property {string} [env] - target environment
|
|
@@ -36,7 +45,7 @@ import { readDotenv, readDotenvSync } from './readDotenv.js';
|
|
|
36
45
|
* @async
|
|
37
46
|
* @function getDotenv
|
|
38
47
|
*
|
|
39
|
-
* @param {
|
|
48
|
+
* @param {GetDotenvOptions} [options] - options object
|
|
40
49
|
*
|
|
41
50
|
* @returns {Promise<object>} The combined parsed dotenv object.
|
|
42
51
|
*/
|
|
@@ -44,8 +53,8 @@ export const getDotenv = async (options = {}) => {
|
|
|
44
53
|
// Apply defaults.
|
|
45
54
|
let {
|
|
46
55
|
dotenvToken,
|
|
47
|
-
env,
|
|
48
56
|
dynamicPath,
|
|
57
|
+
env,
|
|
49
58
|
excludeDynamic,
|
|
50
59
|
excludeEnv,
|
|
51
60
|
excludeGlobal,
|
|
@@ -53,12 +62,13 @@ export const getDotenv = async (options = {}) => {
|
|
|
53
62
|
excludePublic,
|
|
54
63
|
loadProcess,
|
|
55
64
|
log,
|
|
56
|
-
logger,
|
|
65
|
+
logger = console.log,
|
|
57
66
|
outputPath,
|
|
58
67
|
paths,
|
|
59
68
|
privateToken,
|
|
60
|
-
|
|
61
|
-
|
|
69
|
+
} = { ...getdotenvDefaultOptions, ...options };
|
|
70
|
+
|
|
71
|
+
const vars = pruneVars(getdotenvDefaultOptions, options);
|
|
62
72
|
|
|
63
73
|
// Read .env files.
|
|
64
74
|
const loaded = await paths.reduce(async (e, p) => {
|
|
@@ -103,7 +113,7 @@ export const getDotenv = async (options = {}) => {
|
|
|
103
113
|
});
|
|
104
114
|
|
|
105
115
|
// Process dynamic variables.
|
|
106
|
-
if (dynamicPath && !excludeDynamic) {
|
|
116
|
+
if (dynamicPath && !excludeDynamic && (await fs.exists(dynamicPath))) {
|
|
107
117
|
const dynamic = new Function(
|
|
108
118
|
`return ${(await fs.readFile(dynamicPath)).toString()}`
|
|
109
119
|
)()(dotenv);
|
|
@@ -142,8 +152,8 @@ export const getDotenv = async (options = {}) => {
|
|
|
142
152
|
Object.assign(process.env, dotenv, {
|
|
143
153
|
getdotenvOptions: JSON.stringify({
|
|
144
154
|
dotenvToken,
|
|
145
|
-
env,
|
|
146
155
|
dynamicPath,
|
|
156
|
+
env,
|
|
147
157
|
excludeDynamic,
|
|
148
158
|
excludeEnv,
|
|
149
159
|
excludeGlobal,
|
|
@@ -165,7 +175,7 @@ export const getDotenv = async (options = {}) => {
|
|
|
165
175
|
*
|
|
166
176
|
* @function getDotenvSync
|
|
167
177
|
*
|
|
168
|
-
* @param {
|
|
178
|
+
* @param {GetDotenvOptions} [options] - options object
|
|
169
179
|
*
|
|
170
180
|
* @returns {Object} The combined parsed dotenv object.
|
|
171
181
|
*/
|
|
@@ -173,8 +183,8 @@ export const getDotenvSync = (options = {}) => {
|
|
|
173
183
|
// Apply defaults.
|
|
174
184
|
let {
|
|
175
185
|
dotenvToken,
|
|
176
|
-
env,
|
|
177
186
|
dynamicPath,
|
|
187
|
+
env,
|
|
178
188
|
excludeDynamic,
|
|
179
189
|
excludeEnv,
|
|
180
190
|
excludeGlobal,
|
|
@@ -182,11 +192,13 @@ export const getDotenvSync = (options = {}) => {
|
|
|
182
192
|
excludePublic,
|
|
183
193
|
loadProcess,
|
|
184
194
|
log,
|
|
195
|
+
logger = console.log,
|
|
185
196
|
outputPath,
|
|
186
197
|
paths,
|
|
187
198
|
privateToken,
|
|
188
|
-
|
|
189
|
-
|
|
199
|
+
} = { ...getdotenvDefaultOptions, ...options };
|
|
200
|
+
|
|
201
|
+
const vars = pruneVars(getdotenvDefaultOptions, options);
|
|
190
202
|
|
|
191
203
|
// Read .env files.
|
|
192
204
|
const loaded = paths.reduce((e, p) => {
|
|
@@ -229,7 +241,7 @@ export const getDotenvSync = (options = {}) => {
|
|
|
229
241
|
});
|
|
230
242
|
|
|
231
243
|
// Process dynamic variables.
|
|
232
|
-
if (dynamicPath && !excludeDynamic) {
|
|
244
|
+
if (dynamicPath && !excludeDynamic && fs.existsSync(dynamicPath)) {
|
|
233
245
|
const dynamic = new Function(
|
|
234
246
|
`return ${fs.readFileSync(dynamicPath).toString()}`
|
|
235
247
|
)()(dotenv);
|
|
@@ -261,7 +273,7 @@ export const getDotenvSync = (options = {}) => {
|
|
|
261
273
|
}
|
|
262
274
|
|
|
263
275
|
// Log result.
|
|
264
|
-
if (log)
|
|
276
|
+
if (log) logger(dotenv);
|
|
265
277
|
|
|
266
278
|
// Load process.env.
|
|
267
279
|
if (loadProcess)
|