-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall-plugins.ts
More file actions
57 lines (49 loc) · 1.29 KB
/
install-plugins.ts
File metadata and controls
57 lines (49 loc) · 1.29 KB
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
import { Plugin } from './types/plugin.js';
import download from 'download';
import fs from 'fs';
import os from 'os';
import { PluginInfo } from './types/strategy.js';
import { PluginConfig } from './types/common.js';
export async function installPlugins(pluginsInfo: PluginInfo[]): Promise<
Record<
string,
{
plugin: Plugin;
config?: PluginConfig;
}
>
> {
const plugins: Record<
string,
{
plugin: Plugin;
config?: PluginConfig;
}
> = {};
const saveDir = os.homedir() + '/dydx-bot/plugins';
if (!fs.existsSync(saveDir)) {
fs.mkdirSync(saveDir, { recursive: true });
}
await Promise.all(
pluginsInfo.map(async (pluginInfo) => {
try {
const fileName = `${pluginInfo.name}.js`;
const filePath = `${saveDir}/${fileName}`;
if (fs.existsSync(filePath) === false) {
await download(pluginInfo.url, saveDir, {
filename: fileName,
});
}
const fileContent = await import(`file://${filePath}`);
const plugin: Plugin = fileContent.default as Plugin;
plugins[pluginInfo.name] = {
plugin,
config: pluginInfo.config,
};
} catch (e) {
console.error('Failed to download plugin:', e);
}
}),
);
return plugins;
}