pull/3/head
Zef Hemel 2022-04-26 20:31:31 +02:00
parent 76636dd9b1
commit 0182a587e4
10 changed files with 60 additions and 32 deletions

View File

@ -1,3 +1,4 @@
{
"editor.formatOnSave": true
}
"editor.formatOnSave": true,
"typescriptHero.imports.stringQuoteStyle": "\""
}

View File

@ -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;

View File

@ -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) {}

View File

@ -9,5 +9,5 @@ export async function invokeFunction(
}
export async function reloadPlugs() {
return syscall("system.reloadPlugs");
syscall("system.reloadPlugs");
}

View File

@ -108,7 +108,7 @@ export class System<HookT> extends EventEmitter<SystemEvents<HookT>> {
}
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`);

View File

@ -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

View File

@ -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<Manifest> {
let { text } = await readPage(pageName);
console.log("Compiling", pageName);
return compileDefinition(text);
}

View File

@ -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<Manifest> => {
@ -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)

View File

@ -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", {

View File

@ -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 {