From 16fa05d4cc4e07cab6dbcfed2e0b9e93938606ee Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Mon, 28 Mar 2022 08:51:24 +0200 Subject: [PATCH] Git plug tweaks --- package.json | 2 +- plugos/bin/plugos-bundle.ts | 43 +++++++++++++++++++++++++------------ plugos/feature/event.ts | 13 ++++++++++- plugos/sandbox.ts | 17 +++++++++++++-- plugs/core/core.plug.yaml | 2 +- plugs/core/server.ts | 7 ++++-- plugs/core/welcome.ts | 3 --- plugs/git/git.ts | 3 ++- webapp/syscalls/editor.ts | 3 +++ 9 files changed, 68 insertions(+), 25 deletions(-) delete mode 100644 plugs/core/welcome.ts diff --git a/package.json b/package.json index af5693b..c0b21f4 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "watch": "rm -rf .parcel-cache && parcel watch", "build": "parcel build", "clean": "rm -rf dist", - "plugs": "./plugos/dist/plugos/plugos-bundle.js -w --dist plugs/dist plugs/*/*.plug.yaml", + "plugs": "cd plugs && ../plugos/dist/plugos/plugos-bundle.js -w --dist dist */*.plug.yaml", "server": "nodemon -w dist/server dist/server/server.js pages", "test": "jest dist/test" }, diff --git a/plugos/bin/plugos-bundle.ts b/plugos/bin/plugos-bundle.ts index dc5bd75..775b8d0 100755 --- a/plugos/bin/plugos-bundle.ts +++ b/plugos/bin/plugos-bundle.ts @@ -1,27 +1,24 @@ #!/usr/bin/env node import esbuild from "esbuild"; -import { readFile, unlink, writeFile } from "fs/promises"; +import { readFile, unlink, watch, writeFile } from "fs/promises"; import path from "path"; import yargs from "yargs"; import { hideBin } from "yargs/helpers"; import { Manifest } from "../types"; -import { watchFile } from "fs"; import YAML from "yaml"; async function compile(filePath: string, functionName: string, debug: boolean) { - let outFile = "out.js"; - + let outFile = "_out.tmp"; let inFile = filePath; if (functionName) { // Generate a new file importing just this one function and exporting it - inFile = "in.js"; + inFile = "_in.js"; await writeFile( inFile, - `import {${functionName}} from "./${filePath}"; -export default ${functionName};` + `import {${functionName}} from "./${filePath}";export default ${functionName};` ); } @@ -103,14 +100,32 @@ async function run() { ); process.exit(1); } - for (const plugManifestPath of args._) { - let manifestPath = plugManifestPath as string; - await buildManifest(manifestPath, args.dist, !!args.debug); - if (args.watch) { - watchFile(manifestPath, { interval: 1000 }, async () => { - console.log("Rebuilding", manifestPath); + + async function buildAll() { + for (const plugManifestPath of args._) { + let manifestPath = plugManifestPath as string; + try { await buildManifest(manifestPath, args.dist, !!args.debug); - }); + } catch (e) { + console.error(`Error building ${manifestPath}:`, e); + } + } + } + + await buildAll(); + if (args.watch) { + console.log("Watching for changes..."); + for await (const { eventType, filename } of watch(".", { + recursive: true, + })) { + if ( + filename.endsWith(".plug.yaml") || + filename.endsWith(".ts") || + (filename.endsWith(".js") && !filename.endsWith("_in.js")) + ) { + console.log("Change detected", eventType, filename); + await buildAll(); + } } } } diff --git a/plugos/feature/event.ts b/plugos/feature/event.ts index a8b36a8..63c6990 100644 --- a/plugos/feature/event.ts +++ b/plugos/feature/event.ts @@ -1,6 +1,9 @@ import { Feature, Manifest } from "../types"; import { System } from "../system"; +// System events: +// - plug:load (plugName: string) + export type EventHook = { events?: string[]; }; @@ -18,7 +21,10 @@ export class EventFeature implements Feature { plug.manifest!.functions )) { if (functionDef.events && functionDef.events.includes(eventName)) { - promises.push(plug.invoke(name, [data])); + // Only dispatch functions that can run in this environment + if (plug.canInvoke(name)) { + promises.push(plug.invoke(name, [data])); + } } } } @@ -27,6 +33,11 @@ export class EventFeature implements Feature { apply(system: System): void { this.system = system; + this.system.on({ + plugLoaded: (name) => { + this.dispatchEvent("plug:load", name); + }, + }); } validateManifest(manifest: Manifest): string[] { diff --git a/plugos/sandbox.ts b/plugos/sandbox.ts index 8da85cb..c4b917f 100644 --- a/plugos/sandbox.ts +++ b/plugos/sandbox.ts @@ -30,14 +30,27 @@ export class Sandbox { async load(name: string, code: string): Promise { await this.worker.ready; + let outstandingInit = this.outstandingInits.get(name); + if (outstandingInit) { + // Load already in progress, let's wait for it... + return new Promise((resolve) => { + this.outstandingInits.set(name, () => { + outstandingInit!(); + resolve(); + }); + }); + } this.worker.postMessage({ type: "load", name: name, code: code, } as WorkerMessage); return new Promise((resolve) => { - this.loadedFunctions.add(name); - this.outstandingInits.set(name, resolve); + this.outstandingInits.set(name, () => { + this.loadedFunctions.add(name); + this.outstandingInits.delete(name); + resolve(); + }); }); } diff --git a/plugs/core/core.plug.yaml b/plugs/core/core.plug.yaml index 34c73a9..a457894 100644 --- a/plugs/core/core.plug.yaml +++ b/plugs/core/core.plug.yaml @@ -41,6 +41,6 @@ functions: welcome: path: "./server.ts:welcome" events: - - load + - plug:load env: server diff --git a/plugs/core/server.ts b/plugs/core/server.ts index 1a593cd..229b9dd 100644 --- a/plugs/core/server.ts +++ b/plugs/core/server.ts @@ -11,7 +11,10 @@ export function endpointTest(req: EndpointRequest): EndpointResponse { }; } -export function welcome() { - console.log("Hello world!"); +export function welcome(plugName: string) { + if (plugName !== "core") { + return; + } + console.log("Hello world!!", plugName); return "hi"; } diff --git a/plugs/core/welcome.ts b/plugs/core/welcome.ts deleted file mode 100644 index 6ce3811..0000000 --- a/plugs/core/welcome.ts +++ /dev/null @@ -1,3 +0,0 @@ -export default function welcome() { - console.log("Hello world!"); -} diff --git a/plugs/git/git.ts b/plugs/git/git.ts index bd50452..79b353f 100644 --- a/plugs/git/git.ts +++ b/plugs/git/git.ts @@ -27,12 +27,13 @@ export async function snapshotCommand() { } export async function syncCommand() { + await syscall("editor.flashNotification", "Syncing with git"); await syscall("system.invokeFunctionOnServer", "sync"); + await syscall("editor.flashNotification", "Git sync complete!"); } export async function sync() { console.log("Going to sync with git"); - console.log("First locally committing everything"); await commit(); console.log("Then pulling from remote"); await syscall("shell.run", "git", ["pull"]); diff --git a/webapp/syscalls/editor.ts b/webapp/syscalls/editor.ts index 929da5d..5c51fd6 100644 --- a/webapp/syscalls/editor.ts +++ b/webapp/syscalls/editor.ts @@ -42,6 +42,9 @@ export default (editor: Editor): SysCallMapping => ({ openUrl: async (ctx, url: string) => { window.open(url, "_blank")!.focus(); }, + flashNotification: (ctx, message: string) => { + editor.flashNotification(message); + }, insertAtPos: (ctx, text: string, pos: number) => { editor.editorView!.dispatch({ changes: {