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
+29 -14
View File
@@ -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();
}
}
}
}
+12 -1
View File
@@ -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<EventHook> {
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<EventHook> {
apply(system: System<EventHook>): void {
this.system = system;
this.system.on({
plugLoaded: (name) => {
this.dispatchEvent("plug:load", name);
},
});
}
validateManifest(manifest: Manifest<EventHook>): string[] {
+15 -2
View File
@@ -30,14 +30,27 @@ export class Sandbox {
async load(name: string, code: string): Promise<void> {
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();
});
});
}