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";
|
2022-07-15 09:17:02 +00:00
|
|
|
|
2022-10-14 13:11:33 +00:00
|
|
|
import { readYamlPage } from "$sb/lib/yaml_page.ts";
|
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...");
|
2022-07-15 09:17:02 +00:00
|
|
|
try {
|
2022-10-14 13:11:33 +00:00
|
|
|
await system.invokeFunction("server", "updatePlugs");
|
|
|
|
await editor.flashNotification("And... done!");
|
|
|
|
system.reloadPlugs();
|
2022-07-15 09:17:02 +00:00
|
|
|
} catch (e: any) {
|
2022-10-14 13:11:33 +00:00
|
|
|
editor.flashNotification("Error updating plugs: " + e.message, "error");
|
2022-07-15 09:17:02 +00:00
|
|
|
}
|
2022-06-28 12:14:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function updatePlugs() {
|
2022-07-15 09:17:02 +00:00
|
|
|
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
|
|
|
}
|
2022-07-15 09:17:02 +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`,
|
|
|
|
"string",
|
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
|
|
|
);
|
|
|
|
console.log("Considering", plugName);
|
|
|
|
if (!allPlugNames.includes(plugName)) {
|
|
|
|
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-14 13:11:33 +00:00
|
|
|
await 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
|
|
|
}
|