feat: only download java if not present

This commit is contained in:
HerozDotExe 2026-01-08 11:05:41 +01:00
parent d46340c633
commit 76f3d7b588

View file

@ -14,6 +14,7 @@ import { installForge } from "../core/modloaders";
import { getJavaExecutable } from "../core/java"; import { getJavaExecutable } from "../core/java";
import { readJson } from "../utils/fs"; import { readJson } from "../utils/fs";
import { mergeManifests } from "../core/mergeManifests"; import { mergeManifests } from "../core/mergeManifests";
import { exec } from "node:child_process";
function getJavaOs() { function getJavaOs() {
switch (os()) { switch (os()) {
@ -58,6 +59,20 @@ interface InstanceEvents {
]; ];
} }
function checkJava(javaLocation: string) {
const javaExe = getJavaExecutable(javaLocation);
return new Promise<boolean>((res) => {
exec(`${javaExe} -version`, (err) => {
if (err) {
res(false);
} else {
res(true);
}
});
});
}
export class Instance extends EventEmitter<InstanceEvents> { export class Instance extends EventEmitter<InstanceEvents> {
version: string; version: string;
modloader?: Modloader; modloader?: Modloader;
@ -117,7 +132,7 @@ export class Instance extends EventEmitter<InstanceEvents> {
this.args.game = game || ""; this.args.game = game || "";
} }
async initialize() { private async initialize() {
this.versionLocation = path.join(this.paths.versions, this.version); this.versionLocation = path.join(this.paths.versions, this.version);
this.instanceLocation = path.join(this.paths.instances, this.version); this.instanceLocation = path.join(this.paths.instances, this.version);
@ -209,26 +224,28 @@ export class Instance extends EventEmitter<InstanceEvents> {
error.throw(); error.throw();
} }
try { if (!await checkJava(this.javaLocation)) {
const javaDownloader = await core.java.JavaDownloader( try {
getJavaOs(), const javaDownloader = await core.java.JavaDownloader(
this.versionManifest.javaVersion.component, getJavaOs(),
this.paths.javaRoot, this.versionManifest.javaVersion.component,
); this.paths.javaRoot,
javaDownloader.on("completed", () => {
this.emit(
"progress",
"java",
javaDownloader.done,
javaDownloader.total,
javaDownloader.doneSize,
javaDownloader.totalSize,
); );
}); javaDownloader.on("completed", () => {
await javaDownloader.run(); this.emit(
} catch (original) { "progress",
const error = new InstallError("java", original); "java",
error.throw(); javaDownloader.done,
javaDownloader.total,
javaDownloader.doneSize,
javaDownloader.totalSize,
);
});
await javaDownloader.run();
} catch (original) {
const error = new InstallError("java", original);
error.throw();
}
} }
if (this.modloader) { if (this.modloader) {