Commit 935df713 by Netfan Committed by GitHub

fix: app config support `.env.local` (#5012)

1 parent 17c7ce8f
import type { ApplicationPluginOptions } from '../typing'; import type { ApplicationPluginOptions } from '../typing';
import { existsSync } from 'node:fs';
import { join } from 'node:path'; import { join } from 'node:path';
import { fs } from '@vben/node-utils'; import { fs } from '@vben/node-utils';
...@@ -21,12 +22,11 @@ function getConfFiles() { ...@@ -21,12 +22,11 @@ function getConfFiles() {
const script = process.env.npm_lifecycle_script as string; const script = process.env.npm_lifecycle_script as string;
const reg = /--mode ([\d_a-z]+)/; const reg = /--mode ([\d_a-z]+)/;
const result = reg.exec(script); const result = reg.exec(script);
let mode = 'production';
if (result) { if (result) {
const mode = result[1]; mode = result[1] as string;
return ['.env', `.env.${mode}`];
} }
return ['.env', '.env.production']; return ['.env', '.env.local', `.env.${mode}`, `.env.${mode}.local`];
} }
/** /**
...@@ -42,11 +42,14 @@ async function loadEnv<T = Record<string, string>>( ...@@ -42,11 +42,14 @@ async function loadEnv<T = Record<string, string>>(
for (const confFile of confFiles) { for (const confFile of confFiles) {
try { try {
const envPath = await fs.readFile(join(process.cwd(), confFile), { const confFilePath = join(process.cwd(), confFile);
encoding: 'utf8', if (existsSync(confFilePath)) {
}); const envPath = await fs.readFile(confFilePath, {
const env = dotenv.parse(envPath); encoding: 'utf8',
envConfig = { ...envConfig, ...env }; });
const env = dotenv.parse(envPath);
envConfig = { ...envConfig, ...env };
}
} catch (error) { } catch (error) {
console.error(`Error while parsing ${confFile}`, error); console.error(`Error while parsing ${confFile}`, error);
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!