From c7f43e403daefc364ded235f71b24dbd6a0ac96b Mon Sep 17 00:00:00 2001 From: amsyarasyiq <82711525+amsyarasyiq@users.noreply.github.com> Date: Wed, 5 Apr 2023 16:27:53 +0800 Subject: [PATCH] read from `base_manifest.json` --- .eslintrc.json | 3 +- base_manifest.json | 9 ++++++ build.mjs | 35 ++++++++++++------------ plugins/HideGiftButton/manifest.json | 7 ----- plugins/Last.fm/manifest.json | 7 ----- plugins/MoreConfirm/manifest.json | 7 ----- plugins/NoAutoReplyMention/manifest.json | 7 ----- plugins/NoIdle/manifest.json | 7 ----- plugins/RichPresence/manifest.json | 7 ----- 9 files changed, 29 insertions(+), 60 deletions(-) create mode 100644 base_manifest.json diff --git a/.eslintrc.json b/.eslintrc.json index c3a1cd2..a128ed3 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -23,7 +23,8 @@ "rules": { "indent": [ "error", - 4 + 4, + { "SwitchCase": 1 } ], "quotes": [ "error", diff --git a/base_manifest.json b/base_manifest.json new file mode 100644 index 0000000..64fc0eb --- /dev/null +++ b/base_manifest.json @@ -0,0 +1,9 @@ +{ + "authors": [ + { + "name": "amsyarasyiq", + "id": "492949202121261067" + } + ], + "main": "src/index.ts" +} diff --git a/build.mjs b/build.mjs index 25596ca..65a2471 100644 --- a/build.mjs +++ b/build.mjs @@ -1,8 +1,7 @@ -/* eslint-disable indent */ import commonjs from "@rollup/plugin-commonjs"; import nodeResolve from "@rollup/plugin-node-resolve"; import { createHash } from "crypto"; -import { readdirSync, readFileSync, writeFileSync } from "fs"; +import { readFile, readdir, writeFile } from "fs/promises"; import { argv } from "process"; import { rollup, watch } from "rollup"; @@ -16,7 +15,7 @@ const inputPlugins = args.filter(x => !x.startsWith("-")); const flags = args.map(x => x.toLowerCase()).filter(x => x.startsWith("-")); const isWatch = flags.includes("--watch") || flags.includes("-w"); -const toBuild = inputPlugins.length ? inputPlugins : readdirSync("./plugins"); +const toBuild = inputPlugins.length ? inputPlugins : await readdir("./plugins"); console.log(`Building ${toBuild.length} plugin(s)...`); @@ -40,7 +39,13 @@ console.log("\n" + ( isWatch && console.log("\nWatching for changes..."); async function buildPlugin(plugin) { - const manifest = JSON.parse(readFileSync(`./plugins/${plugin}/manifest.json`).toString()); + if (plugin.endsWith(".ts")) return; + + const manifest = Object.assign( + JSON.parse(await readFile("./base_manifest.json")), + JSON.parse(await readFile(`./plugins/${plugin}/manifest.json`)) + ); + const entry = "index.js"; const outPath = `./dist/${plugin}/${entry}`; @@ -102,15 +107,17 @@ async function buildPlugin(plugin) { ] }; + Object.assign(manifest, { + hash: createHash("sha256").update(await readFile(outPath)).digest("hex"), + main: entry + }); + if (!isWatch) { const bundle = await rollup(options); await bundle.write(options.output); await bundle.close(); - const toHash = readFileSync(outPath); - manifest.hash = createHash("sha256").update(toHash).digest("hex"); - manifest.main = "index.js"; - writeFileSync(`./dist/${plugin}/manifest.json`, JSON.stringify(manifest)); + await writeFile(`./dist/${plugin}/manifest.json`, JSON.stringify(manifest)); console.log(`${plugin}: ` + "\x1b[32m" + "Build succeed!" + "\x1b[0m"); return; @@ -119,28 +126,22 @@ async function buildPlugin(plugin) { const watcher = watch(options); return await new Promise((resolve, reject) => { - watcher.on("event", (event) => { + watcher.on("event", async (event) => { switch (event.code) { case "BUNDLE_END": { event.result.close(); - const toHash = readFileSync(outPath); - manifest.hash = createHash("sha256").update(toHash).digest("hex"); - manifest.main = "index.js"; - writeFileSync(`./dist/${plugin}/manifest.json`, JSON.stringify(manifest)); + await writeFile(`./dist/${plugin}/manifest.json`, JSON.stringify(manifest)); console.log(`${plugin}: ` + "\x1b[32m" + `Build succeed! (${event.duration}ms)` + "\x1b[0m"); resolve(); break; } case "ERROR": - console.error(`${plugin}: ` + "\x1b[31m", "Failed! :(", "\x1b[0m"); - reject(event.error); - break; case "FATAL": console.error(`${plugin}: ` + "\x1b[31m", "Failed! :(", "\x1b[0m"); reject(event.error); - process.exit(1); + break; } }); }); diff --git a/plugins/HideGiftButton/manifest.json b/plugins/HideGiftButton/manifest.json index 01d9c50..f41fa9f 100644 --- a/plugins/HideGiftButton/manifest.json +++ b/plugins/HideGiftButton/manifest.json @@ -1,13 +1,6 @@ { "name": "HideGiftButton", "description": "Hides the gift button in the chat input.", - "authors": [ - { - "name": "amsyarasyiq", - "id": "492949202121261067" - } - ], - "main": "src/index.ts", "vendetta": { "icon": "ic_gift_24px" } diff --git a/plugins/Last.fm/manifest.json b/plugins/Last.fm/manifest.json index 510eeb2..6a9f6d3 100644 --- a/plugins/Last.fm/manifest.json +++ b/plugins/Last.fm/manifest.json @@ -1,13 +1,6 @@ { "name": "Last.fm", "description": "Show off your last.fm status on your Discord profile.", - "authors": [ - { - "name": "amsyarasyiq", - "id": "492949202121261067" - } - ], - "main": "src/index.ts", "vendetta": { "icon": "ic_stage_music" } diff --git a/plugins/MoreConfirm/manifest.json b/plugins/MoreConfirm/manifest.json index d455ba1..4950167 100644 --- a/plugins/MoreConfirm/manifest.json +++ b/plugins/MoreConfirm/manifest.json @@ -1,13 +1,6 @@ { "name": "MoreConfirm", "description": "Prompts confirmations before making irreversible actions.", - "authors": [ - { - "name": "amsyarasyiq", - "id": "492949202121261067" - } - ], - "main": "src/index.ts", "vendetta": { "icon": "check" } diff --git a/plugins/NoAutoReplyMention/manifest.json b/plugins/NoAutoReplyMention/manifest.json index 60c40b9..5ecb191 100644 --- a/plugins/NoAutoReplyMention/manifest.json +++ b/plugins/NoAutoReplyMention/manifest.json @@ -1,13 +1,6 @@ { "name": "NoAutoReplyMention", "description": "Turns off auto mention when replying to someone.", - "authors": [ - { - "name": "amsyarasyiq", - "id": "492949202121261067" - } - ], - "main": "src/index.ts", "vendetta": { "icon": "ic_notif_off" } diff --git a/plugins/NoIdle/manifest.json b/plugins/NoIdle/manifest.json index 17a37af..df16ac2 100644 --- a/plugins/NoIdle/manifest.json +++ b/plugins/NoIdle/manifest.json @@ -1,13 +1,6 @@ { "name": "NoIdle", "description": "Prevent status from idling when Discord is in the background.", - "authors": [ - { - "name": "amsyarasyiq", - "id": "492949202121261067" - } - ], - "main": "src/index.ts", "vendetta": { "icon": "StatusIdle" } diff --git a/plugins/RichPresence/manifest.json b/plugins/RichPresence/manifest.json index 81e0f22..3a8b2b2 100644 --- a/plugins/RichPresence/manifest.json +++ b/plugins/RichPresence/manifest.json @@ -1,13 +1,6 @@ { "name": "RichPresence", "description": "Sets Discord Rich Presence for your profile.", - "authors": [ - { - "name": "amsyarasyiq", - "id": "492949202121261067" - } - ], - "main": "src/index.ts", "vendetta": { "icon": "games" }