-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcompile.ts
84 lines (78 loc) · 2.24 KB
/
compile.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import fs from "fs/promises";
import path from "path";
const compile = async () => {
const root = path.join(__dirname, "providers");
const data: any = {};
const providers = await fs.readdir(root);
for (const provider of providers) {
const versions = await fs.readdir(path.join(root, provider));
const config_file = await fs.readFile(
path.join(root, provider, "index.json"),
"utf8"
);
const config: {
label: string;
configs: {
latest_version: string;
topic_identifier: string;
};
} = JSON.parse(config_file);
data[provider] = {
label: config.label,
latest_version: config.configs.latest_version,
versions: {},
};
for (const version of versions.filter((v) => v !== "index.json")) {
data[provider].versions[version] = {};
const topics = await fs.readdir(path.join(root, provider, version));
data[provider].versions[version] = {};
for (const topic of topics) {
const topic_data = await fs.readFile(
path.join(root, provider, version, topic),
"utf8"
);
const parsed_topic = JSON.parse(topic_data);
data[provider].versions[version][parsed_topic.topic] = parsed_topic;
await fs.mkdir(path.join(__dirname, "public", "providers", provider), {
recursive: true,
});
await fs.writeFile(
path.join(
__dirname,
"public",
"providers",
provider,
`${version}.json`
),
JSON.stringify(data[provider].versions[version], null),
"utf8"
);
}
}
}
return data;
};
compile()
.then(async (data) => {
const providers = Object.keys(data).reduce((object, provider) => {
object[provider] = {
label: data[provider].label,
latest_version: data[provider].latest_version,
versions: Object.keys(data[provider].versions),
};
return object;
}, {});
await fs.writeFile(
path.join(__dirname, "public", "providers.json"),
JSON.stringify(providers, null),
"utf8"
);
})
.then(() => {
console.log("Compiled");
process.exit(0);
})
.catch((error) => {
console.error(error);
process.exit(1);
});