Skip to content

Commit 40896e7

Browse files
committed
Add support for setting a .env file
The --env option is processed outside command js, because of the upcoming "cli configurator" (configuration of the "core" application instance). Environment variables are simply injected into the process.env. This will then be returned by a custom bootstrapper for dealing with the env() calls, in custom configuration.
1 parent 7f5e52c commit 40896e7

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

packages/cli/src/CliApplication.ts

+50-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import type {
55
import type {
66
Application
77
} from "@aedart/contracts/core";
8+
import type { ParseArgsConfig } from "node:util";
89
import { Application as CoreApplication } from "@aedart/core";
910
import { CallbackWrapper } from "@aedart/support";
1011
import { Command as CommanderJs } from "commander";
1112
import SkipProcessExitError from "./exceptions/SkipProcessExitError";
1213
import { version } from "../package.json";
1314
import * as process from "node:process";
15+
import { parseArgs } from "node:util";
1416

1517
/**
1618
* Cli Application
@@ -149,13 +151,15 @@ export default class CliApplication
149151
*/
150152
public async run(argv?: readonly string[], options?: ParseOptions): Promise<boolean>
151153
{
154+
this.processEnvOption(argv);
155+
152156
return await this.core.run(CallbackWrapper.makeFor(
153157
this,
154158
this.parse,
155159
[argv, options]
156160
));
157161
}
158-
162+
159163
// TODO:
160164
protected async parse(argv?: readonly string[], options?: ParseOptions): Promise<boolean>
161165
{
@@ -232,7 +236,11 @@ export default class CliApplication
232236
{
233237
return (new CommanderJs())
234238
.version(this.version)
235-
.description(this.description);
239+
.description(this.description)
240+
241+
// This option will be processed outside the "driver's" usual way of dealing with options!
242+
// @see processEnvOption()
243+
.option('--env <file>', 'set environment variables from supplied file.');
236244
}
237245

238246
/**
@@ -267,4 +275,44 @@ export default class CliApplication
267275
throw err;
268276
})
269277
}
278+
279+
/**
280+
* Processes the `--env` option for the cli application
281+
*
282+
* @param {readonly string[]} [argv] Defaults to `process.argv` when no arguments given.
283+
*
284+
* @protected
285+
*/
286+
protected processEnvOption(argv?: readonly string[]): void
287+
{
288+
// Prepare options for the `parseArgs`.
289+
const argsOptions: ParseArgsConfig = {
290+
args: (argv as string[] | undefined),
291+
292+
// Define the '--env' option
293+
options: {
294+
'env': {
295+
type: 'string',
296+
//default: '.env'
297+
}
298+
},
299+
300+
// Ignore any other arguments and options...
301+
strict: false,
302+
}
303+
304+
// Parse the arguments and extract the values
305+
const { values } = parseArgs(argsOptions);
306+
307+
// Resolve path to `.env` file.
308+
const path = Reflect.has(values, 'env')
309+
? values['env']
310+
: undefined;
311+
312+
// Load environment variables into `process.env`, if a path has been provided.
313+
// If nothing was specified, then avoid doing anything.
314+
if (typeof path === 'string' && path.length > 0) {
315+
process.loadEnvFile(path);
316+
}
317+
}
270318
}

0 commit comments

Comments
 (0)