From 61d32b727ecbdd8c9bcf60ac70e78259b9f484a4 Mon Sep 17 00:00:00 2001 From: amsyarasyiq <82711525+amsyarasyiq@users.noreply.github.com> Date: Wed, 15 Feb 2023 15:04:37 +0800 Subject: [PATCH] chore: remove unneeded deps --- .github/workflows/deploy.yml | 5 ++++- build.mjs | 30 ++++++++++++++++-------------- package.json | 10 +++------- plugins/Last.fm/src/Settings.tsx | 4 ++-- plugins/Last.fm/src/index.ts | 6 +++--- plugins/Last.fm/src/utils.ts | 30 ++++++++++++++++-------------- 6 files changed, 44 insertions(+), 41 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 53f3642..87adf2f 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -21,6 +21,9 @@ jobs: - name: Install deps run: pnpm m i + - name: Run ESLint + run: pnpm lint + - name: Build plugin(s) run: node ./build.mjs @@ -44,4 +47,4 @@ jobs: # Makes it so the md files in the previous step get processed by GitHub Pages enable_jekyll: true # This creates the CNAME file required to host GitHub Pages on a custom domain - # cname: example.com \ No newline at end of file + # cname: example.com diff --git a/build.mjs b/build.mjs index 6f835e9..7053ad4 100644 --- a/build.mjs +++ b/build.mjs @@ -24,17 +24,17 @@ for (const plugin of toBuild) { try { await buildPlugin(plugin); } catch (e) { + console.error(e.stack || e); failed++; } } console.log("\n" + ( failed - ? "\x1b[31m" + `Failed to build ${failed} plugin(s)"}` + "\x1b[0m" + ? "\x1b[31m" + `Failed to build ${failed} plugin(s)` + "\x1b[0m" : "\x1b[32m" + "All plugin(s) built successfully!" + "\x1b[0m" )); - isWatch && console.log("\nWatching for changes..."); async function buildPlugin(plugin) { @@ -42,6 +42,7 @@ async function buildPlugin(plugin) { const entry = "index.js"; const outPath = `./dist/${plugin}/${entry}`; + /** @type {import("rollup").RollupOptions} */ const options = { input: `./plugins/${plugin}/${manifest.main}`, output: { @@ -49,7 +50,7 @@ async function buildPlugin(plugin) { globals(id) { if (id.startsWith("@vendetta")) return id.substring(1).replace(/\//g, "."); const map = { - react: "window.React", + react: "window.React" }; return map[id] || null; @@ -70,18 +71,23 @@ async function buildPlugin(plugin) { commonjs(), esbuild({ target: "esnext", + supported: { + "arrow": false, // arrows are supported but not with async await + "class": false, + "bigint": false, + }, minify: true, }) ] }; if (!isWatch) { - return await rollup(options).then(async (bundle) => { - await bundle.write(options.output); - await bundle.close(); + const bundle = await rollup(options); + await bundle.write(options.output); + await bundle.close(); - console.log(`${plugin}: ` + "\x1b[32m" + "Build succeed!" + "\x1b[0m"); - }); + console.log(`${plugin}: ` + "\x1b[32m" + "Build succeed!" + "\x1b[0m"); + return; } const watcher = watch(options); @@ -89,8 +95,6 @@ async function buildPlugin(plugin) { return await new Promise((resolve, reject) => { watcher.on("event", (event) => { switch (event.code) { - case "START": - break; case "BUNDLE_END": { event.result.close(); @@ -105,13 +109,11 @@ async function buildPlugin(plugin) { } case "ERROR": console.error(`${plugin}: ` + "\x1b[31m", "Failed! :(", "\x1b[0m"); - console.error(event.error.stack); - reject(event.error.stack); + reject(event.error); break; case "FATAL": console.error(`${plugin}: ` + "\x1b[31m", "Failed! :(", "\x1b[0m"); - console.error(event.error.stack); - reject(event.error.stack); + reject(event.error); process.exit(1); } }); diff --git a/package.json b/package.json index abcfa29..18b2666 100644 --- a/package.json +++ b/package.json @@ -5,17 +5,13 @@ }, "devDependencies": { "@rollup/plugin-commonjs": "^24.0.1", - "@rollup/plugin-eslint": "^9.0.3", "@rollup/plugin-node-resolve": "^15.0.1", - "@rollup/plugin-typescript": "^11.0.0", - "@typescript-eslint/eslint-plugin": "^5.51.0", - "@typescript-eslint/parser": "^5.51.0", + "@types/react": "^18.0.28", + "@types/react-native": "^0.70.11", "esbuild": "^0.16.17", "eslint": "^8.34.0", - "eslint-plugin-react": "^7.32.2", "rollup": "^3.15.0", "rollup-plugin-esbuild": "^5.0.0", - "typescript": "^4.9.5", - "vendetta-types": "^2.3.4" + "vendetta-types": "^2.3.6" } } diff --git a/plugins/Last.fm/src/Settings.tsx b/plugins/Last.fm/src/Settings.tsx index fc2342e..9845a13 100644 --- a/plugins/Last.fm/src/Settings.tsx +++ b/plugins/Last.fm/src/Settings.tsx @@ -7,7 +7,7 @@ import { showToast } from "@vendetta/ui/toasts"; import { currentSettings } from "."; import Constants from "./constants"; -import { flush, initialize } from "./utils"; +import { initialize } from "./utils"; const { ScrollView, TouchableOpacity, Text } = ReactNative; const { FormInput, FormDivider, FormSwitchRow, FormText, FormIcon } = Forms; @@ -21,7 +21,6 @@ function UpdateButton() { } console.log("Applying settings..."); - await flush(); await initialize(); showToast("Settings updated!", getAssetIDByName("Check")); } @@ -31,6 +30,7 @@ function UpdateButton() { ; } + export default function Settings() { const settings = useProxy(storage) as PluginSettings; const navigation = NavigationNative.useNavigation(); diff --git a/plugins/Last.fm/src/index.ts b/plugins/Last.fm/src/index.ts index 29de04e..794530b 100644 --- a/plugins/Last.fm/src/index.ts +++ b/plugins/Last.fm/src/index.ts @@ -6,7 +6,7 @@ import Settings from "./Settings"; import { flush, initialize, patchActivityType } from "./utils"; export const global = {} as { - stopped?: boolean, + pluginStopped?: boolean, lastActivity?: Activity, updateInterval?: NodeJS.Timer, lastTrackUrl?: string, @@ -26,7 +26,7 @@ export default { onLoad: () => { console.log("Starting last.fm plugin.."); global.activityTypeUnpatch = patchActivityType(); - global.stopped = false; + global.pluginStopped = false; if (UserStore.getCurrentUser()) { console.log("User is already logged in, initializing..."); @@ -40,7 +40,7 @@ export default { onUnload: () => { console.log("Stopping last.fm..."); - global.stopped = true; + global.pluginStopped = true; flush(); global.activityTypeUnpatch?.(); diff --git a/plugins/Last.fm/src/utils.ts b/plugins/Last.fm/src/utils.ts index a6cbaf9..7f52d30 100644 --- a/plugins/Last.fm/src/utils.ts +++ b/plugins/Last.fm/src/utils.ts @@ -16,11 +16,15 @@ async function fetchLatestScrobble(): Promise { "extended": "1" }).toString(); - const info = await fetch(`https://ws.audioscrobbler.com/2.0/?${params}`).then(x => { - if (!x.ok) throw new Error(`Failed to fetch latest scrobble: ${x.status} ${x.statusText}`); - return x.json(); - }); - const lastTrack = info.recenttracks?.track?.[0] || Promise.reject(info); + const result = await fetch(`https://ws.audioscrobbler.com/2.0/?${params}`); + if (!result.ok) throw new Error(`Failed to fetch the latest scrobble: ${result.statusText}`); + + const info = await result.json(); + + const lastTrack = info?.recenttracks?.track?.[0]; + + if (!lastTrack) throw info; + return { name: lastTrack.name, artist: lastTrack.artist.name, @@ -47,7 +51,7 @@ async function handleAlbumCover(cover: string): Promise { /** Sends the activity details to Discord */ async function sendRequest(activity: Activity): Promise { - if (global.stopped) { + if (global.pluginStopped) { console.log("--> Plugin is unloaded, aborting..."); global.updateInterval && clearInterval(global.updateInterval); activity = null; @@ -91,7 +95,7 @@ export async function update() { throw new Error("Username is not set"); } - const lastTrack = await fetchLatestScrobble().catch(async function (err) { + const lastTrack = await fetchLatestScrobble().catch(async (err) => { verboseLog("--> An error occurred while fetching the last track, aborting..."); await clearActivity().catch(); throw err; @@ -132,7 +136,7 @@ export async function update() { }; } - const response = await sendRequest(activity).catch(async function (err) { + const response = await sendRequest(activity).catch(async (err) => { verboseLog("--> An error occurred while setting the activity"); await clearActivity().catch(); throw err; @@ -147,6 +151,8 @@ export async function update() { export function flush(): Promise { console.log("--> Flushing..."); global.lastActivity = null; + global.lastTrackUrl = null; + global.updateInterval && clearInterval(global.updateInterval); return clearActivity(); @@ -156,14 +162,11 @@ export function flush(): Promise { export async function initialize() { console.log("--> Initializing..."); - if (global.stopped) { + if (global.pluginStopped) { throw new Error("Plugin is already stopped!"); } - global.lastTrackUrl = null; - - global.updateInterval && clearInterval(global.updateInterval); - global.lastActivity && await clearActivity(); + await flush(); let tries = 0; @@ -182,7 +185,6 @@ export async function initialize() { if (++tries > 3) { console.error("Failed to fetch/set activity 3 times, aborting..."); clearInterval(global.updateInterval); - global.stopped = true; } }), (currentSettings.timeInterval || Constants.DEFAULT_TIME_INTERVAL) * 1000