diff --git a/.vscode/settings.json b/.vscode/settings.json index 23fd35f..2cd23e5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,4 @@ { - "editor.formatOnSave": true -} \ No newline at end of file + "editor.formatOnSave": true, + "typescriptHero.imports.stringQuoteStyle": "\"" +} diff --git a/desktop/src/index.ts b/desktop/src/index.ts index a412a3c..1c3eb29 100644 --- a/desktop/src/index.ts +++ b/desktop/src/index.ts @@ -1,6 +1,6 @@ import { app, BrowserWindow, dialog, Menu } from "electron"; const path = require("path"); -import { ExpressServer } from "@silverbulletmd/server/api_server"; +import { ExpressServer } from "@silverbulletmd/server/express_server"; import * as fs from "fs"; let mainWindow: BrowserWindow | undefined; diff --git a/packages/common/spaces/evented_space_primitives.ts b/packages/common/spaces/evented_space_primitives.ts index 79278ce..b8ea3e9 100644 --- a/packages/common/spaces/evented_space_primitives.ts +++ b/packages/common/spaces/evented_space_primitives.ts @@ -1,8 +1,9 @@ -import { SpacePrimitives } from "./space_primitives"; import { EventHook } from "@plugos/plugos/hooks/event"; -import { PageMeta } from "../types"; import { Plug } from "@plugos/plugos/plug"; + +import { PageMeta } from "../types"; import { plugPrefix, trashPrefix } from "./constants"; +import { SpacePrimitives } from "./space_primitives"; export class EventedSpacePrimitives implements SpacePrimitives { constructor(private wrapped: SpacePrimitives, private eventHook: EventHook) {} diff --git a/packages/plugos-silverbullet-syscall/system.ts b/packages/plugos-silverbullet-syscall/system.ts index af2220e..6072fa6 100644 --- a/packages/plugos-silverbullet-syscall/system.ts +++ b/packages/plugos-silverbullet-syscall/system.ts @@ -9,5 +9,5 @@ export async function invokeFunction( } export async function reloadPlugs() { - return syscall("system.reloadPlugs"); + syscall("system.reloadPlugs"); } diff --git a/packages/plugos/system.ts b/packages/plugos/system.ts index 6f9ad63..57ea72c 100644 --- a/packages/plugos/system.ts +++ b/packages/plugos/system.ts @@ -108,7 +108,7 @@ export class System extends EventEmitter> { } async unload(name: string) { - console.log("Unloading", name); + // console.log("Unloading", name); const plug = this.plugs.get(name); if (!plug) { throw Error(`Plug ${name} not found`); diff --git a/packages/plugs/plugmd/plugmd.plug.yaml b/packages/plugs/plugmanager/plugmanager.plug.yaml similarity index 59% rename from packages/plugs/plugmd/plugmd.plug.yaml rename to packages/plugs/plugmanager/plugmanager.plug.yaml index 2440bc9..204dfa0 100644 --- a/packages/plugs/plugmd/plugmd.plug.yaml +++ b/packages/plugs/plugmanager/plugmanager.plug.yaml @@ -1,25 +1,25 @@ -name: plugmd +name: plugmanager functions: updatePlugsCommand: - path: ./plugmd.ts:updatePlugsCommand + path: ./plugmanager.ts:updatePlugsCommand command: name: "Plugs: Update" key: "Ctrl-Shift-p" mac: "Cmd-Shift-p" updatePlugs: - path: ./plugmd.ts:updatePlugs + path: ./plugmanager.ts:updatePlugs env: server compile: - path: "./plugmd.ts:compileCommand" + path: "./plugmanager.ts:compileCommand" command: name: "Plug: Compile" mac: "Cmd-Shift-c" key: "Ctrl-Shift-c" compileJS: - path: "./plugmd.ts:compileJS" + path: "./plugmanager.ts:compileJS" env: server getPlugPlugMd: - path: "./plugmd.ts:getPlugPlugMd" + path: "./plugmanager.ts:getPlugPlugMd" events: - get-plug:plugmd diff --git a/packages/plugs/plugmd/plugmd.ts b/packages/plugs/plugmanager/plugmanager.ts similarity index 97% rename from packages/plugs/plugmd/plugmd.ts rename to packages/plugs/plugmanager/plugmanager.ts index 633ef41..1527d96 100644 --- a/packages/plugs/plugmd/plugmd.ts +++ b/packages/plugs/plugmanager/plugmanager.ts @@ -1,12 +1,10 @@ import { dispatch } from "@plugos/plugos-syscall/event"; -import type { Manifest } from "@silverbulletmd/common/manifest"; import { addParentPointers, collectNodesOfType, findNodeOfType, } from "@silverbulletmd/common/tree"; import { - getCurrentPage, getText, hideBhs, showBhs, @@ -23,8 +21,11 @@ import { reloadPlugs, } from "@silverbulletmd/plugos-silverbullet-syscall/system"; import YAML from "yaml"; + import { extractMeta } from "../query/data"; +import type { Manifest } from "@silverbulletmd/common/manifest"; + export async function compileCommand() { let text = await getText(); try { @@ -139,7 +140,7 @@ export async function updatePlugs() { } let plugYaml = codeTextNode.children![0].text; let plugList = YAML.parse(plugYaml!); - console.log("Plug YAML", plugList); + // console.log("Plug YAML", plugList); let allPlugNames: string[] = []; for (let plugUri of plugList) { let [protocol, ...rest] = plugUri.split(":"); @@ -164,11 +165,11 @@ export async function updatePlugs() { await deletePage(`_plug/${existingPlug}`); } } - // Important not to await! - reloadPlugs(); + await reloadPlugs(); } export async function getPlugPlugMd(pageName: string): Promise { let { text } = await readPage(pageName); + console.log("Compiling", pageName); return compileDefinition(text); } diff --git a/packages/server/api_server.ts b/packages/server/express_server.ts similarity index 89% rename from packages/server/api_server.ts rename to packages/server/express_server.ts index a24976e..af54c3d 100644 --- a/packages/server/api_server.ts +++ b/packages/server/express_server.ts @@ -1,7 +1,7 @@ import express, { Express } from "express"; import { Manifest, SilverBulletHooks } from "@silverbulletmd/common/manifest"; import { EndpointHook } from "@plugos/plugos/hooks/endpoint"; -import { readFile } from "fs/promises"; +import { readdir, readFile } from "fs/promises"; import { System } from "@plugos/plugos/system"; import cors from "cors"; import { DiskSpacePrimitives } from "@silverbulletmd/common/spaces/disk_space_primitives"; @@ -17,7 +17,6 @@ import { NodeCronHook } from "@plugos/plugos/hooks/node_cron"; import { markdownSyscalls } from "@silverbulletmd/common/syscalls/markdown"; import { EventedSpacePrimitives } from "@silverbulletmd/common/spaces/evented_space_primitives"; import { Space } from "@silverbulletmd/common/spaces/space"; -import { safeRun, throttle } from "@silverbulletmd/common/util"; import { createSandbox } from "@plugos/plugos/environments/node_sandbox"; import { jwtSyscalls } from "@plugos/plugos/syscalls/jwt"; import buildMarkdown from "@silverbulletmd/web/parser"; @@ -25,6 +24,7 @@ import { loadMarkdownExtensions } from "@silverbulletmd/web/markdown_ext"; import http, { Server } from "http"; import { esbuildSyscalls } from "@plugos/plugos/syscalls/esbuild"; import { systemSyscalls } from "./syscalls/system"; +import { plugPrefix } from "@silverbulletmd/common/spaces/constants"; export class ExpressServer { app: Express; @@ -82,10 +82,6 @@ export class ExpressServer { this.system.registerSyscalls([], jwtSyscalls()); this.system.addHook(new EndpointHook(this.app, "/_/")); - let throttledRebuildMdExtensions = throttle(() => { - this.rebuildMdExtensions(); - }, 100); - this.eventHook.addLocalListener( "get-plug:builtin", async (plugName: string): Promise => { @@ -115,11 +111,42 @@ export class ExpressServer { ); } + private async bootstrapBuiltinPlugs() { + let allPlugFiles = await readdir(this.builtinPlugDir); + let pluginNames = []; + for (let file of allPlugFiles) { + if (file.endsWith(".plug.json")) { + let manifestJson = await readFile( + path.join(this.builtinPlugDir, file), + "utf8" + ); + let manifest: Manifest = JSON.parse(manifestJson); + pluginNames.push(manifest.name); + await this.space.writePage( + `${plugPrefix}${manifest.name}`, + manifestJson + ); + } + } + + await this.space.writePage( + "PLUGS", + "This file lists all plugs that SilverBullet will load. Run the `Plugs: Update` command to update and reload this list of plugs.\n\n```yaml\n- " + + pluginNames.map((name) => `builtin:${name}`).join("\n- ") + + "\n```" + ); + } + async reloadPlugs() { await this.space.updatePageList(); + let allPlugs = this.space.listPlugs(); + if (allPlugs.size === 0) { + await this.bootstrapBuiltinPlugs(); + allPlugs = this.space.listPlugs(); + } await this.system.unloadAll(); console.log("Reloading plugs"); - for (let pageInfo of this.space.listPlugs()) { + for (let pageInfo of allPlugs) { let { text } = await this.space.readPage(pageInfo.name); await this.system.load(JSON.parse(text), (p) => createSandbox(p, this.preloadedModules) diff --git a/packages/server/server.ts b/packages/server/server.ts index 479f37c..3839af9 100755 --- a/packages/server/server.ts +++ b/packages/server/server.ts @@ -1,13 +1,11 @@ #!/usr/bin/env node - -import yargs from "yargs"; -import { hideBin } from "yargs/helpers"; -import { ExpressServer } from "./api_server"; import { nodeModulesDir } from "@plugos/plugos/environments/node_sandbox"; import { preloadModules } from "@silverbulletmd/common/preload_modules"; -import path from "path"; -import { realpath } from "fs/promises"; import { realpathSync } from "fs"; +import yargs from "yargs"; +import { hideBin } from "yargs/helpers"; + +import { ExpressServer } from "./express_server"; let args = yargs(hideBin(process.argv)) .option("port", { diff --git a/packages/server/syscalls/system.ts b/packages/server/syscalls/system.ts index b72bcb4..fbcf90b 100644 --- a/packages/server/syscalls/system.ts +++ b/packages/server/syscalls/system.ts @@ -1,5 +1,5 @@ import { SysCallMapping } from "@plugos/plugos/system"; -import type { ExpressServer } from "../api_server"; +import type { ExpressServer } from "../express_server"; export function systemSyscalls(expressServer: ExpressServer): SysCallMapping { return {