-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathcommunity-plugins.ts
More file actions
177 lines (144 loc) · 4.23 KB
/
Copy pathcommunity-plugins.ts
File metadata and controls
177 lines (144 loc) · 4.23 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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { defineCollection } from "astro:content";
import { z } from "astro/zod";
import { styleText } from "node:util";
import path from "node:path";
import fs from "node:fs/promises";
import { slug as generateSlug } from "github-slugger";
import communityPluginsJson from "./community-plugins.json";
const communityPluginsJsonSchema = z.object({
plugins: z.array(
z.object({
name: z.string(),
website: z.string().optional(),
npmPackage: z.string().optional(),
author: z.string(),
authorUrl: z.string(),
description: z.string(),
tags: z.array(z.string()),
}),
),
});
const communityPluginsCollectionSchema = z.object({
id: z.string(),
slug: z.string(),
name: z.string(),
website: z.string(),
author: z.string(),
authorUrl: z.string(),
description: z.string(),
tags: z.array(z.string()),
downloads: z.number(),
});
const MAX_MS_NPM_DOWNLOADS_CACHE = 24 * 60 * 60 * 1000;
async function getCachedDownloadsIfValid(
cachedResultPath: string,
): Promise<number | undefined> {
try {
const content = await fs.readFile(cachedResultPath, "utf-8");
const json = JSON.parse(content);
const dateStoredValueOf = json.dateStoredValueOf;
const downloads = json.downloads;
if (
typeof dateStoredValueOf !== "number" ||
typeof downloads !== "number"
) {
return undefined;
}
const now = new Date().valueOf();
const diff = now - dateStoredValueOf;
if (diff > MAX_MS_NPM_DOWNLOADS_CACHE) {
return undefined;
}
return downloads;
} catch {
return undefined;
}
}
async function saveCachedDownalods(
cachedResultPath: string,
downloads: number,
) {
const now = new Date().valueOf();
const json = {
dateStoredValueOf: now,
downloads,
};
fs.writeFile(cachedResultPath, JSON.stringify(json, null, 2), "utf-8");
}
function getPluginDownloadsCachedPath(pluginName: string) {
const cachedResultPath = path.join(
import.meta.dirname,
"../../cache/",
`${pluginName.replace(/[@\/\\]/g, "")}.json`,
);
return cachedResultPath;
}
async function getLastMonthDownloads(pluginName: string) {
const cachedResultPath = getPluginDownloadsCachedPath(pluginName);
const cached = await getCachedDownloadsIfValid(cachedResultPath);
if (cached !== undefined) {
console.log(
styleText(
["cyan", "bold"],
`Using cached downloads of community plugin ${pluginName}`,
),
);
return cached;
}
console.log(
styleText(
["magenta", "bold"],
`Fetching downloads of community plugin ${pluginName}`,
),
);
const endpoint = `https://api.npmjs.org/downloads/point/last-month/${pluginName}`;
const res = await fetch(endpoint);
if (res.status === 404) {
return 0;
}
if (res.status === 429) {
const retryAfter = res.headers.get("retry-after");
console.error(
`Too many requests when hitting ${endpoint}. retry-after: ${retryAfter}`,
);
}
if (!res.ok) {
throw new Error(
`Error fetching npm downloads of plugin ${pluginName} — ${res.statusText}`,
);
}
const json = (await res.json()) as { downloads: number };
const downloads = json.downloads;
await saveCachedDownalods(cachedResultPath, downloads);
return downloads;
}
export const communityPlugins = defineCollection({
loader: async () => {
const pluginsFile = communityPluginsJsonSchema.parse(communityPluginsJson);
const resolvedPlugins = [];
for (const plugin of pluginsFile.plugins) {
const npmPackage = plugin.npmPackage ?? plugin.name;
resolvedPlugins.push({
id: plugin.name,
slug: generateSlug(plugin.name.replace(/@\//, "")),
name: plugin.name,
npmPackage,
website:
plugin.website ??
`https://www.npmjs.com/package/${plugin.npmPackage ?? plugin.name}`,
author: plugin.author,
authorUrl: plugin.authorUrl,
description: plugin.description,
tags: plugin.tags,
downloads: await getLastMonthDownloads(
plugin.npmPackage ?? plugin.name,
),
});
}
const sortedPlugins = resolvedPlugins.sort(
(a, b) => b.downloads - a.downloads,
);
return sortedPlugins;
},
schema: communityPluginsCollectionSchema,
});