Pre plug format change

This commit is contained in:
Zef Hemel
2022-03-27 09:55:29 +02:00
parent 08e6c3bad8
commit a382ab3baa
13 changed files with 222 additions and 110 deletions
+39 -7
View File
@@ -7,6 +7,7 @@ import path from "path";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { Manifest } from "../types";
import { watchFile } from "fs";
async function compile(filePath: string, functionName: string, debug: boolean) {
let outFile = "out.js";
@@ -63,18 +64,49 @@ async function bundle(manifestPath: string, sourceMaps: boolean) {
}
return manifest;
}
async function buildManifest(
manifestPath: string,
distPath: string,
debug: boolean
) {
let generatedManifest = await bundle(manifestPath, debug);
const outPath = path.join(distPath, path.basename(manifestPath));
console.log("Emitting bundle to", outPath);
await writeFile(outPath, JSON.stringify(generatedManifest, null, 2));
return { generatedManifest, outPath };
}
async function run() {
let args = await yargs(hideBin(process.argv))
let args = yargs(hideBin(process.argv))
.option("debug", {
type: "boolean",
})
.option("watch", {
type: "boolean",
alias: "w",
})
.option("dist", {
type: "string",
default: ".",
})
.parse();
let generatedManifest = await bundle(args._[0] as string, !!args.debug);
await writeFile(
args._[1] as string,
JSON.stringify(generatedManifest, null, 2)
);
if (args._.length === 0) {
console.log(
"Usage: plugbox-bundle [--debug] [--dist <path>] <manifest.plug.json> <manifest2.plug.json> ..."
);
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);
await buildManifest(manifestPath, args.dist, !!args.debug);
});
}
}
}
run().catch((e) => {
+66
View File
@@ -0,0 +1,66 @@
#!/usr/bin/env node
import express from "express";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { DiskPlugLoader } from "../plug_loader";
import { CronHook, NodeCronFeature } from "../feature/node_cron";
import shellSyscalls from "../syscall/shell.node";
import { System } from "../system";
import { EndpointFeature, EndpointHook } from "../feature/endpoint";
import { safeRun } from "../util";
import knex from "knex";
import {
ensureTable,
storeReadSyscalls,
storeWriteSyscalls,
} from "../syscall/store.knex_node";
import { fetchSyscalls } from "../syscall/fetch.node";
let args = yargs(hideBin(process.argv))
.option("port", {
type: "number",
default: 1337,
})
.parse();
if (!args._.length) {
console.error("Usage: plugbox-server <path-to-plugs>");
process.exit(1);
}
const plugPath = args._[0] as string;
const app = express();
type ServerHook = EndpointHook & CronHook;
const system = new System<ServerHook>("server");
safeRun(async () => {
const db = knex({
client: "better-sqlite3",
connection: {
filename: "plugbox.db",
},
useNullAsDefault: true,
});
await ensureTable(db, "item");
let plugLoader = new DiskPlugLoader(system, plugPath);
await plugLoader.loadPlugs();
plugLoader.watcher();
system.addFeature(new NodeCronFeature());
system.addFeature(new EndpointFeature(app, ""));
system.registerSyscalls("shell", [], shellSyscalls("."));
system.registerSyscalls("fetch", [], fetchSyscalls());
system.registerSyscalls(
"store",
[],
storeWriteSyscalls(db, "item"),
storeReadSyscalls(db, "item")
);
app.listen(args.port, () => {
console.log(`Plugbox server listening on port ${args.port}`);
});
});