Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Packages / Env: Allow cast file or default to string #22164

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/env/src/lib/create-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { readConfigurationFromProcess } from '../utils/read-configuration-from-p
import { removeFileSuffix } from '../utils/remove-file-suffix.js';
import { cast } from './cast.js';
import { readConfigurationFromFile } from './read-configuration-from-file.js';
import { getCastFlag } from '../utils/has-cast-prefix.js';

export const createEnv = (): Env => {
const baseConfiguration = readConfigurationFromProcess();
Expand All @@ -25,7 +26,9 @@ export const createEnv = (): Env => {
for (let [key, value] of Object.entries(rawConfiguration)) {
if (isFileKey(key) && isDirectusVariable(key) && typeof value === 'string') {
try {
value = readFileSync(value, { encoding: 'utf8' });
const castFlag = getCastFlag(value);
const castPrefix = (castFlag ? castFlag : 'string') + ':';
value = castPrefix + readFileSync(castFlag ? value.replace(castPrefix, '') : value, { encoding: 'utf8' });
key = removeFileSuffix(key);
} catch {
throw new Error(`Failed to read value from file "${value}", defined in environment variable "${key}".`);
Expand Down