This commit is contained in:
Zef Hemel
2022-03-04 12:09:25 +01:00
parent a97bff60d9
commit e94d4d3253
14 changed files with 114 additions and 343 deletions
+7 -8
View File
@@ -1,12 +1,13 @@
#!/usr/bin/env node
import esbuild from "esbuild";
import { readFile, unlink, writeFile } from "fs/promises";
import path from "path";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { Manifest } from "../../webapp/src/plugins/types";
async function compile(filePath: string, sourceMap: boolean) {
async function compile(filePath, sourceMap) {
let tempFile = "out.js";
let js = await esbuild.build({
entryPoints: [filePath],
@@ -25,11 +26,9 @@ async function compile(filePath: string, sourceMap: boolean) {
return jsCode;
}
async function bundle(manifestPath: string, sourceMaps: boolean) {
async function bundle(manifestPath, sourceMaps) {
const rootPath = path.dirname(manifestPath);
const manifest = JSON.parse(
(await readFile(manifestPath)).toString()
) as Manifest;
const manifest = JSON.parse((await readFile(manifestPath)).toString());
for (let [name, def] of Object.entries(manifest.functions)) {
let jsFunctionName = def.functionName,
@@ -53,8 +52,8 @@ async function run() {
})
.parse();
let generatedManifest = await bundle(args._[0] as string, !!args.debug);
writeFile(args._[1] as string, JSON.stringify(generatedManifest, null, 2));
let generatedManifest = await bundle(args._[0], !!args.debug);
writeFile(args._[1], JSON.stringify(generatedManifest, null, 2));
}
run().catch((e) => {
+5 -5
View File
@@ -2,10 +2,11 @@
"name": "plugbox",
"version": "1.0.0",
"type": "module",
"source": "src/bundle.ts",
"main": "dist/bundle.js",
"license": "MIT",
"bin": {
"plugbox-bundle": "./bin/plugbox-bundle.mjs"
},
"scripts": {
"build": "parcel build",
"check": "tsc --noEmit"
},
"dependencies": {
@@ -17,7 +18,6 @@
},
"devDependencies": {
"@types/node": "^17.0.21",
"@types/yargs": "^17.0.9",
"parcel": "^2.3.2"
"@types/yargs": "^17.0.9"
}
}
+16 -16
View File
@@ -11,9 +11,9 @@ export class FunctionWorker {
private initCallback: any;
private invokeResolve?: (result?: any) => void;
private invokeReject?: (reason?: any) => void;
private plugin: Plugin;
private plugin: Plugin<any>;
constructor(plugin: Plugin, pathPrefix: string, name: string) {
constructor(plugin: Plugin<any>, pathPrefix: string, name: string) {
let worker = window.Worker;
this.worker = new worker("/function_worker.js");
@@ -76,25 +76,25 @@ export class FunctionWorker {
}
}
export interface PluginLoader {
load(name: string, manifest: Manifest): Promise<void>;
export interface PluginLoader<HookT> {
load(name: string, manifest: Manifest<HookT>): Promise<void>;
}
export class Plugin {
export class Plugin<HookT> {
pathPrefix: string;
system: System;
system: System<HookT>;
private runningFunctions: Map<string, FunctionWorker>;
public manifest?: Manifest;
public manifest?: Manifest<HookT>;
private name: string;
constructor(system: System, pathPrefix: string, name: string) {
constructor(system: System<HookT>, pathPrefix: string, name: string) {
this.name = name;
this.pathPrefix = `${pathPrefix}/${name}`;
this.system = system;
this.runningFunctions = new Map<string, FunctionWorker>();
}
async load(manifest: Manifest) {
async load(manifest: Manifest<HookT>) {
this.manifest = manifest;
await this.system.pluginLoader.load(this.name, manifest);
await this.dispatchEvent("load");
@@ -111,7 +111,7 @@ export class Plugin {
}
async dispatchEvent(name: string, data?: any): Promise<any[]> {
let functionsToSpawn = this.manifest!.events[name];
let functionsToSpawn = this.manifest!.hooks.events[name];
if (functionsToSpawn) {
return await Promise.all(
functionsToSpawn.map(
@@ -135,16 +135,16 @@ export class Plugin {
}
}
export class System {
protected plugins: Map<string, Plugin>;
export class System<HookT> {
protected plugins: Map<string, Plugin<HookT>>;
protected pathPrefix: string;
registeredSyscalls: SysCallMapping;
pluginLoader: PluginLoader;
pluginLoader: PluginLoader<HookT>;
constructor(PluginLoader: PluginLoader, pathPrefix: string) {
constructor(PluginLoader: PluginLoader<HookT>, pathPrefix: string) {
this.pluginLoader = PluginLoader;
this.pathPrefix = pathPrefix;
this.plugins = new Map<string, Plugin>();
this.plugins = new Map<string, Plugin<HookT>>();
this.registeredSyscalls = {};
}
@@ -167,7 +167,7 @@ export class System {
return Promise.resolve(callback(...args));
}
async load(name: string, manifest: Manifest): Promise<Plugin> {
async load(name: string, manifest: Manifest<HookT>): Promise<Plugin<HookT>> {
const plugin = new Plugin(this, this.pathPrefix, name);
await plugin.load(manifest);
this.plugins.set(name, plugin);
+5 -19
View File
@@ -1,28 +1,14 @@
export interface Manifest {
export type EventHook = {
events: { [key: string]: string[] };
commands: {
[key: string]: CommandDef;
};
};
export interface Manifest<HookT> {
hooks: HookT & EventHook;
functions: {
[key: string]: FunctionDef;
};
}
export const slashCommandRegexp = /\/[\w\-]*/;
export interface CommandDef {
// Function name to invoke
invoke: string;
// Bind to keyboard shortcut
key?: string;
mac?: string;
// If to show in slash invoked menu and if so, with what label
// should match slashCommandRegexp
slashCommand?: string;
}
export interface FunctionDef {
path: string;
functionName?: string;
-14
View File
@@ -1,13 +1,3 @@
export function countWords(str: string): number {
var matches = str.match(/[\w\d\'\'-]+/gi);
return matches ? matches.length : 0;
}
export function readingTime(wordCount: number): number {
// 225 is average word reading speed for adults
return Math.ceil(wordCount / 225);
}
export function safeRun(fn: () => Promise<void>) {
fn().catch((e) => {
console.error(e);
@@ -21,7 +11,3 @@ export function sleep(ms: number): Promise<void> {
}, ms);
});
}
export function isMacLike() {
return /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform);
}