1
0
silverbullet/plugs/core/plugmanager.ts

122 lines
3.6 KiB
TypeScript
Raw Normal View History

import { dispatch } from "../../syscall/plugos-syscall/event.ts";
import { Manifest } from "../../common/manifest.ts";
2022-06-29 13:02:53 +00:00
import {
flashNotification,
save,
} from "../../syscall/silverbullet-syscall/editor.ts";
2022-06-28 12:14:15 +00:00
import {
2022-09-12 12:50:37 +00:00
deleteAttachment,
listPlugs,
writeAttachment,
} from "../../syscall/silverbullet-syscall/space.ts";
2022-06-28 12:14:15 +00:00
import {
invokeFunction,
reloadPlugs,
} from "../../syscall/silverbullet-syscall/system.ts";
import { readYamlPage } from "../lib/yaml_page.ts";
2022-06-28 12:14:15 +00:00
export async function updatePlugsCommand() {
2022-06-29 13:02:53 +00:00
await save();
2022-06-28 12:14:15 +00:00
flashNotification("Updating plugs...");
try {
await invokeFunction("server", "updatePlugs");
flashNotification("And... done!");
await reloadPlugs();
} catch (e: any) {
flashNotification("Error updating plugs: " + e.message, "error");
}
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);
let allPlugNames: string[] = [];
for (let plugUri of plugList) {
let [protocol, ...rest] = plugUri.split(":");
let manifests = await dispatch(`get-plug:${protocol}`, rest.join(":"));
if (manifests.length === 0) {
console.error("Could not resolve plug", plugUri);
}
// console.log("Got manifests", plugUri, protocol, manifests);
let manifest = manifests[0];
allPlugNames.push(manifest.name);
// console.log("Writing", `_plug/${manifest.name}`);
2022-09-12 12:50:37 +00:00
await writeAttachment(
`_plug/${manifest.name}.plug.json`,
"string",
2022-10-12 09:47:13 +00:00
JSON.stringify(manifest),
2022-06-28 12:14:15 +00:00
);
}
// And delete extra ones
for (let existingPlug of await listPlugs()) {
2022-09-12 12:50:37 +00:00
let plugName = existingPlug.substring(
"_plug/".length,
2022-10-12 09:47:13 +00:00
existingPlug.length - ".plug.json".length,
2022-09-12 12:50:37 +00:00
);
console.log("Considering", plugName);
if (!allPlugNames.includes(plugName)) {
console.log("Removing plug", plugName);
await deleteAttachment(existingPlug);
2022-06-28 12:14:15 +00:00
}
}
await reloadPlugs();
}
2022-06-29 13:02:53 +00:00
export async function getPlugHTTPS(url: string): Promise<Manifest> {
let fullUrl = `https:${url}`;
console.log("Now fetching plug manifest from", fullUrl);
let req = await fetch(fullUrl);
if (req.status !== 200) {
throw new Error(`Could not fetch plug manifest from ${fullUrl}`);
}
let json = await req.json();
return json;
}
export async function getPlugGithub(identifier: string): Promise<Manifest> {
let [owner, repo, path] = identifier.split("/");
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
}