Plugs: Add command

This commit is contained in:
Zef Hemel
2022-11-20 10:56:52 +01:00
parent 7f7be814fc
commit 5d89e15959
4 changed files with 49 additions and 2 deletions
+35
View File
@@ -3,6 +3,10 @@ import type { Manifest } from "../../common/manifest.ts";
import { editor, space, system } from "$sb/silverbullet-syscall/mod.ts";
import { readYamlPage } from "$sb/lib/yaml_page.ts";
import { writePage } from "$sb/silverbullet-syscall/space.ts";
const plugsPrelude =
"This file lists all plugs that SilverBullet will load. Run the {[Plugs: Update]} command to update and reload this list of plugs.\n\n";
export async function updatePlugsCommand() {
await editor.save();
@@ -16,6 +20,37 @@ export async function updatePlugsCommand() {
}
}
export async function addPlugCommand() {
let name = await editor.prompt("Plug URI:");
if (!name) {
return;
}
// Support people copy & pasting the YAML version
if (name.startsWith("-")) {
name = name.replace(/^\-\s*/, "");
}
let plugList: string[] = [];
try {
plugList = await readYamlPage("PLUGS");
} catch (e: any) {
console.error("ERROR", e);
}
if (plugList.includes(name)) {
await editor.flashNotification("Plug already installed", "error");
return;
}
plugList.push(name);
// await writeYamlPage("PLUGS", plugList, plugsPrelude);
await writePage(
"PLUGS",
plugsPrelude + "```yaml\n" + plugList.map((p) => `- ${p}`).join("\n") +
"\n```",
);
await editor.navigate("PLUGS");
await system.invokeFunction("server", "updatePlugs");
await editor.flashNotification("Plug added!");
}
export async function updatePlugs() {
let plugList: string[] = [];
try {