From 021f1c2c98a268efa8de60620eb5aaa96954eef7 Mon Sep 17 00:00:00 2001 From: "Sakamoto, Kazunori" Date: Sat, 16 Sep 2023 10:31:29 +0900 Subject: [PATCH] feat!: add --auto-cascade-env (#166) --- packages/shared-lib-node/src/env.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/shared-lib-node/src/env.ts b/packages/shared-lib-node/src/env.ts index 1caea62c..8221c8bc 100644 --- a/packages/shared-lib-node/src/env.ts +++ b/packages/shared-lib-node/src/env.ts @@ -6,6 +6,7 @@ interface Options { env?: (string | number)[]; cascadeEnv?: string; cascadeNodeEnv?: boolean; + autoCascadeEnv?: boolean; verbose?: boolean; } @@ -16,14 +17,20 @@ export const yargsOptionsBuilderForEnv = { }, 'cascade-env': { description: - 'environment to load cascading .env files (e.g., `.env`, `.env.`, `.env.local` and `.env..local`)', + 'Environment to load cascading .env files (e.g., `.env`, `.env.`, `.env.local` and `.env..local`). Preferred over `cascade-node-env` and `auto-cascade-env`.', type: 'string', }, 'cascade-node-env': { description: - 'environment to load cascading .env files (e.g., `.env`, `.env.`, `.env.local` and `.env..local`). If NODE_ENV is falsy, "development" is applied. Preferred over `cascade`.', + 'Same with --cascade-env=. If NODE_ENV is falsy, "development" is applied. Preferred over `auto-cascade-env`.', type: 'boolean', }, + 'auto-cascade-env': { + description: + 'Same with --cascade-env=. If they are falsy, "development" is applied.', + type: 'boolean', + default: true, + }, } as const; /** @@ -31,7 +38,13 @@ export const yargsOptionsBuilderForEnv = { * */ export function loadEnvironmentVariables(argv: Options, cwd: string): Record { let envPaths = (argv.env ?? []).map((envPath) => envPath.toString()); - const cascade = argv.cascadeNodeEnv ? process.env.NODE_ENV || 'development' : argv.cascadeEnv; + const cascade = + argv.cascadeEnv ?? + (argv.cascadeNodeEnv + ? process.env.NODE_ENV || 'development' + : argv.autoCascadeEnv + ? process.env.WB_ENV || process.env.APP_ENV || process.env.NODE_ENV || 'development' + : undefined); if (typeof cascade === 'string') { if (envPaths.length === 0) envPaths.push('.env'); envPaths = envPaths.flatMap((envPath) =>