1
0

Git plug tweaks

This commit is contained in:
Zef Hemel 2022-03-28 08:51:24 +02:00
parent 6dd56e85de
commit 16fa05d4cc
9 changed files with 68 additions and 25 deletions

View File

@ -12,7 +12,7 @@
"watch": "rm -rf .parcel-cache && parcel watch", "watch": "rm -rf .parcel-cache && parcel watch",
"build": "parcel build", "build": "parcel build",
"clean": "rm -rf dist", "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", "server": "nodemon -w dist/server dist/server/server.js pages",
"test": "jest dist/test" "test": "jest dist/test"
}, },

View File

@ -1,27 +1,24 @@
#!/usr/bin/env node #!/usr/bin/env node
import esbuild from "esbuild"; import esbuild from "esbuild";
import { readFile, unlink, writeFile } from "fs/promises"; import { readFile, unlink, watch, writeFile } from "fs/promises";
import path from "path"; import path from "path";
import yargs from "yargs"; import yargs from "yargs";
import { hideBin } from "yargs/helpers"; import { hideBin } from "yargs/helpers";
import { Manifest } from "../types"; import { Manifest } from "../types";
import { watchFile } from "fs";
import YAML from "yaml"; import YAML from "yaml";
async function compile(filePath: string, functionName: string, debug: boolean) { async function compile(filePath: string, functionName: string, debug: boolean) {
let outFile = "out.js"; let outFile = "_out.tmp";
let inFile = filePath; let inFile = filePath;
if (functionName) { if (functionName) {
// Generate a new file importing just this one function and exporting it // Generate a new file importing just this one function and exporting it
inFile = "in.js"; inFile = "_in.js";
await writeFile( await writeFile(
inFile, inFile,
`import {${functionName}} from "./${filePath}"; `import {${functionName}} from "./${filePath}";export default ${functionName};`
export default ${functionName};`
); );
} }
@ -103,14 +100,32 @@ async function run() {
); );
process.exit(1); process.exit(1);
} }
for (const plugManifestPath of args._) {
let manifestPath = plugManifestPath as string; async function buildAll() {
await buildManifest(manifestPath, args.dist, !!args.debug); for (const plugManifestPath of args._) {
if (args.watch) { let manifestPath = plugManifestPath as string;
watchFile(manifestPath, { interval: 1000 }, async () => { try {
console.log("Rebuilding", manifestPath);
await buildManifest(manifestPath, args.dist, !!args.debug); 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();
}
} }
} }
} }

View File

@ -1,6 +1,9 @@
import { Feature, Manifest } from "../types"; import { Feature, Manifest } from "../types";
import { System } from "../system"; import { System } from "../system";
// System events:
// - plug:load (plugName: string)
export type EventHook = { export type EventHook = {
events?: string[]; events?: string[];
}; };
@ -18,7 +21,10 @@ export class EventFeature implements Feature<EventHook> {
plug.manifest!.functions plug.manifest!.functions
)) { )) {
if (functionDef.events && functionDef.events.includes(eventName)) { 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<EventHook> {
apply(system: System<EventHook>): void { apply(system: System<EventHook>): void {
this.system = system; this.system = system;
this.system.on({
plugLoaded: (name) => {
this.dispatchEvent("plug:load", name);
},
});
} }
validateManifest(manifest: Manifest<EventHook>): string[] { validateManifest(manifest: Manifest<EventHook>): string[] {

View File

@ -30,14 +30,27 @@ export class Sandbox {
async load(name: string, code: string): Promise<void> { async load(name: string, code: string): Promise<void> {
await this.worker.ready; 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({ this.worker.postMessage({
type: "load", type: "load",
name: name, name: name,
code: code, code: code,
} as WorkerMessage); } as WorkerMessage);
return new Promise((resolve) => { return new Promise((resolve) => {
this.loadedFunctions.add(name); this.outstandingInits.set(name, () => {
this.outstandingInits.set(name, resolve); this.loadedFunctions.add(name);
this.outstandingInits.delete(name);
resolve();
});
}); });
} }

View File

@ -41,6 +41,6 @@ functions:
welcome: welcome:
path: "./server.ts:welcome" path: "./server.ts:welcome"
events: events:
- load - plug:load
env: server env: server

View File

@ -11,7 +11,10 @@ export function endpointTest(req: EndpointRequest): EndpointResponse {
}; };
} }
export function welcome() { export function welcome(plugName: string) {
console.log("Hello world!"); if (plugName !== "core") {
return;
}
console.log("Hello world!!", plugName);
return "hi"; return "hi";
} }

View File

@ -1,3 +0,0 @@
export default function welcome() {
console.log("Hello world!");
}

View File

@ -27,12 +27,13 @@ export async function snapshotCommand() {
} }
export async function syncCommand() { export async function syncCommand() {
await syscall("editor.flashNotification", "Syncing with git");
await syscall("system.invokeFunctionOnServer", "sync"); await syscall("system.invokeFunctionOnServer", "sync");
await syscall("editor.flashNotification", "Git sync complete!");
} }
export async function sync() { export async function sync() {
console.log("Going to sync with git"); console.log("Going to sync with git");
console.log("First locally committing everything");
await commit(); await commit();
console.log("Then pulling from remote"); console.log("Then pulling from remote");
await syscall("shell.run", "git", ["pull"]); await syscall("shell.run", "git", ["pull"]);

View File

@ -42,6 +42,9 @@ export default (editor: Editor): SysCallMapping => ({
openUrl: async (ctx, url: string) => { openUrl: async (ctx, url: string) => {
window.open(url, "_blank")!.focus(); window.open(url, "_blank")!.focus();
}, },
flashNotification: (ctx, message: string) => {
editor.flashNotification(message);
},
insertAtPos: (ctx, text: string, pos: number) => { insertAtPos: (ctx, text: string, pos: number) => {
editor.editorView!.dispatch({ editor.editorView!.dispatch({
changes: { changes: {