1
0
silverbullet/packages/plugos/plug_loader.ts

67 lines
1.9 KiB
TypeScript
Raw Normal View History

2022-03-28 13:25:05 +00:00
import fs from "fs/promises";
import watch from "node-watch";
2022-03-21 14:21:34 +00:00
import path from "path";
import { createSandbox } from "./environments/node_sandbox";
2022-03-23 14:41:12 +00:00
import { System } from "./system";
2022-04-26 17:04:36 +00:00
import { Manifest } from "./types";
2022-03-21 14:21:34 +00:00
export class DiskPlugLoader<HookT> {
private system: System<HookT>;
private plugPath: string;
constructor(system: System<HookT>, plugPath: string) {
this.system = system;
this.plugPath = plugPath;
}
watcher() {
2022-03-28 13:25:05 +00:00
watch(this.plugPath, (eventType, localPath) => {
if (!localPath.endsWith(".plug.json")) {
return;
}
Promise.resolve()
.then(async () => {
2022-03-21 14:21:34 +00:00
try {
// let localPath = path.join(this.plugPath, filename);
2022-04-26 17:04:36 +00:00
console.log("Change detected for", localPath);
try {
await fs.stat(localPath);
} catch (e) {
// Likely removed
2022-04-26 17:04:36 +00:00
console.log("Plug removed, TODO: Unload");
return;
}
const plugDef = await this.loadPlugFromFile(localPath);
2022-03-21 14:21:34 +00:00
} catch (e) {
console.log("Ignoring something FYI", e);
// ignore, error handled by loadPlug
2022-03-21 14:21:34 +00:00
}
})
.catch(console.error);
2022-03-21 14:21:34 +00:00
});
}
private async loadPlugFromFile(localPath: string) {
const plug = await fs.readFile(localPath, "utf8");
try {
2022-04-26 17:04:36 +00:00
const plugDef: Manifest<HookT> = JSON.parse(plug);
console.log("Now loading plug", plugDef.name);
await this.system.load(plugDef, createSandbox);
2022-03-21 14:21:34 +00:00
return plugDef;
} catch (e) {
console.error("Could not parse plugin file", e);
throw e;
}
}
async loadPlugs() {
for (let filename of await fs.readdir(this.plugPath)) {
if (filename.endsWith(".plug.json")) {
let localPath = path.join(this.plugPath, filename);
await this.loadPlugFromFile(localPath);
}
}
}
}