From 4da12acacebf3d51fdfc69228990dc97923ca09c Mon Sep 17 00:00:00 2001 From: amsyarasyiq <82711525+amsyarasyiq@users.noreply.github.com> Date: Sat, 13 Jan 2024 20:05:02 +0800 Subject: [PATCH] Begin working on plugin system --- build.mjs | 30 +++++++++++++++++++++++++++++- src/core/managers/plugins.ts | 8 ++++++++ src/core/patches/index.ts | 2 +- src/modules.d.ts | 10 ++++++++++ src/plugins/Experiments/index.ts | 4 ++-- 5 files changed, 50 insertions(+), 4 deletions(-) diff --git a/build.mjs b/build.mjs index a6db34f..9c3490a 100644 --- a/build.mjs +++ b/build.mjs @@ -3,7 +3,7 @@ import swc from "@swc/core"; import { execSync } from "child_process"; import { createHash } from "crypto"; import esbuild from "esbuild"; -import { readFile } from "fs/promises"; +import { readdir, readFile } from "fs/promises"; import { createServer } from "http"; import { argv } from "process"; @@ -22,6 +22,34 @@ const constantsMap = { "module-definition-hash": async () => { const defs = await readFile("src/internal-metro/requireDef.ts"); return JSON.stringify(createHash("sha256").update(defs).digest("hex")); + }, + "pyon-plugins": async () => { + const ret = {}; + const pluginList = await readdir("src/plugins"); + + for (const pluginName of pluginList) { + const ctx = await esbuild.build({ + entryPoints: [`src/plugins/${pluginName}/index.ts`], + bundle: true, + format: "iife", + target: "esnext", + keepNames: true, + write: false, + minify: true, + footer: { + js: `//# sourceURL=pyon-plugin-${pluginName}` + }, + legalComments: "none" + }); + + const script = ctx.outputFiles[0].text; + ret[pluginName] = { + name: pluginName, + script + }; + } + + return JSON.stringify(ret); } }; diff --git a/src/core/managers/plugins.ts b/src/core/managers/plugins.ts index be24c3b..72f9a1b 100644 --- a/src/core/managers/plugins.ts +++ b/src/core/managers/plugins.ts @@ -1,3 +1,11 @@ +import plugins from "~pyon-plugins"; + export function loadPlugins() { + for (const pluginName in plugins) { + const definePlugin = r => console.log("definePlugin", r); + const definePluginSettings = r => console.log("definePluginSettings", r); + const execute = new Function("definePlugin", "definePluginSettings", `return ${plugins[pluginName].script}`); + execute(definePlugin, definePluginSettings); + } } diff --git a/src/core/patches/index.ts b/src/core/patches/index.ts index c080b41..d94b0ab 100644 --- a/src/core/patches/index.ts +++ b/src/core/patches/index.ts @@ -2,4 +2,4 @@ export { default as patchChatInput } from "./chatInput"; export { default as patchExperiments } from "./experiments"; export { default as patchIdle } from "./idle"; export { default as patchSettings } from "./settings"; -export { default as patchTheme } from "./theme"; + diff --git a/src/modules.d.ts b/src/modules.d.ts index 4596781..ca2afd2 100644 --- a/src/modules.d.ts +++ b/src/modules.d.ts @@ -7,3 +7,13 @@ declare module "~module-definition-hash" { const defHash: string; export default hash; } + +declare module "~pyon-plugins" { + const plugins: { + [name: string]: { + name: string, + script: string; + }; + }; + export default plugins; +} diff --git a/src/plugins/Experiments/index.ts b/src/plugins/Experiments/index.ts index 6742d71..4752d78 100644 --- a/src/plugins/Experiments/index.ts +++ b/src/plugins/Experiments/index.ts @@ -21,12 +21,12 @@ export default definePlugin({ start() { console.log("hello!"); - patcher("do the patch"); + patcher?.("do the patch"); }, stop() { console.log("bye, bye!"); - patcher("the bye unpatch"); + patcher?.("the bye unpatch"); } });