SilverBullet pivot to become an offline-first PWA (#403)
This commit is contained in:
+71
-62
@@ -2,6 +2,7 @@ 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";
|
||||
|
||||
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";
|
||||
@@ -10,9 +11,69 @@ export async function updatePlugsCommand() {
|
||||
await editor.save();
|
||||
await editor.flashNotification("Updating plugs...");
|
||||
try {
|
||||
await system.invokeFunction("server", "updatePlugs");
|
||||
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`,
|
||||
"utf8",
|
||||
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);
|
||||
}
|
||||
}
|
||||
await editor.flashNotification("And... done!");
|
||||
system.reloadPlugs();
|
||||
} catch (e: any) {
|
||||
editor.flashNotification("Error updating plugs: " + e.message, "error");
|
||||
}
|
||||
@@ -45,74 +106,22 @@ export async function addPlugCommand() {
|
||||
"\n```",
|
||||
);
|
||||
await editor.navigate("PLUGS");
|
||||
await system.invokeFunction("server", "updatePlugs");
|
||||
await updatePlugsCommand();
|
||||
await editor.flashNotification("Plug added!");
|
||||
system.reloadPlugs();
|
||||
}
|
||||
|
||||
export async function updatePlugs() {
|
||||
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 allPlugNames: string[] = [];
|
||||
for (const plugUri of plugList) {
|
||||
const [protocol, ...rest] = plugUri.split(":");
|
||||
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 manifest = manifests[0];
|
||||
allPlugNames.push(manifest.name);
|
||||
// console.log("Writing", `_plug/${manifest.name}`);
|
||||
await space.writeAttachment(
|
||||
`_plug/${manifest.name}.plug.json`,
|
||||
"utf8",
|
||||
JSON.stringify(manifest),
|
||||
);
|
||||
}
|
||||
|
||||
// And delete extra ones
|
||||
for (const existingPlug of await space.listPlugs()) {
|
||||
const plugName = existingPlug.substring(
|
||||
"_plug/".length,
|
||||
existingPlug.length - ".plug.json".length,
|
||||
);
|
||||
if (!allPlugNames.includes(plugName)) {
|
||||
await space.deleteAttachment(existingPlug);
|
||||
}
|
||||
}
|
||||
system.reloadPlugs();
|
||||
}
|
||||
|
||||
export async function getPlugHTTPS(url: string): Promise<Manifest> {
|
||||
export async function getPlugHTTPS(url: string): Promise<string> {
|
||||
const fullUrl = `https:${url}`;
|
||||
console.log("Now fetching plug manifest from", fullUrl);
|
||||
console.log("Now fetching plug code from", fullUrl);
|
||||
const req = await fetch(fullUrl);
|
||||
if (req.status !== 200) {
|
||||
throw new Error(`Could not fetch plug manifest from ${fullUrl}`);
|
||||
throw new Error(`Could not fetch plug code from ${fullUrl}`);
|
||||
}
|
||||
return req.json();
|
||||
return req.text();
|
||||
}
|
||||
|
||||
export function getPlugGithub(identifier: string): Promise<Manifest> {
|
||||
export function getPlugGithub(identifier: string): Promise<string> {
|
||||
const [owner, repo, path] = identifier.split("/");
|
||||
let [repoClean, branch] = repo.split("@");
|
||||
if (!branch) {
|
||||
@@ -125,7 +134,7 @@ export function getPlugGithub(identifier: string): Promise<Manifest> {
|
||||
|
||||
export async function getPlugGithubRelease(
|
||||
identifier: string,
|
||||
): Promise<Manifest> {
|
||||
): Promise<string> {
|
||||
let [owner, repo, version] = identifier.split("/");
|
||||
if (!version || version === "latest") {
|
||||
console.log("fetching the latest version");
|
||||
@@ -141,6 +150,6 @@ export async function getPlugGithubRelease(
|
||||
version = result.name;
|
||||
}
|
||||
const finalUrl =
|
||||
`//github.com/${owner}/${repo}/releases/download/${version}/${repo}.plug.json`;
|
||||
`//github.com/${owner}/${repo}/releases/download/${version}/${repo}.plug.js`;
|
||||
return getPlugHTTPS(finalUrl);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user