diff --git a/.eslintrc.json b/.eslintrc.json index ab5b5aa..3ca2809 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -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" } } diff --git a/build.mjs b/build.mjs index 677b678..22147dd 100644 --- a/build.mjs +++ b/build.mjs @@ -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, }) - ], + ] + }; + + 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.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", - }); - 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...");