repos / neovimcraft

website that makes it easy to find neovim plugins
git clone https://github.com/neurosnap/neovimcraft.git

neovimcraft / src / scripts
Eric Bower · 02 Apr 24

patch.ts

 1import scrapePluginData from "../../data/scrape.json" with { type: "json" };
 2import manualPluginData from "../../data/manual.json" with { type: "json" };
 3import scrapeConfigData from "../../data/scrape-config.json" with {
 4  type: "json",
 5};
 6import manualConfigData from "../../data/manual-config.json" with {
 7  type: "json",
 8};
 9
10import type { Resource, ResourceMap } from "../types.ts";
11import { getResourceId } from "../entities.ts";
12
13init().catch(console.error);
14
15async function init() {
16  const plugins = patch({
17    scrapeData: scrapePluginData as any,
18    manualData: manualPluginData as any,
19  });
20  await Deno.writeTextFile("./data/resources.json", plugins);
21
22  const config = patch({
23    scrapeData: scrapeConfigData as any,
24    manualData: manualConfigData as any,
25  });
26  await Deno.writeTextFile("./data/resources-config.json", config);
27}
28
29interface ResourceContainer {
30  resources: Resource[];
31}
32
33interface PatchOpt {
34  scrapeData: ResourceContainer;
35  manualData: ResourceContainer;
36}
37
38function patch(
39  { scrapeData, manualData }: PatchOpt,
40) {
41  const db: ResourceMap = {};
42  const scrapeResources = scrapeData.resources as Resource[];
43  scrapeResources.forEach((r: Resource) => {
44    db[getResourceId(r)] = r;
45  });
46
47  const manualResources = manualData.resources as Resource[];
48  // resource file trumps what we scrape so we can make changes to things like the tags
49  manualResources.forEach((r) => {
50    db[getResourceId(r)] = r;
51  });
52
53  const newResources = Object.values(db).sort((a, b) => {
54    if (a.username === b.username) {
55      return a.repo.localeCompare(b.repo);
56    }
57    return a.username.localeCompare(b.username);
58  });
59  const data = { resources: newResources };
60  const json = JSON.stringify(data, null, 2);
61  return json;
62}