From d4c4c00e0f8aa1bdbe48055b5a93fcbe7e48a422 Mon Sep 17 00:00:00 2001 From: RuiNtD Date: Sun, 23 Jun 2024 10:48:22 -0600 Subject: [PATCH] Simplify code --- deno.json | 1 + deno.lock | 12 ++++++++++++ src/java.ts | 16 ++++++---------- src/main.ts | 24 ++++++++---------------- 4 files changed, 27 insertions(+), 26 deletions(-) diff --git a/deno.json b/deno.json index c78663f..45fc29b 100644 --- a/deno.json +++ b/deno.json @@ -11,6 +11,7 @@ "@std/fmt": "jsr:@std/fmt@^0.223.0", "@std/ini": "jsr:@std/ini@^0.223.0", "@zip-js/zip-js": "jsr:@zip-js/zip-js@^2.7.42", + "memoize": "npm:memoize@^10.0.0", "zod": "npm:zod@^3.23.4" } } diff --git a/deno.lock b/deno.lock index 701bea5..006abc2 100644 --- a/deno.lock +++ b/deno.lock @@ -20,6 +20,7 @@ "jsr:@std/path@^0.221.0": "jsr:@std/path@0.221.0", "jsr:@std/streams@0.221.0": "jsr:@std/streams@0.221.0", "jsr:@zip-js/zip-js@^2.7.42": "jsr:@zip-js/zip-js@2.7.42", + "npm:memoize@^10.0.0": "npm:memoize@10.0.0", "npm:zod@^3.23.4": "npm:zod@3.23.4" }, "jsr": { @@ -99,6 +100,16 @@ } }, "npm": { + "memoize@10.0.0": { + "integrity": "sha512-H6cBLgsi6vMWOcCpvVCdFFnl3kerEXbrYh9q+lY6VXvQSmM6CkmV08VOwT+WE2tzIEqRPFfAq3fm4v/UIW6mSA==", + "dependencies": { + "mimic-function": "mimic-function@5.0.1" + } + }, + "mimic-function@5.0.1": { + "integrity": "sha512-VP79XUPxV2CigYP3jWwAUFSku2aKqBH7uTAapFWCBqutsbmDo96KY5o8uh6U+/YSIn5OxJnXp73beVkpqMIGhA==", + "dependencies": {} + }, "zod@3.23.4": { "integrity": "sha512-/AtWOKbBgjzEYYQRNfoGKHObgfAZag6qUJX1VbHo2PRBgS+wfWagEY2mizjfyAPcGesrJOcx/wcl0L9WnVrHFw==", "dependencies": {} @@ -115,6 +126,7 @@ "jsr:@std/fmt@^0.223.0", "jsr:@std/ini@^0.223.0", "jsr:@zip-js/zip-js@^2.7.42", + "npm:memoize@^10.0.0", "npm:zod@^3.23.4" ] } diff --git a/src/java.ts b/src/java.ts index 440754d..24eb5a2 100644 --- a/src/java.ts +++ b/src/java.ts @@ -1,7 +1,9 @@ import z from "zod"; import $ from "@david/dax"; +import memoize from "memoize"; -export type JavaVersion = 8 | 17 | 21; +export const javaVersions = [8, 17, 21] as const; +export type JavaVersion = (typeof javaVersions)[number]; const Release = z.object({ release_name: z.string(), @@ -19,12 +21,7 @@ const Release = z.object({ }); type Release = z.infer; -const cache = new Map(); - -export async function getJavaRelease(version: JavaVersion): Promise { - const cached = cache.get(version); - if (cached) return cached; - +async function _getJavaRelease(version: JavaVersion): Promise { const params = new URLSearchParams({ architecture: "x64", image_type: "jre", @@ -32,10 +29,9 @@ export async function getJavaRelease(version: JavaVersion): Promise { vendor: "eclipse", }); const url = `https://api.adoptium.net/v3/assets/latest/${version}/hotspot?${params}`; - const release = z.tuple([Release]).parse(await $.request(url).json())[0]; - cache.set(version, release); - return release; + return z.tuple([Release]).parse(await $.request(url).json())[0]; } +export const getJavaRelease = memoize(_getJavaRelease); if (import.meta.main) { $.log(await getJavaRelease(21)); diff --git a/src/main.ts b/src/main.ts index b97ec0a..6d6d32e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,7 +3,7 @@ import $ from "@david/dax"; import * as ini from "@std/ini"; import { ZipReader, Uint8ArrayWriter } from "@zip-js/zip-js"; import { getLatestPrism } from "./prism.ts"; -import { JavaVersion, getJavaRelease } from "./java.ts"; +import { JavaVersion, getJavaRelease, javaVersions } from "./java.ts"; import * as dotenv from "@std/dotenv"; $.setPrintCommand(true); @@ -94,12 +94,12 @@ async function getJavaVer(version: JavaVersion) { return { java: data.JAVA_VERSION, full: data.FULL_VERSION }; } -async function updateJava(version: JavaVersion) { +for (const version of javaVersions) { const jreDir = appPath.join(`jre${version}`); const release = await getJavaRelease(version); if (release.version.openjdk_version == (await getJavaVer(version))?.full) { $.logLight(`Skipping Java ${version} (Already up to date)`); - return; + continue; } jreDir.emptyDir(); @@ -120,10 +120,6 @@ async function updateJava(version: JavaVersion) { } } -await updateJava(8); -await updateJava(17); -await updateJava(21); - if (updateAvailable || (await $.confirm("Create launcher and installer?"))) { $.logStep("Creating launcher"); await $`PortableApps.comLauncher/PortableApps.comLauncherGenerator.exe $PWD\\PrismLauncherPortable`; @@ -131,19 +127,15 @@ if (updateAvailable || (await $.confirm("Create launcher and installer?"))) { await $`PortableApps.comInstaller/PortableApps.comInstaller.exe $PWD\\PrismLauncherPortable`; } -async function logJavaVersion(version: JavaVersion) { - const release = await getJavaRelease(version); - const vers = (await getJavaVer(version))?.java; - $.log(`- Includes [Java ${vers}](${release.release_link})`); -} - $.log(); $.log( `- Using [Prism Launcher ${latestVersion}](https://prismlauncher.org/news/release-${latestVersion})` ); -await logJavaVersion(8); -await logJavaVersion(17); -await logJavaVersion(21); +for (const version of javaVersions) { + const release = await getJavaRelease(version); + const vers = (await getJavaVer(version))?.java; + $.log(`- Includes [Java ${vers}](${release.release_link})`); +} $.log(); alert("Done!");