-
Notifications
You must be signed in to change notification settings - Fork 0
/
configs.ts
54 lines (46 loc) · 1.29 KB
/
configs.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
const CONFIGS_DIR = "./configs";
const configFiles: string[] = [];
const readDirForConfigs = async (dir: string) => {
for await (const entry of Deno.readDir(dir)) {
if (entry.isDirectory) {
await readDirForConfigs(`${dir}/${entry.name}`);
} else {
configFiles.push(`${dir}/${entry.name}`);
}
}
};
await readDirForConfigs(CONFIGS_DIR);
export interface Config {
priority: number;
platforms: string[];
extends?: [string, string, string];
type: string;
id: string;
}
const configs: Config[] = [];
for (const file of configFiles) {
const data = await Deno.readTextFile(file);
const config: Config = JSON.parse(data);
configs.push(config);
}
export const getConfig = (
platform: string,
type: string,
id: string,
): Config | undefined => {
const config =
configs.filter((config) =>
config.platforms.includes(platform) && config.type === type &&
config.id === id
).sort((a, b) => b.priority - a.priority)[0];
if (!config && platform !== "generic") return getConfig("generic", type, id);
else if (!config) return;
if (config.extends) {
const [platform, type, id] = config.extends;
const extendedConfig = getConfig(platform, type, id);
if (extendedConfig) {
return { ...extendedConfig, ...config };
}
}
return config;
};