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