1
0
silverbullet/plugs/core/plugmanager.ts

145 lines
4.5 KiB
TypeScript
Raw Normal View History

2022-10-14 13:11:33 +00:00
import { events } from "$sb/plugos-syscall/mod.ts";
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";
2022-11-20 09:56:52 +00:00
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";
2022-06-28 12:14:15 +00:00
export async function updatePlugsCommand() {
2022-10-14 13:11:33 +00:00
await editor.save();
await editor.flashNotification("Updating plugs...");
try {
2022-10-14 13:11:33 +00:00
await system.invokeFunction("server", "updatePlugs");
await editor.flashNotification("And... done!");
system.reloadPlugs();
} catch (e: any) {
2022-10-14 13:11:33 +00:00
editor.flashNotification("Error updating plugs: " + e.message, "error");
}
2022-06-28 12:14:15 +00:00
}
2022-11-20 09:56:52 +00:00
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);
2023-01-15 10:20:02 +00:00
await space.writePage(
2022-11-20 09:56:52 +00:00
"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!");
system.reloadPlugs();
2022-11-20 09:56:52 +00:00
}
2022-06-28 12:14:15 +00:00
export async function updatePlugs() {
let plugList: string[] = [];
try {
2022-07-29 16:26:42 +00:00
const plugListRead: any[] = await readYamlPage("PLUGS");
2022-09-12 12:50:37 +00:00
plugList = plugListRead.filter((plug) => typeof plug === "string");
2022-07-29 16:26:42 +00:00
if (plugList.length !== plugListRead.length) {
2022-09-12 12:50:37 +00:00
throw new Error(
2022-10-12 09:47:13 +00:00
`Some of the plugs were not in a yaml list format, they were ignored`,
2022-09-12 12:50:37 +00:00
);
2022-07-29 16:26:42 +00:00
}
} catch (e: any) {
throw new Error(`Error processing PLUGS: ${e.message}`);
2022-06-28 12:14:15 +00:00
}
console.log("Plug YAML", plugList);
2022-10-14 13:11:33 +00:00
const allPlugNames: string[] = [];
for (const plugUri of plugList) {
const [protocol, ...rest] = plugUri.split(":");
const manifests = await events.dispatchEvent(
`get-plug:${protocol}`,
rest.join(":"),
);
2022-06-28 12:14:15 +00:00
if (manifests.length === 0) {
console.error("Could not resolve plug", plugUri);
}
// console.log("Got manifests", plugUri, protocol, manifests);
2022-10-14 13:11:33 +00:00
const manifest = manifests[0];
2022-06-28 12:14:15 +00:00
allPlugNames.push(manifest.name);
// console.log("Writing", `_plug/${manifest.name}`);
2022-10-14 13:11:33 +00:00
await space.writeAttachment(
2022-09-12 12:50:37 +00:00
`_plug/${manifest.name}.plug.json`,
2023-01-13 14:41:29 +00:00
"utf8",
2022-10-12 09:47:13 +00:00
JSON.stringify(manifest),
2022-06-28 12:14:15 +00:00
);
}
// And delete extra ones
2022-10-14 13:11:33 +00:00
for (const existingPlug of await space.listPlugs()) {
const plugName = existingPlug.substring(
2022-09-12 12:50:37 +00:00
"_plug/".length,
2022-10-12 09:47:13 +00:00
existingPlug.length - ".plug.json".length,
2022-09-12 12:50:37 +00:00
);
2022-11-28 07:48:09 +00:00
// console.log("Considering", plugName);
2022-09-12 12:50:37 +00:00
if (!allPlugNames.includes(plugName)) {
2022-11-28 07:48:09 +00:00
// console.log("Removing plug", plugName);
2022-10-14 13:11:33 +00:00
await space.deleteAttachment(existingPlug);
2022-06-28 12:14:15 +00:00
}
}
2022-10-21 18:20:58 +00:00
system.reloadPlugs();
2022-06-28 12:14:15 +00:00
}
2022-06-29 13:02:53 +00:00
export async function getPlugHTTPS(url: string): Promise<Manifest> {
2022-10-14 13:11:33 +00:00
const fullUrl = `https:${url}`;
2022-06-29 13:02:53 +00:00
console.log("Now fetching plug manifest from", fullUrl);
2022-10-14 13:11:33 +00:00
const req = await fetch(fullUrl);
2022-06-29 13:02:53 +00:00
if (req.status !== 200) {
throw new Error(`Could not fetch plug manifest from ${fullUrl}`);
}
2022-10-14 13:11:33 +00:00
return req.json();
2022-06-29 13:02:53 +00:00
}
2022-10-14 13:11:33 +00:00
export function getPlugGithub(identifier: string): Promise<Manifest> {
const [owner, repo, path] = identifier.split("/");
2022-06-29 13:02:53 +00:00
let [repoClean, branch] = repo.split("@");
if (!branch) {
branch = "main"; // or "master"?
}
return getPlugHTTPS(
2022-10-12 09:47:13 +00:00
`//raw.githubusercontent.com/${owner}/${repoClean}/${branch}/${path}`,
2022-06-29 13:02:53 +00:00
);
}
2022-07-23 18:57:59 +00:00
2022-09-12 12:50:37 +00:00
export async function getPlugGithubRelease(
2022-10-12 09:47:13 +00:00
identifier: string,
2022-09-12 12:50:37 +00:00
): Promise<Manifest> {
2022-07-23 18:57:59 +00:00
let [owner, repo, version] = identifier.split("/");
if (!version || version === "latest") {
2022-09-12 12:50:37 +00:00
console.log("fetching the latest version");
const req = await fetch(
2022-10-12 09:47:13 +00:00
`https://api.github.com/repos/${owner}/${repo}/releases/latest`,
2022-09-12 12:50:37 +00:00
);
2022-07-23 18:57:59 +00:00
if (req.status !== 200) {
2022-09-12 12:50:37 +00:00
throw new Error(
2022-10-12 09:47:13 +00:00
`Could not fetch latest relase manifest from ${identifier}}`,
2022-09-12 12:50:37 +00:00
);
2022-07-23 18:57:59 +00:00
}
const result = await req.json();
version = result.name;
2022-09-12 12:50:37 +00:00
}
2022-10-12 09:47:13 +00:00
const finalUrl =
`//github.com/${owner}/${repo}/releases/download/${version}/${repo}.plug.json`;
2022-07-23 18:57:59 +00:00
return getPlugHTTPS(finalUrl);
2022-09-12 12:50:37 +00:00
}