feat: Convert model files to JS
This commit is contained in:
70
packages/backend/src/models/app.js
Normal file
70
packages/backend/src/models/app.js
Normal file
@@ -0,0 +1,70 @@
|
||||
import fs from 'fs';
|
||||
import { join } from 'path';
|
||||
import appInfoConverter from '../helpers/app-info-converter';
|
||||
import getApp from '../helpers/get-app';
|
||||
|
||||
class App {
|
||||
static folderPath = join(__dirname, '../apps');
|
||||
static list = fs
|
||||
.readdirSync(this.folderPath)
|
||||
.filter((file) => fs.statSync(this.folderPath + '/' + file).isDirectory());
|
||||
|
||||
static async findAll(name, stripFuncs = true) {
|
||||
if (!name)
|
||||
return Promise.all(
|
||||
this.list.map(
|
||||
async (name) => await this.findOneByName(name, stripFuncs)
|
||||
)
|
||||
);
|
||||
|
||||
return Promise.all(
|
||||
this.list
|
||||
.filter((app) => app.includes(name.toLowerCase()))
|
||||
.map((name) => this.findOneByName(name, stripFuncs))
|
||||
);
|
||||
}
|
||||
|
||||
static async findOneByName(name, stripFuncs = false) {
|
||||
const rawAppData = await getApp(name.toLocaleLowerCase(), stripFuncs);
|
||||
|
||||
return appInfoConverter(rawAppData);
|
||||
}
|
||||
|
||||
static async findOneByKey(key, stripFuncs = false) {
|
||||
const rawAppData = await getApp(key, stripFuncs);
|
||||
|
||||
return appInfoConverter(rawAppData);
|
||||
}
|
||||
|
||||
static async checkAppAndAction(appKey, actionKey) {
|
||||
const app = await this.findOneByKey(appKey);
|
||||
|
||||
if (!actionKey) return;
|
||||
|
||||
const hasAction = app.actions?.find((action) => action.key === actionKey);
|
||||
|
||||
if (!hasAction) {
|
||||
throw new Error(
|
||||
`${app.name} does not have an action with the "${actionKey}" key!`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
static async checkAppAndTrigger(appKey, triggerKey) {
|
||||
const app = await this.findOneByKey(appKey);
|
||||
|
||||
if (!triggerKey) return;
|
||||
|
||||
const hasTrigger = app.triggers?.find(
|
||||
(trigger) => trigger.key === triggerKey
|
||||
);
|
||||
|
||||
if (!hasTrigger) {
|
||||
throw new Error(
|
||||
`${app.name} does not have a trigger with the "${triggerKey}" key!`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default App;
|
Reference in New Issue
Block a user