-
Notifications
You must be signed in to change notification settings - Fork 1
/
executeMethod.ts
68 lines (61 loc) · 2.13 KB
/
executeMethod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { Any } from "@cedelabs/demo-sdk";
import fs from "fs";
import path from "path";
import * as prettyjson from "prettyjson";
import { fileURLToPath } from "url";
import CedeSDK from "./sdk";
export type ExecuteMethodParams = {
cedeSDK: CedeSDK;
exchangeInstanceId: string;
exchangeId: string;
};
export type ExecuteMethod = (params: ExecuteMethodParams) => Promise<Any>;
export const getAvailableMethods = () => {
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename) + "/methods";
const folders = fs.readdirSync(__dirname, { withFileTypes: true });
const methods: Record<string, string[]> = {};
folders
.filter((folder) => folder.isDirectory())
.forEach((folder) => {
const folderPath = path.join(__dirname, folder.name);
const files = fs.readdirSync(folderPath);
methods[folder.name] = files.map((file) => file.replace(".ts", ""));
});
return methods;
};
export async function executeMethod(methodName: string, cedeSDK: CedeSDK, selectedExchange: string) {
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename) + "/methods";
const folders = fs.readdirSync(__dirname, { withFileTypes: true });
// open each folder and find the path to the method
let methodPath = "";
folders
.filter((folder) => folder.isDirectory())
.forEach((folder) => {
const folderPath = path.join(__dirname, folder.name);
const files = fs.readdirSync(folderPath);
const method = files.find((file) => file === `${methodName}.ts`);
if (method) {
methodPath = path.join(folderPath, method);
}
});
if (!methodPath) {
console.log(`Method not found: '${methodName}'`);
return;
}
console.log(` Executing...`);
const { executeMethod }: { executeMethod: ExecuteMethod } = await import(methodPath + `?update=${Date.now()}`);
let result: Any;
try {
result = await executeMethod({
cedeSDK,
exchangeInstanceId: selectedExchange,
exchangeId: selectedExchange,
});
} catch (e: Any) {
console.log(" Error:", e);
return;
}
console.log(prettyjson.render(result));
}