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

resource.ts

 1import resourceFile from "../../data/resources.json" with { type: "json" };
 2import resourceConfigFile from "../../data/resources-config.json" with {
 3  type: "json",
 4};
 5import manualFile from "../../data/manual.json" with { type: "json" };
 6import manualConfigFile from "../../data/manual-config.json" with {
 7  type: "json",
 8};
 9
10import type { Resource, ResourceMap } from "../types.ts";
11import { createResource, getResourceId } from "../entities.ts";
12
13type Option = "plugin" | "config";
14
15const forges = ["github", "srht"];
16const forgesStr = forges.join(",");
17
18let option = Deno.args[0];
19if (!option) {
20  option = "plugin";
21}
22if (option !== "config" && option !== "plugin") {
23  throw new Error('"config" and "plugin" are the only two choices');
24}
25
26const resource = cli(option as Option);
27save(resource).catch(console.error);
28
29async function save(resource: Resource | undefined) {
30  if (!resource) return;
31  if (option === "plugin") {
32    manualFile.resources.push(resource);
33    const json = JSON.stringify(manualFile, null, 2);
34    await Deno.writeTextFile("./data/manual.json", json);
35  } else {
36    manualConfigFile.resources.push(resource);
37    const json = JSON.stringify(manualConfigFile, null, 2);
38    await Deno.writeTextFile("./data/manual-config.json", json);
39  }
40}
41
42function cli(opt: "config" | "plugin") {
43  const type = prompt(`code forge [${forgesStr}] (default: github):`) ||
44    "github";
45  if (!forges.includes(type)) {
46    throw new Error(`${type} is not a valid code forge, choose ${forgesStr}`);
47  }
48
49  const name = prompt("name (username/repo):") || "";
50  let [username, repo] = name.split("/");
51  if (type === "srht" && username[0] === "~") {
52    username = username.replace("~", "");
53  }
54
55  if (opt === "plugin") {
56    const foundResource = (resourceFile.resources as Resource[]).find(
57      (r) => getResourceId(r) === name,
58    );
59    if (foundResource) {
60      console.log(`${name} aleady found in resources, not adding`);
61      return;
62    }
63  } else {
64    const resources = Object.values(
65      resourceConfigFile.resources as ResourceMap,
66    );
67    const foundResource = resources.find(
68      (r) => getResourceId(r) === name,
69    );
70    if (foundResource) {
71      console.log(`${name} aleady found in resources, not adding`);
72      return;
73    }
74  }
75
76  let tags: string[] = [];
77  if (opt === "plugin") {
78    console.log(
79      "\nNOTICE: Please review all current tags and see if any fit, only add new tags if absolutely necessary\n",
80    );
81    const tagsRes = prompt("tags (comma separated):") || "";
82    tags = tagsRes.split(",");
83  }
84
85  return createResource({
86    type: type as any,
87    username,
88    repo,
89    tags,
90  });
91}