-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreadInfoFile.ts
55 lines (50 loc) · 1.47 KB
/
readInfoFile.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
import { getLatestInfo } from "./plugins.ts";
import { getAllDownloadCount } from "./utils/github.ts";
import { createAsyncLazy } from "./utils/mod.ts";
const infoLazy = createAsyncLazy<Readonly<PluginsData>>(async () => {
const data = await Deno.readTextFile("./info.json");
return JSON.parse(data);
});
// only typing what's used on the server
export interface PluginsData {
latest: PluginData[];
}
export interface PluginData {
name: string;
url: string;
version: string;
downloadCount: {
currentVersion: number;
allVersions: number;
};
}
export async function readInfoFile(): Promise<Readonly<PluginsData>> {
const infoObj = await infoLazy.get();
return {
...infoObj,
latest: await getLatest(infoObj.latest),
};
async function getLatest(latest: Readonly<PluginData>[]) {
const results = [];
for (const plugin of latest) {
const [username, pluginName] = plugin.name.split("/");
const info = pluginName
? await getLatestInfo(username, pluginName)
: await getLatestInfo("dprint", plugin.name);
if (info != null) {
results.push({
...plugin,
version: info.version,
url: info.url,
downloadCount: {
currentVersion: info.downloadCount,
allVersions: pluginName
? await getAllDownloadCount(username, pluginName)
: await getAllDownloadCount("dprint", plugin.name),
},
});
}
}
return results;
}
}