37 lines
2.5 KiB
JavaScript
37 lines
2.5 KiB
JavaScript
import { readFile } from "node:fs/promises";
|
|
|
|
/** Load the actual compiled packages through the production Blob module loader. */
|
|
export async function mockFeaturePlugins(surface) {
|
|
const packages = await Promise.all(["terminal", "table-formulas"].map(async name => {
|
|
const root = new URL(`../plugin-packages/${name}/`, import.meta.url);
|
|
return {
|
|
manifest: JSON.parse(await readFile(new URL("plugin.json", root), "utf8")),
|
|
code: await readFile(new URL("main.js", root), "utf8"),
|
|
css: name === "terminal" ? await readFile(new URL("style.css", root), "base64") : "",
|
|
};
|
|
}));
|
|
await surface.addInitScript(packages => {
|
|
const key = "test.installed-plugins";
|
|
const root = "C:\\AppData\\RuneStone\\plugins";
|
|
const defaults = packages.map(item => ({ manifest: item.manifest, hash: "test-hash", enabled: true, path: root + "\\" + item.manifest.id, error: null, removal: false }));
|
|
const read = () => JSON.parse(localStorage.getItem(key) || JSON.stringify(defaults));
|
|
const write = rows => { localStorage.setItem(key, JSON.stringify(rows)); window.__TEST__?.emit("plugins-changed", {}); };
|
|
if (!localStorage.getItem(key)) localStorage.setItem(key, JSON.stringify(defaults));
|
|
window.addEventListener("storage", event => { if (event.key === key) window.__TEST__?.emit("plugins-changed", {}); });
|
|
const original = window.__TAURI_INTERNALS__.invoke;
|
|
window.__TAURI_INTERNALS__.invoke = async (command, args = {}) => {
|
|
if (!command.startsWith("plugins_")) return original(command, args);
|
|
window.__TEST__?.calls.push({ command, args });
|
|
if (command === "plugins_snapshot") return { root, plugins: read() };
|
|
if (command === "plugins_load") return packages.find(item => item.manifest.id === args.id).code;
|
|
if (command === "plugins_asset") return packages.find(item => item.manifest.id === args.id).css;
|
|
if (command === "plugins_set_enabled") { write(read().map(item => item.manifest.id === args.id ? { ...item, enabled: args.enabled } : item)); return; }
|
|
if (command === "plugins_prepare_remove") return { token: args.id, plugin: read().find(item => item.manifest.id === args.id) };
|
|
if (command === "plugins_remove") { write(read().filter(item => item.manifest.id !== args.token)); return; }
|
|
if (command === "plugins_data") return "null";
|
|
if (["plugins_release", "plugins_cancel", "plugins_reveal"].includes(command)) return;
|
|
throw new Error(`Unhandled plugin IPC: ${command}`);
|
|
};
|
|
}, packages);
|
|
}
|