Skip to content

Commit ee16814

Browse files
committed
feat: allow any env to be read from a file
1 parent 2f19140 commit ee16814

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/lib/config/read/env.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { log } from '@/lib/logger';
22
import { parse } from './transform';
3+
import { readFileSync } from 'node:fs';
34

45
export type EnvType = 'string' | 'string[]' | 'number' | 'boolean' | 'byte' | 'ms' | 'json';
56
export function env(property: string, env: string | string[], type: EnvType, isDb: boolean = false) {
@@ -178,7 +179,17 @@ export function readEnv(): EnvResult {
178179
env.variable = env.variable.find((v) => process.env[v] !== undefined) || 'DATABASE_URL';
179180
}
180181

181-
const value = process.env[env.variable];
182+
let value = process.env[env.variable];
183+
const valueFileName = process.env[`${env.variable}_FILE`];
184+
if (valueFileName) {
185+
try {
186+
value = readFileSync(valueFileName, 'utf-8').trim();
187+
logger.debug('Using env value from file', { variable: env.variable, file: valueFileName });
188+
} catch (e) {
189+
logger.error(`Failed to read env value from file for ${env.variable}. Skipping...`).error(e as Error);
190+
continue;
191+
}
192+
}
182193

183194
if (value === undefined) continue;
184195

0 commit comments

Comments
 (0)