feat: Convert helpers to use JS files

This commit is contained in:
Faruk AYDIN
2024-01-04 19:55:41 +01:00
parent 8819ddefa7
commit 85141812d9
48 changed files with 259 additions and 384 deletions

View File

@@ -0,0 +1,88 @@
import path from 'node:path';
import fs from 'node:fs';
import { omit, cloneDeep } from 'lodash';
import addAuthenticationSteps from './add-authentication-steps';
import addReconnectionSteps from './add-reconnection-steps';
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;
}, {});
async function getAppDefaultExport(appKey) {
if (!Object.prototype.hasOwnProperty.call(apps, appKey)) {
throw new Error(
`An application with the "${appKey}" key couldn't be found.`
);
}
return (await apps[appKey]).default;
}
function stripFunctions(data) {
return JSON.parse(JSON.stringify(data));
}
const getApp = async (appKey, stripFuncs = true) => {
let appData = cloneDeep(await getAppDefaultExport(appKey));
if (appData.auth) {
appData = addAuthenticationSteps(appData);
appData = addReconnectionSteps(appData);
}
appData.triggers = appData?.triggers?.map((trigger) => {
return addStaticSubsteps('trigger', appData, trigger);
});
appData.actions = appData?.actions?.map((action) => {
return addStaticSubsteps('action', appData, action);
});
if (stripFuncs) {
return stripFunctions(appData);
}
return appData;
};
const chooseConnectionStep = {
key: 'chooseConnection',
name: 'Choose connection',
};
const testStep = (stepType) => {
return {
key: 'testStep',
name: stepType === 'trigger' ? 'Test trigger' : 'Test action',
};
};
const addStaticSubsteps = (stepType, appData, step) => {
const computedStep = omit(step, ['arguments']);
computedStep.substeps = [];
if (appData.supportsConnections) {
computedStep.substeps.push(chooseConnectionStep);
}
if (step.arguments) {
computedStep.substeps.push({
key: 'chooseTrigger',
name: stepType === 'trigger' ? 'Set up a trigger' : 'Set up action',
arguments: step.arguments,
});
}
computedStep.substeps.push(testStep(stepType));
return computedStep;
};
export default getApp;