refactor: inline auth, data, triggers and actions in apps
This commit is contained in:
@@ -1,8 +1,4 @@
|
||||
import fs from 'fs';
|
||||
import { join } from 'path';
|
||||
import { IApp, IAuth, IAction, ITrigger, IData } from '@automatisch/types';
|
||||
|
||||
const appsPath = join(__dirname, '../apps');
|
||||
import { IApp } from '@automatisch/types';
|
||||
|
||||
async function getDefaultExport(path: string) {
|
||||
return (await import(path)).default;
|
||||
@@ -12,63 +8,12 @@ function stripFunctions<C>(data: C): C {
|
||||
return JSON.parse(JSON.stringify(data));
|
||||
}
|
||||
|
||||
async function getFileContent<C>(
|
||||
path: string,
|
||||
stripFuncs: boolean
|
||||
): Promise<C> {
|
||||
const fileContent = await getDefaultExport(path);
|
||||
|
||||
if (stripFuncs) {
|
||||
return stripFunctions(fileContent);
|
||||
}
|
||||
|
||||
return fileContent;
|
||||
}
|
||||
|
||||
async function getChildrenContentInDirectory<C>(
|
||||
path: string,
|
||||
stripFuncs: boolean
|
||||
): Promise<C[]> {
|
||||
const appSubdirectory = join(appsPath, path);
|
||||
const childrenContent = [];
|
||||
|
||||
if (fs.existsSync(appSubdirectory)) {
|
||||
const filesInSubdirectory = fs.readdirSync(appSubdirectory);
|
||||
|
||||
for (const filename of filesInSubdirectory) {
|
||||
const filePath = join(appSubdirectory, filename);
|
||||
const fileContent = await getFileContent<C>(filePath, stripFuncs);
|
||||
|
||||
childrenContent.push(fileContent);
|
||||
}
|
||||
|
||||
return childrenContent;
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
const getApp = async (appKey: string, stripFuncs = true) => {
|
||||
const appData: IApp = await getDefaultExport(`../apps/${appKey}`);
|
||||
|
||||
if (appData.supportsConnections) {
|
||||
appData.auth = await getFileContent<IAuth>(
|
||||
`../apps/${appKey}/auth`,
|
||||
stripFuncs
|
||||
);
|
||||
if (stripFuncs) {
|
||||
return stripFunctions(appData);
|
||||
}
|
||||
appData.triggers = await getChildrenContentInDirectory<ITrigger>(
|
||||
`${appKey}/triggers`,
|
||||
stripFuncs
|
||||
);
|
||||
appData.actions = await getChildrenContentInDirectory<IAction>(
|
||||
`${appKey}/actions`,
|
||||
stripFuncs
|
||||
);
|
||||
appData.data = await getChildrenContentInDirectory<IData>(
|
||||
`${appKey}/data`,
|
||||
stripFuncs
|
||||
);
|
||||
|
||||
return appData;
|
||||
};
|
||||
|
Reference in New Issue
Block a user