1
0
silverbullet/plugs/core/plugmanager.ts

155 lines
4.9 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";
import { builtinPlugNames } from "../builtin_plugs.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 {
let plugList: string[] = [];
try {
const plugListRead: any[] = await readYamlPage("PLUGS");
plugList = plugListRead.filter((plug) => typeof plug === "string");
if (plugList.length !== plugListRead.length) {
throw new Error(
`Some of the plugs were not in a yaml list format, they were ignored`,
);
}
} catch (e: any) {
if (e.message.includes("Could not read file")) {
console.warn("No PLUGS page found, not loading anything");
return;
}
throw new Error(`Error processing PLUGS: ${e.message}`);
}
console.log("Plug YAML", plugList);
const allCustomPlugNames: string[] = [];
for (const plugUri of plugList) {
const [protocol, ...rest] = plugUri.split(":");
const plugNameMatch = /\/([^\/]+)\.plug\.js$/.exec(plugUri);
if (!plugNameMatch) {
console.error(
"Could not extract plug name from ",
plugUri,
"ignoring...",
);
continue;
}
const plugName = plugNameMatch[1];
const manifests = await events.dispatchEvent(
`get-plug:${protocol}`,
rest.join(":"),
);
if (manifests.length === 0) {
console.error("Could not resolve plug", plugUri);
}
// console.log("Got manifests", plugUri, protocol, manifests);
const workerCode = manifests[0] as string;
allCustomPlugNames.push(plugName);
// console.log("Writing", `_plug/${plugName}.plug.js`, workerCode);
await space.writeAttachment(
`_plug/${plugName}.plug.js`,
new TextEncoder().encode(workerCode),
);
}
const allPlugNames = [...builtinPlugNames, ...allCustomPlugNames];
// And delete extra ones
for (const existingPlug of await space.listPlugs()) {
const plugName = existingPlug.substring(
"_plug/".length,
existingPlug.length - ".plug.js".length,
);
if (!allPlugNames.includes(plugName)) {
await space.deleteAttachment(existingPlug);
}
}
2022-10-14 13:11:33 +00:00
await editor.flashNotification("And... done!");
} 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 updatePlugsCommand();
2022-11-20 09:56:52 +00:00
await editor.flashNotification("Plug added!");
system.reloadPlugs();
2022-11-20 09:56:52 +00:00
}
export async function getPlugHTTPS(url: string): Promise<string> {
2022-10-14 13:11:33 +00:00
const fullUrl = `https:${url}`;
console.log("Now fetching plug code 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 code from ${fullUrl}`);
2022-06-29 13:02:53 +00:00
}
return req.text();
2022-06-29 13:02:53 +00:00
}
export function getPlugGithub(identifier: string): Promise<string> {
2022-10-14 13:11:33 +00:00
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,
): Promise<string> {
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.js`;
2022-07-23 18:57:59 +00:00
return getPlugHTTPS(finalUrl);
2022-09-12 12:50:37 +00:00
}