From d0875165dfc70c35de6ae948662fb6d22e287b33 Mon Sep 17 00:00:00 2001 From: "Marek S. Lukasiewicz" Date: Wed, 16 Oct 2024 19:55:48 +0200 Subject: [PATCH] WIP: Get plug URIs from Space Config #1042 --- plugs/editor/editor.plug.yaml | 4 ++ plugs/plug-manager/plugmanager.test.ts | 17 +++++ plugs/plug-manager/plugmanager.ts | 89 ++++++++++++++++++-------- 3 files changed, 85 insertions(+), 25 deletions(-) create mode 100644 plugs/plug-manager/plugmanager.test.ts diff --git a/plugs/editor/editor.plug.yaml b/plugs/editor/editor.plug.yaml index 794e89be..3a1d2ec1 100644 --- a/plugs/editor/editor.plug.yaml +++ b/plugs/editor/editor.plug.yaml @@ -63,6 +63,10 @@ config: defaultLinkStyle: type: string nullable: true + plugs: + type: array + items: + type: string functions: setEditorMode: path: "./editor.ts:setEditorMode" diff --git a/plugs/plug-manager/plugmanager.test.ts b/plugs/plug-manager/plugmanager.test.ts new file mode 100644 index 00000000..e1589427 --- /dev/null +++ b/plugs/plug-manager/plugmanager.test.ts @@ -0,0 +1,17 @@ +import { assertEquals } from "@std/assert"; +import { insertPlugIntoPage } from "./plugmanager.ts"; + +/** Convenience function simulating repeatedly calling `editor.replaceRange` */ +function replaceRanges( + pageText: string, + ranges: Array<{ from: number; to: number; text: string }>, +): string { + let result = pageText; + for (const { from, to, text } of ranges) { + result = result.substring(0, from) + text + result.substring(to); + } + return result; +} + +Deno.test("Append plug to page", () => { +}); diff --git a/plugs/plug-manager/plugmanager.ts b/plugs/plug-manager/plugmanager.ts index 071b1eeb..c28ee365 100644 --- a/plugs/plug-manager/plugmanager.ts +++ b/plugs/plug-manager/plugmanager.ts @@ -14,30 +14,52 @@ export async function updatePlugsCommand() { await editor.save(); await editor.flashNotification("Updating plugs..."); try { - let plugList: string[] = []; - try { - const plugListRead: any[] = await readYamlPage("PLUGS"); - if (!Array.isArray(plugListRead)) { - await editor.flashNotification( - "PLUGS YAML does not contain a plug list, not loading anything", + const plugList: string[] = []; + const configPlugs: any[] = await system.getSpaceConfig("plugs", []); + if (!Array.isArray(configPlugs)) { + throw new Error("Expected 'plugs' in Space Config to be an array"); + } + const stringPlugs = configPlugs.filter((plug) => typeof plug === "string"); + if (stringPlugs.length !== configPlugs.length) { + throw new Error( + `${ + configPlugs.length - stringPlugs.length + } plugs in Space Config aren't set as strings`, + ); + } + plugList.push(...stringPlugs); + if (await space.fileExists("PLUGS.md")) { + // This is not primary mode of managing plugs anymore, only here for backwards compatibility. + try { + const pagePlugs: any[] = await readYamlPage("PLUGS"); + if (Array.isArray(pagePlugs)) { + // It's possible that the user is using it for something else, but if it has yaml with an array, assume it's plugs + const pageStringPlugs = pagePlugs.filter((plug) => + typeof plug === "string" + ); + if (pageStringPlugs.length !== pagePlugs.length) { + throw new Error( + `${ + pagePlugs.length - pageStringPlugs.length + } plugs from PLUG page were not in a yaml list format`, + ); + } + plugList.push(...pageStringPlugs); + if (pageStringPlugs.length > 0) { + editor.flashNotification( + `${pageStringPlugs.length} plugs in PLUGS page can be moved to Space Config for better editor support`, + ); + } + } + } catch (e: any) { + editor.flashNotification( + `Error processing PLUGS page: ${e.message}`, "error", ); return; } - 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("Not found")) { - console.warn("No PLUGS page found, not loading anything"); - return; - } - throw new Error(`Error processing PLUGS: ${e.message}`); } - console.log("Plug YAML", plugList); + console.log("Found Plug URIs:", plugList); const allCustomPlugNames: string[] = []; for (const plugUri of plugList) { const [protocol, ...rest] = plugUri.split(":"); @@ -100,13 +122,13 @@ export async function updatePlugsCommand() { } export async function addPlugCommand() { - let name = await editor.prompt("Plug URI:"); - if (!name) { + let uri = await editor.prompt("Plug URI:"); + if (!uri) { return; } // Support people copy & pasting the YAML version - if (name.startsWith("-")) { - name = name.replace(/^\-\s*/, ""); + if (uri.startsWith("-")) { + uri = uri.replace(/^\-\s*/, ""); } let plugList: string[] = []; try { @@ -114,11 +136,11 @@ export async function addPlugCommand() { } catch (e: any) { console.error("ERROR", e); } - if (plugList.includes(name)) { + if (plugList.includes(uri)) { await editor.flashNotification("Plug already installed", "error"); return; } - plugList.push(name); + plugList.push(uri); // await writeYamlPage("PLUGS", plugList, plugsPrelude); await space.writePage( "PLUGS", @@ -131,6 +153,23 @@ export async function addPlugCommand() { system.reloadPlugs(); } +/** Add the plug to the end of the plugs list in Space Config inside the page content + * Returns an array for `editor.replaceRange` syscalls. + * + * Appends the `space-config` block if needed. + * Appends the `plugs` key on root level if needed. + * Rewrites the `yaml` block if it's on PLUGS page for new syntax. + * + * It's exported only to allow testing. + */ +export function insertPlugIntoPage( + uri: string, + pageContent: string, + isPlugsPage: boolean = false, +): Array<{ from: number; to: number; text: string }> { + return []; +} + export async function getPlugHTTPS(url: string): Promise { const fullUrl = `https:${url}`; console.log("Now fetching plug code from", fullUrl);