mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
Simplify parse-env util
This commit is contained in:
13
api/src/utils/get-config-from-env.ts
Normal file
13
api/src/utils/get-config-from-env.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import camelcase from 'camelcase';
|
||||
import env from '../env';
|
||||
|
||||
export function getConfigFromEnv(prefix: string) {
|
||||
const config: any = {};
|
||||
|
||||
for (const [key, value] of Object.entries(env)) {
|
||||
if (key.toLowerCase().startsWith(prefix.toLowerCase()) === false) continue;
|
||||
config[camelcase(key.slice(prefix.length))] = value;
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
import camelcase from 'camelcase';
|
||||
import env from '../env';
|
||||
|
||||
export default function parseEnv(splitWord: number, envType?: string) {
|
||||
// splitWord = where to spit the string to exlude any prewords
|
||||
const config: any = {};
|
||||
const storageConfig: any = { disks: {} };
|
||||
|
||||
for (const [key, value] of Object.entries(env)) {
|
||||
if (envType === 'storage') {
|
||||
if (key.startsWith('STORAGE') === false) continue;
|
||||
if (key === 'STORAGE_LOCATIONS') continue;
|
||||
if (key.endsWith('PUBLIC_URL')) continue;
|
||||
|
||||
const disk = key.split('_')[1].toLowerCase();
|
||||
if (!storageConfig.disks[disk]) storageConfig.disks[disk] = { config: {} };
|
||||
|
||||
if (key.endsWith('DRIVER')) {
|
||||
storageConfig.disks[disk].driver = value;
|
||||
continue;
|
||||
}
|
||||
storageConfig.disks[disk].config[configCamel(key, splitWord)] = value;
|
||||
}
|
||||
if (envType === 'redis') {
|
||||
if (key.startsWith('REDIS')) {
|
||||
config[configCamel(key, splitWord)] = value;
|
||||
}
|
||||
}
|
||||
|
||||
if (!envType) {
|
||||
config[configCamel(key, splitWord)] = value;
|
||||
}
|
||||
}
|
||||
if (envType === 'storage') {
|
||||
return storageConfig;
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
function configCamel(key: string, sWord: number) {
|
||||
return camelcase(key.split('_').filter((_, index) => [0, sWord].includes(index) === false));
|
||||
}
|
||||
Reference in New Issue
Block a user