Skip to content

Commit

Permalink
Simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
RuiNtD committed Jun 23, 2024
1 parent ec0549e commit d4c4c00
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 26 deletions.
1 change: 1 addition & 0 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
12 changes: 12 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 6 additions & 10 deletions src/java.ts
Original file line number Diff line number Diff line change
@@ -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(),
Expand All @@ -19,23 +21,17 @@ const Release = z.object({
});
type Release = z.infer<typeof Release>;

const cache = new Map<JavaVersion, Release>();

export async function getJavaRelease(version: JavaVersion): Promise<Release> {
const cached = cache.get(version);
if (cached) return cached;

async function _getJavaRelease(version: JavaVersion): Promise<Release> {
const params = new URLSearchParams({
architecture: "x64",
image_type: "jre",
os: "windows",
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));
Expand Down
24 changes: 8 additions & 16 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();

Expand All @@ -120,30 +120,22 @@ 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`;
$.logStep("Creating 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!");

0 comments on commit d4c4c00

Please sign in to comment.