diff --git a/packages/env/src/lib/create-env.test.ts b/packages/env/src/lib/create-env.test.ts index d7b1508fe0..09f27761ad 100644 --- a/packages/env/src/lib/create-env.test.ts +++ b/packages/env/src/lib/create-env.test.ts @@ -59,19 +59,6 @@ test('Reads file configuration from config path', () => { expect(readConfigurationFromFile).toHaveBeenCalledWith('./test/config/path'); }); -test('Skips environment variables that are not Directus configuration flags', () => { - vi.mocked(isDirectusVariable).mockImplementation((key) => { - return key === 'PROCESS'; - }); - - const env = createEnv(); - - expect(env).toEqual({ - PROCESS: 'test-process', - DEFAULT: 'test-default', - }); -}); - test('Reads value from file if key is a file key', () => { vi.mocked(readConfigurationFromFile).mockReturnValue({ TEST_FILE: './test/path', @@ -81,6 +68,10 @@ test('Reads value from file if key is a file key', () => { return key === 'TEST_FILE'; }); + vi.mocked(isDirectusVariable).mockImplementation((key) => { + return key === 'TEST_FILE'; + }); + vi.mocked(removeFileSuffix).mockReturnValue('TEST'); vi.mocked(readFileSync).mockReturnValue('file-contents'); @@ -96,6 +87,26 @@ test('Reads value from file if key is a file key', () => { }); }); +test('Passthrough file variables that are not Directus configuration flags', () => { + vi.mocked(readConfigurationFromFile).mockReturnValue({ + TEST_FILE: './test/path', + }); + + vi.mocked(isDirectusVariable).mockImplementation(() => { + return false; + }); + + const env = createEnv(); + + expect(readFileSync).not.toHaveBeenCalled(); + + expect(env).toEqual({ + PROCESS: 'test-process', + DEFAULT: 'test-default', + TEST_FILE: './test/path', + }); +}); + test('Throws error if file could not be read', () => { vi.mocked(readConfigurationFromFile).mockReturnValue({ TEST_FILE: './test/path', @@ -105,6 +116,10 @@ test('Throws error if file could not be read', () => { return key === 'TEST_FILE'; }); + vi.mocked(isDirectusVariable).mockImplementation((key) => { + return key === 'TEST_FILE'; + }); + vi.mocked(removeFileSuffix).mockReturnValue('TEST'); vi.mocked(readFileSync).mockImplementation(() => { diff --git a/packages/env/src/lib/create-env.ts b/packages/env/src/lib/create-env.ts index 6df077093b..b602da1ff7 100644 --- a/packages/env/src/lib/create-env.ts +++ b/packages/env/src/lib/create-env.ts @@ -17,18 +17,17 @@ export const createEnv = (): Env => { const output: Env = {}; - for (const [key, value] of Object.entries(rawConfiguration)) { - if (isDirectusVariable(key) === false) continue; - - if (isFileKey(key) && typeof value === 'string') { + for (let [key, value] of Object.entries(rawConfiguration)) { + if (isFileKey(key) && isDirectusVariable(key) && typeof value === 'string') { try { - output[removeFileSuffix(key)] = readFileSync(value, { encoding: 'utf8' }); + value = readFileSync(value, { encoding: 'utf8' }); + key = removeFileSuffix(key); } catch { throw new Error(`Failed to read value from file "${value}", defined in environment variable "${key}".`); } - } else { - output[key] = cast(value, key); } + + output[key] = cast(key, value); } return { ...DEFAULTS, ...output };