Main: add watch

This commit is contained in:
amsyarasyiq 2023-02-13 00:18:39 +08:00
parent 4ab72b8dd9
commit fa69e9e3a9
2 changed files with 60 additions and 30 deletions

View file

@ -36,6 +36,7 @@
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/triple-slash-reference": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"eol-last": "error"
}
}

View file

@ -1,18 +1,42 @@
/* eslint-disable indent */
import commonjs from "@rollup/plugin-commonjs";
import nodeResolve from "@rollup/plugin-node-resolve";
import { createHash } from "crypto";
import { readdir, readFile, writeFile } from "fs/promises";
import { rollup } from "rollup";
import { readdirSync, readFileSync } from "fs";
import { argv } from "process";
import { watch } from "rollup";
import esbuild from "rollup-plugin-esbuild";
for (const plug of await readdir("./plugins")) {
const manifest = JSON.parse(await readFile(`./plugins/${plug}/manifest.json`));
const args = argv.slice(2);
const plugin = args.find(x => !x.startsWith("-"));
const flags = args.map(x => x.toLowerCase()).filter(x => x.startsWith("-"));
const isWatching = flags.includes("--watch") || flags.includes("-w");
console.clear();
for (const plug of plugin ? [plugin] : readdirSync("./plugins")) {
const manifest = JSON.parse(readFileSync(`./plugins/${plug}/manifest.json`).toString());
const entry = "index.js";
const outPath = `./dist/${plug}/${entry}`;
try {
const bundle = await rollup({
const config = {
input: `./plugins/${plug}/${manifest.main}`,
output: {
file: outPath,
globals(id) {
if (id.startsWith("@vendetta")) return id.substring(1).replace(/\//g, ".");
const map = {
react: "window.React",
};
return map[id] || null;
},
format: "iife",
compact: true,
exports: "named",
},
onwarn: (warning) => {
![
"UNRESOLVED_IMPORT",
@ -27,33 +51,38 @@ for (const plug of await readdir("./plugins")) {
target: "esnext",
minify: true,
})
],
});
await bundle.write({
file: outPath,
globals(id) {
if (id.startsWith("@vendetta")) return id.substring(1).replace(/\//g, ".");
const map = {
react: "window.React",
]
};
return map[id] || null;
},
format: "iife",
compact: true,
exports: "named",
const watchPlugin = new Promise((resolve, reject) => {
const watcher = watch(config);
watcher.on("event", (event) => {
switch (event.code) {
case "START":
process.stdout.write(`Building ${plug}... `);
break;
case "BUNDLE_END":
console.log("\x1b[32m", `Succeed! (${event.duration}ms)`, "\x1b[0m");
resolve();
break;
case "ERROR":
console.error("\x1b[31m", "Failed! :(", "\x1b[0m");
console.error(event.error);
reject(event.error);
break;
case "FATAL":
reject(event.error);
process.exit(1);
}
});
});
await bundle.close();
const toHash = await readFile(outPath);
manifest.hash = createHash("sha256").update(toHash).digest("hex");
manifest.main = entry;
await writeFile(`./dist/${plug}/manifest.json`, JSON.stringify(manifest));
console.log(`Successfully built ${manifest.name}!`);
await watchPlugin.catch(console.error);
} catch (e) {
console.error("Failed to build plugin...", e);
console.error(`Failed to build ${plug}...`, e);
process.exit(1);
}
}
if (!isWatching) process.exit(0);
else console.log("\nWatching for changes...");