Return all env vars in @directus/env (#21084)

Co-authored-by: Rijk van Zanten <rijkvanzanten@me.com>
This commit is contained in:
Pascal Jufer
2024-02-06 17:43:11 +01:00
committed by GitHub
parent b1e7bf4f1f
commit d314f82d54
2 changed files with 34 additions and 20 deletions

View File

@@ -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(() => {

View File

@@ -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 };