Skip to content
This repository has been archived by the owner on May 26, 2024. It is now read-only.

Commit

Permalink
Begin working on plugin system
Browse files Browse the repository at this point in the history
  • Loading branch information
pylixonly committed Jan 13, 2024
1 parent 7b38196 commit 4da12ac
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 4 deletions.
30 changes: 29 additions & 1 deletion build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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);
}
};

Expand Down
8 changes: 8 additions & 0 deletions src/core/managers/plugins.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 1 addition & 1 deletion src/core/patches/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

10 changes: 10 additions & 0 deletions src/modules.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 2 additions & 2 deletions src/plugins/Experiments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
});

0 comments on commit 4da12ac

Please sign in to comment.