Skip to content

Commit 6e77d1d

Browse files
committed
feat: CLI implementation and enhance command-line features
- Updated the GitHub Actions workflow to use double quotes for npm cache. - Added CLI usage instructions and options to the README.md. - Introduced new dependencies: cross-spawn and minimist for improved command execution and argument parsing. - Refactored CLI tests to use the new runNodeCli function instead of runCli. - Created a new node-cli.ts file to encapsulate CLI logic and options parsing. - Enhanced the main CLI function to support multiple command-line flags and improved error handling. - Updated load.ts to utilize the new runNodeCli function.
1 parent 80cdf9a commit 6e77d1d

File tree

10 files changed

+980
-536
lines changed

10 files changed

+980
-536
lines changed

.github/workflows/node.js.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
uses: actions/setup-node@v4
2525
with:
2626
node-version: ${{ matrix.node-version }}
27-
cache: 'npm'
27+
cache: "npm"
2828
- name: Install dependencies
2929
run: npm ci
3030
- name: Build production bundle

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,51 @@ $ DOTENV_CONFIG_<OPTION>=value node -r dotenv-mono/load your_script.js
270270
$ DOTENV_CONFIG_ENCODING=latin1 DOTENV_CONFIG_DEBUG=true node -r dotenv-mono/load your_script.js dotenv_config_path=/custom/path/to/.env
271271
```
272272

273+
### CLI
274+
275+
You can use `dotenv-mono` as a command-line tool to run commands with environment variables loaded from your `.env` files. This is similar to `dotenv-cli` but with all the enhanced features of `dotenv-mono`.
276+
277+
```bash
278+
# Basic usage - load .env and run a command
279+
dotenv-mono -- node your_script.js
280+
281+
# Load specific .env file
282+
dotenv-mono -e .env.production -- node your_script.js
283+
284+
# Load multiple .env files
285+
dotenv-mono -e .env.local -e .env.production -- node your_script.js
286+
287+
# Set additional variables
288+
dotenv-mono -v NODE_ENV=production -v DEBUG=true -- node your_script.js
289+
290+
# Print a variable value
291+
dotenv-mono -p NODE_ENV
292+
293+
# Debug mode - see what files would be loaded
294+
dotenv-mono --debug -e .env.production
295+
296+
# Use all dotenv-mono features
297+
dotenv-mono --cwd /path/to/project --extension server --depth 3 -- node server.js
298+
```
299+
300+
#### CLI Options
301+
302+
| Option | Description |
303+
| --------------------- | ------------------------------------------------------------------------------------------------ |
304+
| `--help` | Print help message |
305+
| `--debug` | Output the files that would be processed but don't actually parse them or run the command |
306+
| `-e <path>` | Parse the file `<path>` as a `.env` file and add variables to the environment (multiple allowed) |
307+
| `-v <name>=<value>` | Put variable `<name>` into environment using `<value>` (multiple allowed) |
308+
| `-p <variable>` | Print value of `<variable>` to the console |
309+
| `--no-expand` | Skip variable expansion |
310+
| `--override` | Override system variables |
311+
| `--cwd <path>` | Specify the current working directory |
312+
| `--depth <number>` | Specify the max depth to reach when finding up the folder tree |
313+
| `--encoding <enc>` | Specify the encoding of your file containing environment variables |
314+
| `--extension <ext>` | Specify to load specific dotenv file used only on specific apps/packages |
315+
| `--defaults <file>` | Specify the defaults dotenv filename |
316+
| `--priorities <json>` | Specify the criteria of the filename priority to load as dotenv file |
317+
273318
## 💡 Methods
274319

275320
### Config

package-lock.json

Lines changed: 21 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,15 @@
3838
"coverage": "jest --coverage && start coverage/index.html"
3939
},
4040
"dependencies": {
41+
"cross-spawn": "^7.0.6",
4142
"dotenv": "^17.2.0",
42-
"dotenv-expand": "^12.0.2"
43+
"dotenv-expand": "^12.0.2",
44+
"minimist": "^1.2.8"
4345
},
4446
"devDependencies": {
47+
"@types/cross-spawn": "^6.0.6",
4548
"@types/jest": "^30.0.0",
49+
"@types/minimist": "^1.2.5",
4650
"@types/mock-fs": "^4.13.4",
4751
"@types/node": "^24.0.14",
4852
"@typescript-eslint/eslint-plugin": "^8.37.0",
@@ -86,49 +90,59 @@
8690
"url": "git+https://github.com/marcocesarato/dotenv-mono.git"
8791
},
8892
"keywords": [
89-
"monorepo",
9093
"dotenv",
94+
"dotenv-mono",
9195
"dotenv-expand",
92-
"expand",
93-
"vars",
96+
"monorepo",
97+
"env",
98+
"environment",
99+
"environment variables",
100+
"env loader",
101+
"env manager",
102+
"env file",
103+
".env",
104+
".env.local",
105+
".env.development",
106+
".env.production",
107+
"centralized",
108+
"config",
109+
"configuration",
94110
"variables",
95-
"load",
111+
"vars",
96112
"loader",
97-
"mono",
98-
"one",
99-
"single",
100-
"centralized",
101-
"repo",
102-
"changes",
113+
"load",
103114
"edit",
115+
"save",
116+
"manipulate",
104117
"priorities",
105-
"env",
106-
"environemnt",
107-
"local",
108-
"test",
109-
"production",
110-
"development",
111-
"prod",
112-
"dev",
118+
"default",
119+
"defaults",
120+
"shared",
121+
"share",
122+
"single",
123+
"one",
124+
"repo",
113125
"apps",
114126
"packages",
115127
"nextjs",
116128
"next.js",
117129
"react",
118130
"angular",
119-
".env",
120-
".env.local",
121-
".env.development",
122-
".env.production",
123131
"turbo",
124132
"turborepo",
125133
"storybook",
126-
"shared",
127-
"share",
128-
"default",
129-
"defaults",
130134
"cli",
131135
"console",
132-
"preload"
136+
"node",
137+
"typescript",
138+
"js",
139+
"javascript",
140+
"test",
141+
"production",
142+
"development",
143+
"prod",
144+
"dev",
145+
"preload",
146+
"changes"
133147
]
134148
}

0 commit comments

Comments
 (0)