From dbceed563a8f09cfc090bb9580d1c356ca89bf8b Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Tue, 8 Nov 2022 00:44:23 +0100 Subject: [PATCH] feat: eager load apps --- packages/backend/src/helpers/get-app.ts | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/packages/backend/src/helpers/get-app.ts b/packages/backend/src/helpers/get-app.ts index be42581e..2dcea731 100644 --- a/packages/backend/src/helpers/get-app.ts +++ b/packages/backend/src/helpers/get-app.ts @@ -1,3 +1,5 @@ +import path from 'node:path'; +import fs from 'node:fs'; import { IAction, IApp, @@ -8,8 +10,22 @@ import { import { omit, cloneDeep } from 'lodash'; import addReconnectionSteps from './add-reconnection-steps'; -async function getDefaultExport(path: string) { - return (await import(path)).default; +type TApps = Record>; +const apps = fs + .readdirSync( + path.resolve(__dirname, `../apps/`), + { withFileTypes: true } + ) + .reduce((apps, dirent) => { + if (!dirent.isDirectory()) return apps; + + apps[dirent.name] = import(path.resolve(__dirname, '../apps', dirent.name)); + + return apps; + }, {} as TApps); + +async function getDefaultExport(appKey: string) { + return (await apps[appKey]).default; } function stripFunctions(data: C): C { @@ -17,7 +33,7 @@ function stripFunctions(data: C): C { } const getApp = async (appKey: string, stripFuncs = true) => { - let appData: IApp = cloneDeep(await getDefaultExport(`../apps/${appKey}`)); + let appData: IApp = cloneDeep(await getDefaultExport(appKey)); if (appData.auth) { appData = addReconnectionSteps(appData);