Skip to content

Commit

Permalink
Add support for setting a .env file
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aedart committed Nov 3, 2024
1 parent 7f5e52c commit 40896e7
Showing 1 changed file with 50 additions and 2 deletions.
52 changes: 50 additions & 2 deletions packages/cli/src/CliApplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import type {
import type {
Application
} from "@aedart/contracts/core";
import type { ParseArgsConfig } from "node:util";
import { Application as CoreApplication } from "@aedart/core";
import { CallbackWrapper } from "@aedart/support";
import { Command as CommanderJs } from "commander";
import SkipProcessExitError from "./exceptions/SkipProcessExitError";
import { version } from "../package.json";
import * as process from "node:process";
import { parseArgs } from "node:util";

/**
* Cli Application
Expand Down Expand Up @@ -149,13 +151,15 @@ export default class CliApplication
*/
public async run(argv?: readonly string[], options?: ParseOptions): Promise<boolean>
{
this.processEnvOption(argv);

return await this.core.run(CallbackWrapper.makeFor(
this,
this.parse,
[argv, options]
));
}

// TODO:
protected async parse(argv?: readonly string[], options?: ParseOptions): Promise<boolean>
{
Expand Down Expand Up @@ -232,7 +236,11 @@ export default class CliApplication
{
return (new CommanderJs())
.version(this.version)
.description(this.description);
.description(this.description)

// This option will be processed outside the "driver's" usual way of dealing with options!
// @see processEnvOption()
.option('--env <file>', 'set environment variables from supplied file.');
}

/**
Expand Down Expand Up @@ -267,4 +275,44 @@ export default class CliApplication
throw err;
})
}

/**
* Processes the `--env` option for the cli application
*
* @param {readonly string[]} [argv] Defaults to `process.argv` when no arguments given.
*
* @protected
*/
protected processEnvOption(argv?: readonly string[]): void
{
// Prepare options for the `parseArgs`.
const argsOptions: ParseArgsConfig = {
args: (argv as string[] | undefined),

// Define the '--env' option
options: {
'env': {
type: 'string',
//default: '.env'
}
},

// Ignore any other arguments and options...
strict: false,
}

// Parse the arguments and extract the values
const { values } = parseArgs(argsOptions);

// Resolve path to `.env` file.
const path = Reflect.has(values, 'env')
? values['env']
: undefined;

// Load environment variables into `process.env`, if a path has been provided.
// If nothing was specified, then avoid doing anything.
if (typeof path === 'string' && path.length > 0) {
process.loadEnvFile(path);
}
}
}

0 comments on commit 40896e7

Please sign in to comment.