Eric Bower
·
02 Apr 24
process.ts
1import resourceFile from "../../data/resources.json" with { type: "json" };
2import resourceConfigFile from "../../data/resources-config.json" with {
3 type: "json",
4};
5
6import type { Plugin, Resource } from "../types.ts";
7import { createPlugin, getResourceId } from "../entities.ts";
8import { fetchGithubData, ghToken } from "../github.ts";
9// import { fetchSrhtData } from "../stht.ts";
10
11// const srhtToken = Deno.env.get("SRHT_ACCESS_TOKEN") || "";
12
13const option = Deno.args[0];
14if (option === "missing") {
15 console.log("PROCESSING MISSING RESOURCES");
16 processMissingResources().then(saveData).catch(console.error);
17} else {
18 console.log("PROCESSING ALL RESOURCES");
19 processResources(resourceFile.resources as Resource[])
20 .then(saveData)
21 .catch(console.error);
22
23 console.log("PROCESSING CONFIG RESOURCES");
24 processResources(Object.values(resourceConfigFile.resources) as Resource[])
25 .then(saveConfigData)
26 .catch(console.error);
27}
28
29async function processMissingResources() {
30 const dbFile = await Deno.readTextFile("./data/db.json");
31 const db = JSON.parse(dbFile.toString());
32 const missing: Resource[] = [];
33 const resources = resourceFile.resources as Resource[];
34 resources.forEach((r) => {
35 if (db.plugins[getResourceId(r)]) {
36 return;
37 }
38
39 missing.push(r);
40 });
41 console.log(`Missing ${missing.length} resources`);
42
43 const results = await processResources(missing);
44 const markdownFile = await Deno.readTextFile("./data/markdown.json");
45 const markdownJson = JSON.parse(markdownFile.toString());
46 const plugins = { ...db.plugins, ...results.plugins };
47 const markdown = { ...markdownJson.markdown, ...results.markdown };
48 return { plugins, markdown };
49}
50
51async function processResources(resources: Resource[]) {
52 const plugins: { [key: string]: Plugin } = {};
53 const markdown: { [key: string]: string } = {};
54
55 console.log(`Fetching ${resources.length} resources`);
56
57 for (let i = 0; i < resources.length; i += 1) {
58 const d = resources[i];
59
60 if (d.type === "srht") {
61 continue;
62 /*
63 01/12/2024 - SUSPENDING until sr.ht is back online
64
65 const result = await fetchSrhtData({ ...d, token: srhtToken });
66 const id = getResourceId(d);
67 if (!result.ok) {
68 console.log(result);
69 continue;
70 }
71
72 const repo = result.data.repo;
73 markdown[id] = result.data.readme;
74 plugins[id] = createPlugin({
75 type: "srht",
76 id,
77 username: d.username,
78 repo: d.repo,
79 tags: d.tags,
80 link: `https://git.sr.ht/~${id}`,
81 name: repo.name,
82 description: repo.description,
83 createdAt: repo.created,
84 updatedAt: repo.updated,
85 branch: result.data.branch,
86 });*/
87 } else if (d.type === "github") {
88 const result = await fetchGithubData({ ...d, token: ghToken });
89 if (!result.ok) {
90 continue;
91 }
92
93 const resp = result.data;
94 const id = getResourceId(d);
95
96 let updatedAt = "";
97 if (result.data.branch.ok) {
98 updatedAt = result.data.branch.data.commit.commit.committer.date;
99 }
100
101 markdown[id] = resp.readme;
102 plugins[id] = createPlugin({
103 type: "github",
104 id,
105 username: d.username,
106 repo: d.repo,
107 tags: d.tags,
108 name: resp.repo.name,
109 link: resp.repo.html_url,
110 homepage: resp.repo.homepage,
111 branch: resp.repo.default_branch,
112 openIssues: resp.repo.open_issues_count,
113 watchers: resp.repo.watchers_count,
114 forks: resp.repo.forks,
115 stars: resp.repo.stargazers_count,
116 subscribers: resp.repo.subscribers_count,
117 network: resp.repo.network_count,
118 description: resp.repo.description,
119 createdAt: resp.repo.created_at,
120 updatedAt: updatedAt,
121 });
122 }
123 }
124
125 return { plugins, markdown };
126}
127
128async function saveData({
129 plugins,
130 markdown,
131}: {
132 plugins: { [key: string]: Plugin };
133 markdown: { [key: string]: string };
134}) {
135 const pluginJson = JSON.stringify({ plugins }, null, 2);
136 const markdownJson = JSON.stringify({ markdown });
137 await Deno.writeTextFile("./data/db.json", pluginJson);
138 await Deno.writeTextFile("./data/markdown.json", markdownJson);
139}
140
141async function saveConfigData({
142 plugins,
143 markdown,
144}: {
145 plugins: { [key: string]: Plugin };
146 markdown: { [key: string]: string };
147}) {
148 const pluginJson = JSON.stringify({ plugins }, null, 2);
149 const markdownJson = JSON.stringify({ markdown });
150 await Deno.writeTextFile("./data/db-config.json", pluginJson);
151 await Deno.writeTextFile("./data/markdown-config.json", markdownJson);
152}