-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload-strategies.ts
More file actions
31 lines (26 loc) · 963 Bytes
/
load-strategies.ts
File metadata and controls
31 lines (26 loc) · 963 Bytes
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
import { Strategy } from './types/strategy.js';
import { strategySchema } from './validations/strategy-schema.js';
import { readFileSync } from 'fs';
import config from './config.js';
export const loadStrategies = async (): Promise<Strategy[]> => {
const loadedStrategies: Strategy[] = [];
try {
const loadedFile = readFileSync(config.strategiesFilePath, 'utf-8');
const importedJson: Strategy[] = JSON.parse(loadedFile);
importedJson.forEach((strategy) => {
const isValid = Object.keys(strategySchema).every((key) => {
const typedKey = key as keyof Strategy;
return strategySchema[typedKey](strategy[typedKey]);
});
if (!isValid) {
throw new Error('Invalid strategy file');
}
loadedStrategies.push(strategy as Strategy);
});
} catch (error) {
console.error(
`Error loading a strategy file ${config.strategiesFilePath}: ${error}`,
);
}
return loadedStrategies;
};