@depup/env-cmd 11.0.0-depup.25
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/.editorconfig +14 -0
- package/.husky/commit-msg +1 -0
- package/.mocharc.json +8 -0
- package/CHANGELOG.md +162 -0
- package/LICENSE +21 -0
- package/README.md +32 -0
- package/bin/env-cmd.js +3 -0
- package/changes.json +14 -0
- package/dist/cli.d.ts +8 -0
- package/dist/cli.js +21 -0
- package/dist/env-cmd.d.ts +10 -0
- package/dist/env-cmd.js +58 -0
- package/dist/expand-envs.d.ts +6 -0
- package/dist/expand-envs.js +10 -0
- package/dist/get-env-vars.d.ts +12 -0
- package/dist/get-env-vars.js +122 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +6 -0
- package/dist/loaders/typescript.d.ts +1 -0
- package/dist/loaders/typescript.js +8 -0
- package/dist/parse-args.d.ts +6 -0
- package/dist/parse-args.js +105 -0
- package/dist/parse-env-file.d.ts +30 -0
- package/dist/parse-env-file.js +119 -0
- package/dist/parse-rc-file.d.ts +8 -0
- package/dist/parse-rc-file.js +78 -0
- package/dist/signal-termination.d.ts +27 -0
- package/dist/signal-termination.js +116 -0
- package/dist/types.d.ts +39 -0
- package/dist/types.js +1 -0
- package/dist/utils.d.ts +15 -0
- package/dist/utils.js +46 -0
- package/eslint.config.js +38 -0
- package/package.json +118 -0
package/.editorconfig
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# EditorConfig is awesome: https://EditorConfig.org
|
|
2
|
+
|
|
3
|
+
# top-most EditorConfig file
|
|
4
|
+
root = true
|
|
5
|
+
|
|
6
|
+
# Unix-style newlines with a newline ending every file
|
|
7
|
+
[*]
|
|
8
|
+
end_of_line = lf
|
|
9
|
+
insert_final_newline = true
|
|
10
|
+
charset = utf-8
|
|
11
|
+
|
|
12
|
+
[*.{js*,ts*}]
|
|
13
|
+
indent_style = space
|
|
14
|
+
indent_size = 2
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
npx commitlint --edit
|
package/.mocharc.json
ADDED
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 11.0.0
|
|
4
|
+
|
|
5
|
+
- **BREAKING**: Drop support for nodejs `v8` to `v20.9`. The minimum supported nodejs version is now `v20.10`.
|
|
6
|
+
- **BREAKING**: Removed `-r` flag and use only `-f` flag.
|
|
7
|
+
- **BREAKING**: Support inline comments in `.env` files. A `#` character now signifies the start of an inline comment, unless the value is surrounded by quotation marks (`"`).
|
|
8
|
+
- **BREAKING**: Migrated the repository to ESM modules instead of CommonJS.
|
|
9
|
+
- **BREAKING**: Support variable expansion using curly-brace syntax (`${MY_VAR}`), when the `-x` option is enabled.
|
|
10
|
+
- **Feature**: Support loading env variables from `.cjs` and `.mjs` files.
|
|
11
|
+
- **Feature**: Support loading env variables from `.ts`, `.cts`, and `.mts` files.
|
|
12
|
+
- **Feature**: When loading an invalid JSON file, show the original parse error.
|
|
13
|
+
- **Feature**: Add a more helpful error message when trying to invoke env-cmd as a standalone command.
|
|
14
|
+
- **Feature**: Added support for nested env variables within env files with the `--recursive` flag
|
|
15
|
+
- **Docs**: clarify how variable expansion works.
|
|
16
|
+
- **Internal**: Replaced Travis CI with GitHub Actions, run unit tests on windows.
|
|
17
|
+
- **Internal**: Configure automatic releases to npm from GitHub Actions
|
|
18
|
+
- **Internal**: Refactor the loader logic, to make it easier to add other loaders.
|
|
19
|
+
- **Upgrade**: Update all dependencies.
|
|
20
|
+
- **Upgrade**: Upgraded all devDependencies
|
|
21
|
+
|
|
22
|
+
## 10.1.0
|
|
23
|
+
|
|
24
|
+
- **Feature**: Added support for expanding vars using the `-x` flag.
|
|
25
|
+
Note: only supports `$var` syntax
|
|
26
|
+
- **Feature**: Added support for `--silent` flag that ignores env-cmd errors and missing files and
|
|
27
|
+
only terminates on caught signals
|
|
28
|
+
- **Feature**: Added a new `--verbose` flag that prints additional debugging info to `console.info`
|
|
29
|
+
- **Upgrade**: Upgraded dependency `commander` to `4.x`
|
|
30
|
+
- **Upgrade**: Upgraded devDependencies `sinon`, `nyc`, and `ts-standard`
|
|
31
|
+
- **Fix**: Handle case where the termination signal is the termination code
|
|
32
|
+
|
|
33
|
+
## 10.0.1
|
|
34
|
+
|
|
35
|
+
- **Fix**: Fixed bug introduced by strict equal checking for `undefined` when the value was `null`. This
|
|
36
|
+
bug caused most executions of `env-cmd` to fail with an error, when in fact no error had occurred.
|
|
37
|
+
|
|
38
|
+
## 10.0.0
|
|
39
|
+
|
|
40
|
+
- **BREAKING**: Typescript now targets ES2017
|
|
41
|
+
- **Fix**: Default RC files will now properly be searched
|
|
42
|
+
- **Change**: Fixed some documentation issues
|
|
43
|
+
- **Change**: Use `ts-standard` instead of `eslint` for linting
|
|
44
|
+
|
|
45
|
+
## 9.0.3
|
|
46
|
+
|
|
47
|
+
- **Fix**: Use global flag on regex when parsing and preserving newline characters in env values (thanks to MaximTovstashev)
|
|
48
|
+
|
|
49
|
+
## 9.0.2
|
|
50
|
+
|
|
51
|
+
- **Fix**: CLI will now exit with non-zero error code when an error is encountered (thanks to blagh)
|
|
52
|
+
|
|
53
|
+
## 9.0.1
|
|
54
|
+
|
|
55
|
+
- **BREAKING**: Fixed major bug that required passing `--` in order to pass flags to the command.
|
|
56
|
+
Normally I release major breaking changes as major versions, but this was a bug and no documentation
|
|
57
|
+
anywhere states using `--` as intended or official behavior.
|
|
58
|
+
- **Change**: Fixed some documentation issues
|
|
59
|
+
- **Change**: `npm run lint` command now includes calling `tsc` to check for typescript errors
|
|
60
|
+
|
|
61
|
+
## 9.0.0
|
|
62
|
+
|
|
63
|
+
- ***BREAKING***: Converted project to Typescript
|
|
64
|
+
- ***BREAKING***: Changes to all option flags, see docs for new options
|
|
65
|
+
- ***BREAKING***: Dropping support for node v4 and v6
|
|
66
|
+
- **Change**: Updated all dependencies
|
|
67
|
+
- **Change**: Update package-lock.json file
|
|
68
|
+
- **Feature**: Added support for asynchronous .env and .rc files
|
|
69
|
+
- **Feature**: Added support for a programmatic API
|
|
70
|
+
- **Feature**: Added --use-shell option (thanks to nidkil)
|
|
71
|
+
- **Fix**: Keep newline (`\n`) characters intact when parsing env files
|
|
72
|
+
- **Change**: Added node v10 and v12 to build automation
|
|
73
|
+
- **Change**: Updated Readme file to reflect new options and CLI changes
|
|
74
|
+
|
|
75
|
+
## 8.0.2
|
|
76
|
+
|
|
77
|
+
- **Change**: Updated dependencies and packages.json to fix `npm audit` concerns.
|
|
78
|
+
|
|
79
|
+
## 8.0.1
|
|
80
|
+
|
|
81
|
+
- **Bug**: Properly propagate child process exit signals and codes to parent process
|
|
82
|
+
|
|
83
|
+
## 8.0.0
|
|
84
|
+
|
|
85
|
+
- ***BREAKING***: Stripe out spaces around the `key` and `value` in an env file. In order to include a beginning/ending space in an env var value, you need to surround the value in double or single quotes. `ENV = " Value"`
|
|
86
|
+
- **Bug**: Fixed some bugs around how the parent process and spawn processes are killed
|
|
87
|
+
- **Change**: Updated a number of core libraries: `cross-spawn`, `coveralls`, `istanbul -> nyc`, `mocha`, `proxyquire`, `sinon`, `standard`
|
|
88
|
+
|
|
89
|
+
## 7.0.0
|
|
90
|
+
|
|
91
|
+
- ***BREAKING***: The `.env` file path resolving has been changed to allow for absolute pathing, relative pathing, and `~` home directory pathing. Please
|
|
92
|
+
see Readme.md for more info about how the new pathing conventions work.
|
|
93
|
+
|
|
94
|
+
## 6.0.0
|
|
95
|
+
|
|
96
|
+
- ***BREAKING***: Fallback to default `.env` file behavior is no longer the default behavior. You must specify `--fallback` option for that behavior now.
|
|
97
|
+
- ***BREAKING***: A specific node version has been set in package.json. Current minimum version is `>=4.0.0`. *Note: the implied minimum version
|
|
98
|
+
before this release was always `4.0.0`, but now it is explicitly set and could produce warnings by npm if included in projects that utilizes a
|
|
99
|
+
node version that is less than `4.0.0`.*
|
|
100
|
+
- **Feature**: Added `--fallback` option to allow for falling back to the default `.env` file if the provided `.env` file is not found.
|
|
101
|
+
- **Feature**: Added ability to select multiple environments from the `.env-cmdrc` file. The environments override each other like this:
|
|
102
|
+
`development,production` where `production` vars override `development` vars if they share the same vars.
|
|
103
|
+
- **Bug**: `env-cmd` no longer crashes when it cannot find the provided `.env` file. Instead, it will execute as normal, but without included any custom env vars. *Note: it will still include system and shell vars.*
|
|
104
|
+
|
|
105
|
+
## 5.1.0
|
|
106
|
+
|
|
107
|
+
- **Feature**: Added new option `--no-override` that when passed will make it so that the env file
|
|
108
|
+
vars will not overwrite already defined env vars on `process.env` or in the shell
|
|
109
|
+
- **Updated Dev-Dependencies**: `standard >= 10.0.0`, `sinon >= 2.0.0`
|
|
110
|
+
|
|
111
|
+
## 5.0.0
|
|
112
|
+
|
|
113
|
+
- ***BREAKING***: Inline comments are no longer allowed in `.env` files (full line comments are still allowed)
|
|
114
|
+
- ***BREAKING***: `.env` file no longer supports the `env var` format (only `env=var` is allowed now)
|
|
115
|
+
- ***BREAKING***: Double Quotes are no longer needed when using special symbols (such as `#`) in the value portion of an env var
|
|
116
|
+
- **Feature**: if the given env file cannot be found, it will auto default to searching
|
|
117
|
+
the execution directory for a file called `.env` and use that as a fallback. See README for why this is
|
|
118
|
+
helpful. (special thanks to Alexander Praetorius)
|
|
119
|
+
|
|
120
|
+
## 4.0.0
|
|
121
|
+
|
|
122
|
+
- ***BREAKING***: In order to use double quotes as part of the env value, you must now surround those double quotes with an additional set of quotes: So `ENV1="value"` -> `ENV1=""value""` (this only applies to double quotes, single quotes continue to work as normal)
|
|
123
|
+
- **Bug**: Fixed bug in the comment stripper function that would remove env values that included a `#`. Now, in order to use a `#` in a env value, you have to surround that env value in double quotes: `ENV="Some#Value"`.
|
|
124
|
+
- **Bug**: Fixed a major bug with the `.env-cmdrc` file that would not add system env vars back in after reading the `.env-cmdrc` file. This meant that system vars like `PATH` would not exist when running the command.
|
|
125
|
+
|
|
126
|
+
## 3.0.0
|
|
127
|
+
|
|
128
|
+
- **Feature**: Added ability to use an `.env-cmdrc` file to hold multiple configs
|
|
129
|
+
- **Feature**: Added ability to pass in a regular `.js` file exporting an object for your env file (special thanks to Jon Scheiding!)
|
|
130
|
+
- **Change**: Updated core `cross-spawn` lib to 5.0.1
|
|
131
|
+
|
|
132
|
+
## 2.2.0
|
|
133
|
+
|
|
134
|
+
- **Feature**: Added support for .json env files (special thanks to Eric Lanehart!)
|
|
135
|
+
|
|
136
|
+
## 2.1.0
|
|
137
|
+
|
|
138
|
+
- **Feature**: Added support for `key value` mapping in env vars file
|
|
139
|
+
- **Feature**: Added support for inline comments `ENV=VALUE # inline comment`
|
|
140
|
+
- **Change**: Will now ignore invalid lines in env vars file instead of throwing an error
|
|
141
|
+
- **Change**: Migrated all the parsing over to regex since the file format is simple enough right
|
|
142
|
+
now to support that
|
|
143
|
+
- **Bug**: Removed old test cases for the `-e/--env` flags that were not needed anymore
|
|
144
|
+
|
|
145
|
+
## 2.0.0
|
|
146
|
+
|
|
147
|
+
- ***BREAKING***: Removed the `-e` and `--env` flags. Now it just expects the first arg to `env-cmd` to be the relative path to the env file: `env-cmd env_file command carg1 carg2`
|
|
148
|
+
- **Change:** `ParseEnvFile` is now more properly named `ParseEnvString`
|
|
149
|
+
- **Feature:** `ParseEnvString` will ignore comment lines (lines starting with `#`)
|
|
150
|
+
- **Feature:** `ParseEnvString` will ignore empty lines in env file
|
|
151
|
+
- **Bug:** `ParseEnvString` will extract the last line even if no newline (`\n`) exists on it
|
|
152
|
+
|
|
153
|
+
## 1.0.1
|
|
154
|
+
|
|
155
|
+
- Fixed badges
|
|
156
|
+
- Added .npmignore
|
|
157
|
+
- Added help text to be printed out on certain errors
|
|
158
|
+
- Handled uncaught errors nicely
|
|
159
|
+
|
|
160
|
+
## 1.0.0
|
|
161
|
+
|
|
162
|
+
- Initial release
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Todd Bluhm
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# @depup/env-cmd
|
|
2
|
+
|
|
3
|
+
> Dependency-bumped version of [env-cmd](https://www.npmjs.com/package/env-cmd)
|
|
4
|
+
|
|
5
|
+
Generated by [DepUp](https://github.com/depup/npm) -- all production
|
|
6
|
+
dependencies bumped to latest versions.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @depup/env-cmd
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
| Field | Value |
|
|
15
|
+
|-------|-------|
|
|
16
|
+
| Original | [env-cmd](https://www.npmjs.com/package/env-cmd) @ 11.0.0 |
|
|
17
|
+
| Processed | 2026-07-21 |
|
|
18
|
+
| Smoke test | passed |
|
|
19
|
+
| Deps updated | 2 |
|
|
20
|
+
|
|
21
|
+
## Dependency Changes
|
|
22
|
+
|
|
23
|
+
| Dependency | From | To |
|
|
24
|
+
|------------|------|-----|
|
|
25
|
+
| @commander-js/extra-typings | ^13.1.0 | ^15.0.0 |
|
|
26
|
+
| commander | ^13.1.0 | ^15.0.0 |
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
Source: https://github.com/depup/npm | Original: https://www.npmjs.com/package/env-cmd
|
|
31
|
+
|
|
32
|
+
License inherited from the original package.
|
package/bin/env-cmd.js
ADDED
package/changes.json
ADDED
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Environment } from './types.ts';
|
|
2
|
+
/**
|
|
3
|
+
* Executes env - cmd using command line arguments
|
|
4
|
+
* @export
|
|
5
|
+
* @param {string[]} args Command line argument to pass in ['-f', './.env']
|
|
6
|
+
* @returns {Promise<Environment>}
|
|
7
|
+
*/
|
|
8
|
+
export declare function CLI(args: string[]): Promise<Environment>;
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import * as processLib from 'node:process';
|
|
2
|
+
import { EnvCmd } from './env-cmd.js';
|
|
3
|
+
import { parseArgs } from './parse-args.js';
|
|
4
|
+
/**
|
|
5
|
+
* Executes env - cmd using command line arguments
|
|
6
|
+
* @export
|
|
7
|
+
* @param {string[]} args Command line argument to pass in ['-f', './.env']
|
|
8
|
+
* @returns {Promise<Environment>}
|
|
9
|
+
*/
|
|
10
|
+
export async function CLI(args) {
|
|
11
|
+
// Parse the args from the command line
|
|
12
|
+
const parsedArgs = parseArgs(args);
|
|
13
|
+
// Run EnvCmd
|
|
14
|
+
try {
|
|
15
|
+
return await EnvCmd(parsedArgs);
|
|
16
|
+
}
|
|
17
|
+
catch (e) {
|
|
18
|
+
console.error(e);
|
|
19
|
+
return processLib.exit(1);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { EnvCmdOptions, Environment } from './types.ts';
|
|
2
|
+
/**
|
|
3
|
+
* The main env-cmd program. This will spawn a new process and run the given command using
|
|
4
|
+
* various environment file solutions.
|
|
5
|
+
*
|
|
6
|
+
* @export
|
|
7
|
+
* @param {EnvCmdOptions} { command, commandArgs, envFile, rc, options }
|
|
8
|
+
* @returns {Promise<Environment>} Returns an object containing [environment variable name]: value
|
|
9
|
+
*/
|
|
10
|
+
export declare function EnvCmd({ command, commandArgs, envFile, rc, options, }: EnvCmdOptions): Promise<Environment>;
|
package/dist/env-cmd.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { default as spawn } from 'cross-spawn';
|
|
2
|
+
import { TermSignals } from './signal-termination.js';
|
|
3
|
+
import { getEnvVars } from './get-env-vars.js';
|
|
4
|
+
import { expandEnvs } from './expand-envs.js';
|
|
5
|
+
import * as processLib from 'node:process';
|
|
6
|
+
/**
|
|
7
|
+
* The main env-cmd program. This will spawn a new process and run the given command using
|
|
8
|
+
* various environment file solutions.
|
|
9
|
+
*
|
|
10
|
+
* @export
|
|
11
|
+
* @param {EnvCmdOptions} { command, commandArgs, envFile, rc, options }
|
|
12
|
+
* @returns {Promise<Environment>} Returns an object containing [environment variable name]: value
|
|
13
|
+
*/
|
|
14
|
+
export async function EnvCmd({ command, commandArgs, envFile, rc, options = {}, }) {
|
|
15
|
+
let env = {};
|
|
16
|
+
try {
|
|
17
|
+
env = await getEnvVars({ envFile, rc, verbose: options.verbose });
|
|
18
|
+
}
|
|
19
|
+
catch (e) {
|
|
20
|
+
if (!(options.silent ?? false)) {
|
|
21
|
+
throw e;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
// Override the merge order if --no-override flag set
|
|
25
|
+
if (options.noOverride === true) {
|
|
26
|
+
env = Object.assign({}, env, processLib.env);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
// Add in the system environment variables to our environment list
|
|
30
|
+
env = Object.assign({}, processLib.env, env);
|
|
31
|
+
}
|
|
32
|
+
if (options.recursive === true) {
|
|
33
|
+
for (const key of Object.keys(env)) {
|
|
34
|
+
if (env[key] !== undefined) {
|
|
35
|
+
env[key] = expandEnvs(env[key], env);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (options.expandEnvs === true) {
|
|
40
|
+
command = expandEnvs(command, env);
|
|
41
|
+
commandArgs = commandArgs.map(arg => expandEnvs(arg, env));
|
|
42
|
+
}
|
|
43
|
+
if (!command) {
|
|
44
|
+
throw new Error('env-cmd cannot be used as a standalone command. ' +
|
|
45
|
+
'Refer to the documentation for usage examples: https://npm.im/env-cmd');
|
|
46
|
+
}
|
|
47
|
+
// Execute the command with the given environment variables
|
|
48
|
+
const proc = spawn(command, commandArgs, {
|
|
49
|
+
stdio: 'inherit',
|
|
50
|
+
shell: options.useShell,
|
|
51
|
+
env,
|
|
52
|
+
});
|
|
53
|
+
// Handle any termination signals for parent and child proceses
|
|
54
|
+
const signals = new TermSignals({ verbose: options.verbose });
|
|
55
|
+
signals.handleUncaughtExceptions();
|
|
56
|
+
signals.handleTermSignals(proc);
|
|
57
|
+
return env;
|
|
58
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Environment } from './types.ts';
|
|
2
|
+
/**
|
|
3
|
+
* expandEnvs Replaces $var and ${var} in args and command with environment variables
|
|
4
|
+
* if the environment variable doesn't exist, it leaves it as is.
|
|
5
|
+
*/
|
|
6
|
+
export declare function expandEnvs(str: string, envs: Environment): string;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* expandEnvs Replaces $var and ${var} in args and command with environment variables
|
|
3
|
+
* if the environment variable doesn't exist, it leaves it as is.
|
|
4
|
+
*/
|
|
5
|
+
export function expandEnvs(str, envs) {
|
|
6
|
+
return str.replace(/(?<!\\)\$(\{\w+\}|\w+)?/g, (varName) => {
|
|
7
|
+
const varValue = envs[varName.startsWith('${') ? varName.slice(2, varName.length - 1) : varName.slice(1)];
|
|
8
|
+
return varValue ?? varName;
|
|
9
|
+
});
|
|
10
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { GetEnvVarOptions, Environment } from './types.ts';
|
|
2
|
+
export declare function getEnvVars(options?: GetEnvVarOptions): Promise<Environment>;
|
|
3
|
+
export declare function getEnvFile({ filePath, fallback, verbose }: {
|
|
4
|
+
filePath?: string;
|
|
5
|
+
fallback?: boolean;
|
|
6
|
+
verbose?: boolean;
|
|
7
|
+
}): Promise<Environment>;
|
|
8
|
+
export declare function getRCFile({ environments, filePath, verbose }: {
|
|
9
|
+
environments: string[];
|
|
10
|
+
filePath?: string;
|
|
11
|
+
verbose?: boolean;
|
|
12
|
+
}): Promise<Environment>;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { getRCFileVars } from './parse-rc-file.js';
|
|
2
|
+
import { getEnvFileVars } from './parse-env-file.js';
|
|
3
|
+
import { isLoaderError } from './utils.js';
|
|
4
|
+
const RC_FILE_DEFAULT_LOCATIONS = ['./.env-cmdrc', './.env-cmdrc.js', './.env-cmdrc.json'];
|
|
5
|
+
const ENV_FILE_DEFAULT_LOCATIONS = ['./.env', './.env.js', './.env.json'];
|
|
6
|
+
export async function getEnvVars(options = {}) {
|
|
7
|
+
options.envFile = options.envFile ?? {};
|
|
8
|
+
// Check for rc file usage
|
|
9
|
+
if (options.rc !== undefined) {
|
|
10
|
+
return await getRCFile({
|
|
11
|
+
environments: options.rc.environments,
|
|
12
|
+
filePath: options.rc.filePath,
|
|
13
|
+
verbose: options.verbose,
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return await getEnvFile({
|
|
17
|
+
filePath: options.envFile.filePath,
|
|
18
|
+
fallback: options.envFile.fallback,
|
|
19
|
+
verbose: options.verbose,
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
export async function getEnvFile({ filePath, fallback, verbose }) {
|
|
23
|
+
// Use env file
|
|
24
|
+
if (filePath !== undefined) {
|
|
25
|
+
try {
|
|
26
|
+
const env = await getEnvFileVars(filePath);
|
|
27
|
+
if (verbose === true) {
|
|
28
|
+
console.info(`Found .env file at path: ${filePath}`);
|
|
29
|
+
}
|
|
30
|
+
return env;
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
if (isLoaderError(error)) {
|
|
34
|
+
throw error;
|
|
35
|
+
}
|
|
36
|
+
if (verbose === true) {
|
|
37
|
+
console.info(`Failed to find .env file at path: ${filePath}`);
|
|
38
|
+
}
|
|
39
|
+
// Ignore error as we are just trying this location
|
|
40
|
+
}
|
|
41
|
+
if (fallback !== true) {
|
|
42
|
+
throw new Error(`Failed to find .env file at path: ${filePath}`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
// Use the default env file locations
|
|
46
|
+
for (const path of ENV_FILE_DEFAULT_LOCATIONS) {
|
|
47
|
+
try {
|
|
48
|
+
const env = await getEnvFileVars(path);
|
|
49
|
+
if (verbose === true) {
|
|
50
|
+
console.info(`Found .env file at default path: ${path}`);
|
|
51
|
+
}
|
|
52
|
+
return env;
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
// Ignore error because we are just trying this location
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
const error = `Failed to find .env file at default paths: [${ENV_FILE_DEFAULT_LOCATIONS.join(',')}]`;
|
|
59
|
+
if (verbose === true) {
|
|
60
|
+
console.info(error);
|
|
61
|
+
}
|
|
62
|
+
throw new Error(error);
|
|
63
|
+
}
|
|
64
|
+
export async function getRCFile({ environments, filePath, verbose }) {
|
|
65
|
+
// User provided an .rc file path
|
|
66
|
+
if (filePath !== undefined) {
|
|
67
|
+
try {
|
|
68
|
+
const env = await getRCFileVars({ environments, filePath });
|
|
69
|
+
if (verbose === true) {
|
|
70
|
+
console.info(`Found environments: [${environments.join(',')}] for .rc file at path: ${filePath}`);
|
|
71
|
+
}
|
|
72
|
+
return env;
|
|
73
|
+
}
|
|
74
|
+
catch (e) {
|
|
75
|
+
if (e instanceof Error) {
|
|
76
|
+
if (e.name === 'PathError') {
|
|
77
|
+
if (verbose === true) {
|
|
78
|
+
console.info(`Failed to find .rc file at path: ${filePath}`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (e.name === 'EnvironmentError') {
|
|
82
|
+
if (verbose === true) {
|
|
83
|
+
console.info(`Failed to find environments: [${environments.join(',')}] for .rc file at path: ${filePath}`);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
throw e;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
// Use the default .rc file locations
|
|
91
|
+
for (const path of RC_FILE_DEFAULT_LOCATIONS) {
|
|
92
|
+
try {
|
|
93
|
+
const env = await getRCFileVars({ environments, filePath: path });
|
|
94
|
+
if (verbose === true) {
|
|
95
|
+
console.info(`Found environments: [${environments.join(',')}] for default .rc file at path: ${path}`);
|
|
96
|
+
}
|
|
97
|
+
return env;
|
|
98
|
+
}
|
|
99
|
+
catch (e) {
|
|
100
|
+
if (e instanceof Error) {
|
|
101
|
+
if (e.name === 'EnvironmentError') {
|
|
102
|
+
const errorText = `Failed to find environments: [${environments.join(',')}] for .rc file at path: ${path}`;
|
|
103
|
+
if (verbose === true) {
|
|
104
|
+
console.info(errorText);
|
|
105
|
+
}
|
|
106
|
+
throw new Error(errorText);
|
|
107
|
+
}
|
|
108
|
+
if (e.name === 'ParseError') {
|
|
109
|
+
if (verbose === true) {
|
|
110
|
+
console.info(e.message);
|
|
111
|
+
}
|
|
112
|
+
throw new Error(e.message);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
const errorText = `Failed to find .rc file at default paths: [${RC_FILE_DEFAULT_LOCATIONS.join(',')}]`;
|
|
118
|
+
if (verbose === true) {
|
|
119
|
+
console.info(errorText);
|
|
120
|
+
}
|
|
121
|
+
throw new Error(errorText);
|
|
122
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function checkIfTypescriptSupported(): void;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export function checkIfTypescriptSupported() {
|
|
2
|
+
if (!process.features.typescript) {
|
|
3
|
+
const error = new Error('To load typescript files with env-cmd, you need to upgrade to node v23.6' +
|
|
4
|
+
' or later. See https://nodejs.org/en/learn/typescript/run-natively');
|
|
5
|
+
Object.assign(error, { code: 'ERR_UNKNOWN_FILE_EXTENSION' });
|
|
6
|
+
throw error;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { EnvCmdOptions, CommanderOptions } from './types.ts';
|
|
2
|
+
/**
|
|
3
|
+
* Parses the arguments passed into the cli
|
|
4
|
+
*/
|
|
5
|
+
export declare function parseArgs(args: string[]): EnvCmdOptions;
|
|
6
|
+
export declare function parseArgsUsingCommander(args: string[]): CommanderOptions;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { Command, Option, CommanderError } from '@commander-js/extra-typings';
|
|
2
|
+
import { parseArgList } from './utils.js';
|
|
3
|
+
import packageJson from '../package.json' with { type: 'json' };
|
|
4
|
+
/**
|
|
5
|
+
* Parses the arguments passed into the cli
|
|
6
|
+
*/
|
|
7
|
+
export function parseArgs(args) {
|
|
8
|
+
// Run the initial arguments through commander in order to determine
|
|
9
|
+
// which value in the args array is the `command` to execute
|
|
10
|
+
const program = parseArgsUsingCommander(args);
|
|
11
|
+
const command = program.args[0];
|
|
12
|
+
// Grab all arguments after the `command` in the args array
|
|
13
|
+
const commandArgs = args.splice(args.indexOf(command) + 1);
|
|
14
|
+
// Reprocess the args with the command and command arguments removed
|
|
15
|
+
// program = parseArgsUsingCommander(args.slice(0, args.indexOf(command)))
|
|
16
|
+
const parsedCmdOptions = program.opts();
|
|
17
|
+
// Set values for provided options
|
|
18
|
+
let noOverride = false;
|
|
19
|
+
// In commander `no-` negates the original value `override`
|
|
20
|
+
if (parsedCmdOptions.override === false) {
|
|
21
|
+
noOverride = true;
|
|
22
|
+
}
|
|
23
|
+
let useShell = false;
|
|
24
|
+
if (parsedCmdOptions.useShell === true) {
|
|
25
|
+
useShell = true;
|
|
26
|
+
}
|
|
27
|
+
let expandEnvs = false;
|
|
28
|
+
if (parsedCmdOptions.expandEnvs === true) {
|
|
29
|
+
expandEnvs = true;
|
|
30
|
+
}
|
|
31
|
+
let recursive = false;
|
|
32
|
+
if (parsedCmdOptions.recursive === true) {
|
|
33
|
+
recursive = true;
|
|
34
|
+
}
|
|
35
|
+
let verbose = false;
|
|
36
|
+
if (parsedCmdOptions.verbose === true) {
|
|
37
|
+
verbose = true;
|
|
38
|
+
}
|
|
39
|
+
let silent = false;
|
|
40
|
+
if (parsedCmdOptions.silent === true) {
|
|
41
|
+
silent = true;
|
|
42
|
+
}
|
|
43
|
+
let rc;
|
|
44
|
+
if (parsedCmdOptions.environments !== undefined
|
|
45
|
+
&& Array.isArray(parsedCmdOptions.environments)
|
|
46
|
+
&& parsedCmdOptions.environments.length !== 0) {
|
|
47
|
+
rc = {
|
|
48
|
+
environments: parsedCmdOptions.environments,
|
|
49
|
+
// if we get a boolean value assume not defined
|
|
50
|
+
filePath: parsedCmdOptions.file === true ?
|
|
51
|
+
undefined :
|
|
52
|
+
parsedCmdOptions.file,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
let envFile;
|
|
56
|
+
if (parsedCmdOptions.file !== undefined) {
|
|
57
|
+
envFile = {
|
|
58
|
+
// if we get a boolean value assume not defined
|
|
59
|
+
filePath: parsedCmdOptions.file === true ?
|
|
60
|
+
undefined :
|
|
61
|
+
parsedCmdOptions.file,
|
|
62
|
+
fallback: parsedCmdOptions.fallback,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
const options = {
|
|
66
|
+
command,
|
|
67
|
+
commandArgs,
|
|
68
|
+
envFile,
|
|
69
|
+
rc,
|
|
70
|
+
options: {
|
|
71
|
+
expandEnvs,
|
|
72
|
+
recursive,
|
|
73
|
+
noOverride,
|
|
74
|
+
silent,
|
|
75
|
+
useShell,
|
|
76
|
+
verbose,
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
if (verbose) {
|
|
80
|
+
console.info(`Options: ${JSON.stringify(options, null, 0)}`);
|
|
81
|
+
}
|
|
82
|
+
return options;
|
|
83
|
+
}
|
|
84
|
+
export function parseArgsUsingCommander(args) {
|
|
85
|
+
return new Command('env-cmd')
|
|
86
|
+
.description('CLI for executing commands using an environment from an env file.')
|
|
87
|
+
.version(packageJson.version, '-v, --version')
|
|
88
|
+
.usage('[options] -- <command> [...args]')
|
|
89
|
+
.option('-e, --environments [envs...]', 'The rc file environment(s) to use', parseArgList)
|
|
90
|
+
.option('-f, --file [path]', 'Custom env file path or .rc file path if \'-e\' used (default path: ./.env or ./.env-cmdrc.(js|cjs|mjs|json))')
|
|
91
|
+
.option('-x, --expand-envs', 'Replace $var and ${var} in args and command with environment variables')
|
|
92
|
+
.option('--recursive', 'Replace $var and ${var} in env file with the referenced environment variable')
|
|
93
|
+
.option('--fallback', 'Fallback to default env file path, if custom env file path not found')
|
|
94
|
+
.option('--no-override', 'Do not override existing environment variables')
|
|
95
|
+
.option('--silent', 'Ignore any env-cmd errors and only fail on executed program failure.')
|
|
96
|
+
.option('--use-shell', 'Execute the command in a new shell with the given environment')
|
|
97
|
+
.option('--verbose', 'Print helpful debugging information')
|
|
98
|
+
// TODO: Remove -r deprecation error on version >= v12
|
|
99
|
+
.addOption(new Option('-r, --rc-file [path]', 'Deprecated Option')
|
|
100
|
+
.hideHelp()
|
|
101
|
+
.argParser(() => { throw new CommanderError(1, 'deprecated-option', 'The -r flag has been deprecated, use the -f flag instead.'); }))
|
|
102
|
+
.allowUnknownOption(true)
|
|
103
|
+
.allowExcessArguments(true)
|
|
104
|
+
.parse(['_', '_', ...args], { from: 'node' });
|
|
105
|
+
}
|