mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
Return all env vars in @directus/env (#21084)
Co-authored-by: Rijk van Zanten <rijkvanzanten@me.com>
This commit is contained in:
41
packages/env/src/lib/create-env.test.ts
vendored
41
packages/env/src/lib/create-env.test.ts
vendored
@@ -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(() => {
|
||||
|
||||
13
packages/env/src/lib/create-env.ts
vendored
13
packages/env/src/lib/create-env.ts
vendored
@@ -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 };
|
||||
|
||||
Reference in New Issue
Block a user