feat: eager load apps

This commit is contained in:
Ali BARIN
2022-11-08 00:44:23 +01:00
parent cbf1d86c26
commit dbceed563a

View File

@@ -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<string, Promise<{ default: IApp }>>;
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<C>(data: C): C {
@@ -17,7 +33,7 @@ function stripFunctions<C>(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);