From 143a85228114eb24271c20fdfd451ba99507e0ed Mon Sep 17 00:00:00 2001 From: HerozDotExe Date: Fri, 6 Mar 2026 14:54:23 +0100 Subject: [PATCH] fix: fix eslint not actually working and fixed every ts/eslint warnings --- eslint.config.js | 7 ++- src/core/arguments.ts | 22 ++++---- src/core/launch.ts | 2 +- src/core/mergeManifests.ts | 16 ++++-- src/launcher/instance.ts | 107 ++++++++++++++++++++----------------- src/launcher/java.ts | 6 +-- src/utils/errors.ts | 4 +- src/utils/fetch.ts | 19 ++++--- src/utils/rules.ts | 2 +- src/utils/types.ts | 3 +- src/utils/unzip.ts | 4 +- tsconfig.json | 1 + 12 files changed, 107 insertions(+), 86 deletions(-) diff --git a/eslint.config.js b/eslint.config.js index 7b94d32..bbb4a43 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -9,7 +9,12 @@ export default defineConfig([ files: ["**/*.{js,mjs,cjs,ts,mts,cts}"], plugins: { js }, extends: ["js/recommended"], - languageOptions: { globals: globals.node }, + languageOptions: { + globals: globals.node, + parserOptions: { + projectService: true + } + }, }, tseslint.configs.recommended, eslintConfigPrettier, diff --git a/src/core/arguments.ts b/src/core/arguments.ts index 9382f94..b8930af 100644 --- a/src/core/arguments.ts +++ b/src/core/arguments.ts @@ -15,7 +15,7 @@ function fillArguments( classPaths: string, auth: Auth, ) { - const argumentsToFill = { + const argumentsToFill: { [key: string]: string } = { "${natives_directory}": path.join(instancePath, "natives"), "${launcher_name}": "nlk", "${launcher_version}": packageVersion, @@ -127,12 +127,8 @@ export async function generateLaunchArguments( assetsPath: string, versionRoot: string, auth: Auth, - options: { - minRam?: string; - maxRam?: string; - customJvmArgs?: string; - customGameArgs?: string; - }, + customArgs: { java: string; game: string }, + ram: { max: string; min: string }, ) { const jvm: string[] = []; let game: string[] = []; @@ -186,14 +182,14 @@ export async function generateLaunchArguments( jvm.push(classPaths) } - if (options.customJvmArgs !== "") { - for (const arg of options.customJvmArgs.split(" ")) { + if (customArgs.java !== "") { + for (const arg of customArgs.java.split(" ")) { jvm.push(arg); } } - if (options.customGameArgs !== "") { - for (const arg of options.customGameArgs.split(" ")) { + if (customArgs.game !== "") { + for (const arg of customArgs.game.split(" ")) { game.push(arg); } } @@ -202,8 +198,8 @@ export async function generateLaunchArguments( let launchArguments = [ ...jvm, - `-Xms${options.minRam || "2G"}`, - `-Xmx${options.maxRam || "2G"}`, + `-Xms${ram.min}`, + `-Xmx${ram.max}`, "-XX:+UnlockExperimentalVMOptions", "-XX:+UseG1GC", "-XX:G1NewSizePercent=20", diff --git a/src/core/launch.ts b/src/core/launch.ts index b199dd9..ca8c840 100644 --- a/src/core/launch.ts +++ b/src/core/launch.ts @@ -1,5 +1,5 @@ import { version as packageVersion } from "../../package.json"; -import { spawn, spawnSync } from "child_process"; +import { spawn } from "child_process"; import { LaunchArguments } from "../utils/types"; export function launch( diff --git a/src/core/mergeManifests.ts b/src/core/mergeManifests.ts index 766117f..cd43be4 100644 --- a/src/core/mergeManifests.ts +++ b/src/core/mergeManifests.ts @@ -14,12 +14,18 @@ export function mergeManifests(base: Version, layer: Version) { base.libraries.push(lib); } - for (const arg of layer.arguments.game) { - base.arguments.game.push(arg); - } + if (layer.arguments && base.arguments) { + // modern versions + for (const arg of layer.arguments.game) { + base.arguments.game.push(arg); + } - for (const arg of layer.arguments.jvm) { - base.arguments.jvm.push(arg); + for (const arg of layer.arguments.jvm) { + base.arguments.jvm.push(arg); + } + } else { + // older versions + base.minecraftArguments = layer.minecraftArguments } return base; diff --git a/src/launcher/instance.ts b/src/launcher/instance.ts index ff9217d..76a9d82 100644 --- a/src/launcher/instance.ts +++ b/src/launcher/instance.ts @@ -14,22 +14,25 @@ import { installForge } from "../core/modloaders"; import { readJson } from "../utils/fs"; import { mergeManifests } from "../core/mergeManifests"; import { checkJava } from "./java"; +import { ChildProcessWithoutNullStreams } from "node:child_process"; export class Instance extends EventEmitter { - version: string; + version?: string; modloader?: Modloader; - auth: Auth; - paths: Paths; + auth?: Auth; + paths?: Paths; args: { java: string; game: string }; - versionManifest: Version; - versionLocation: string; - instanceLocation: string; + ram: { max: string; min: string }; + versionManifest?: Version; + versionLocation?: string; + instanceLocation?: string; ready: boolean; - javaExecutable: string; + javaExecutable?: string; constructor() { super(); this.args = { java: "", game: "" }; + this.ram = { max: "2G", min: "2G" }; this.ready = false; } @@ -45,9 +48,7 @@ export class Instance extends EventEmitter { this.javaExecutable = path; } - setPaths(paths: string); - setPaths(paths: Paths); - setPaths(paths: Paths | string) { + setPaths(paths: Paths | string): void { if (typeof paths === "string") { this.paths = { root: paths, @@ -76,13 +77,18 @@ export class Instance extends EventEmitter { this.args.game = game || ""; } + setRAM({ min, max }: { min?: string; max?: string }) { + this.ram.min = min || "2G"; + this.ram.max = max || "2G"; + } + private async initialize() { - if (!this.javaExecutable || !this.paths.root || !this.version) { + if (!this.javaExecutable || !this.paths?.root || !this.version || !this.auth) { throw new Error("Missing options") } - this.versionLocation = path.join(this.paths.versions, this.version); - this.instanceLocation = path.join(this.paths.instances, this.version); + this.versionLocation = path.join(this.paths.versions!, this.version); + this.instanceLocation = path.join(this.paths.instances!, this.version); this.versionManifest = await core.version.getVersionManifest( this.version, @@ -95,8 +101,8 @@ export class Instance extends EventEmitter { await this.initialize(); this.ready = true; await core.version.downloadJar( - this.versionManifest, - this.versionLocation, + this.versionManifest!, + this.versionLocation!, ); } catch (original) { const error = new InstallError("installInit", original); @@ -105,8 +111,8 @@ export class Instance extends EventEmitter { try { const librariesDownloader = await core.LibrariesDownloader( - this.paths.libraries, - this.versionManifest, + this.paths!.libraries!, + this.versionManifest!, ); librariesDownloader.on("completed", () => { @@ -127,8 +133,8 @@ export class Instance extends EventEmitter { try { const assetsDownloader = await core.AssetsDownloader( - this.paths.assets, - this.versionManifest, + this.paths!.assets!, + this.versionManifest!, ); assetsDownloader.on("completed", () => { this.emit( @@ -148,8 +154,8 @@ export class Instance extends EventEmitter { try { const nativesDownloader = await core.NativesDownloader( - path.join(this.instanceLocation, "natives"), - this.versionManifest, + path.join(this.instanceLocation!, "natives"), + this.versionManifest!, ); nativesDownloader.on("completed", () => { this.emit( @@ -167,7 +173,7 @@ export class Instance extends EventEmitter { error.throw(); } - const javaError = await checkJava(this.javaExecutable) + const javaError = await checkJava(this.javaExecutable!) if (javaError) { const error = new InstallError("java", javaError) error.throw() @@ -179,12 +185,12 @@ export class Instance extends EventEmitter { case "forge": case "neoforge": await installForge( - this.version, + this.version!, this.modloader, - this.javaExecutable, - this.paths.root, - this.paths.libraries, - this.paths.versions, + this.javaExecutable!, + this.paths!.root, + this.paths!.libraries!, + this.paths!.versions!, ); break; default: @@ -197,7 +203,7 @@ export class Instance extends EventEmitter { } } - async launch() { + async launch(): Promise { try { if (!this.ready) { try { @@ -212,14 +218,14 @@ export class Instance extends EventEmitter { case "forge": { const forgeVersionManifest = await readJson( path.join( - this.paths.versions, + this.paths!.versions!, `${this.version}-${this.modloader.name}-${this.modloader.version}`, `${this.version}-${this.modloader.name}-${this.modloader.version}.json`, ), ); this.versionManifest = mergeManifests( - this.versionManifest, + this.versionManifest!, forgeVersionManifest, ); break; @@ -228,14 +234,14 @@ export class Instance extends EventEmitter { { const neoForgeVersionManifest = await readJson( path.join( - this.paths.versions, + this.paths!.versions!, `${this.modloader.name}-${this.modloader.version}`, `${this.modloader.name}-${this.modloader.version}.json`, ), ); this.versionManifest = mergeManifests( - this.versionManifest, + this.versionManifest!, neoForgeVersionManifest, ); } @@ -244,33 +250,34 @@ export class Instance extends EventEmitter { } const args = await core.arguments.generateLaunchArguments( - this.versionManifest, - this.javaExecutable, - this.instanceLocation, - this.paths.libraries, - this.paths.assets, - this.versionLocation, - this.auth, - { customGameArgs: this.args.game, customJvmArgs: this.args.java }, + this.versionManifest!, + this.javaExecutable!, + this.instanceLocation!, + this.paths!.libraries!, + this.paths!.assets!, + this.versionLocation!, + this.auth!, + this.args, + this.ram ); - const process = core.launch(args, this.instanceLocation); + const process = core.launch(args, this.instanceLocation!); return process; } catch (original) { const error = new LaunchError( { - version: this.version, - auth: this.auth, - paths: this.paths, - customGameArgs: this.args.game, - customJvmArgs: this.args.java, - versionManifest: this.versionManifest, - modloader: this.modloader, + version: this.version!, + auth: this.auth!, + paths: this.paths!, + customGameArgs: this.args.game!, + customJvmArgs: this.args.java!, + versionManifest: this.versionManifest!, + modloader: this.modloader!, }, - original, + original as Error, ); - error.throw(); + throw error.throw(); } } } diff --git a/src/launcher/java.ts b/src/launcher/java.ts index 1b5eca7..c891261 100644 --- a/src/launcher/java.ts +++ b/src/launcher/java.ts @@ -9,7 +9,7 @@ import { RuntimeComponent, RuntimeOS, } from "../utils/types"; -import { ensureDir, exists } from "../utils/fs"; +import { ensureDir } from "../utils/fs"; import { EventEmitter } from "stream"; import { exec } from "child_process"; import { arch, os } from "../utils/systemInfo"; @@ -91,9 +91,9 @@ async function JavaDownloader( const element = javaRuntimeManifest[key]; if (element.type === "file") { files.push({ - url: element.downloads.raw.url, + url: element.downloads!.raw.url, path: path.join(componentDestination, key), - size: element.downloads.raw.size, + size: element.downloads!.raw.size, }); } else if (element.type === "directory") { await ensureDir(path.join(componentDestination, key), true); diff --git a/src/utils/errors.ts b/src/utils/errors.ts index 0874e68..a9fcae7 100644 --- a/src/utils/errors.ts +++ b/src/utils/errors.ts @@ -5,10 +5,10 @@ export class InstallError extends Error { step: string; original: Error; moreInfo?: string; - constructor(step: string, original: Error, moreInfo?: string) { + constructor(step: string, original: unknown, moreInfo?: string) { super(); this.step = step; - this.original = original; + this.original = original as Error; this.moreInfo = moreInfo; } diff --git a/src/utils/fetch.ts b/src/utils/fetch.ts index cb924b0..7626ae4 100644 --- a/src/utils/fetch.ts +++ b/src/utils/fetch.ts @@ -15,13 +15,13 @@ export async function downloadFile(file: PoolFile, overwrite = true) { await ensureDir(path.dirname(file.path), true); const res = await fetch(file.url); - if (!res.ok) + if (!res.ok || !res.body) throw new Error( `Failed to download ${file.url}: ${res.status} ${res.statusText}`, ); await pipeline(res.body, fs.createWriteStream(file.path)); } catch (error) { - if (error.name !== "AbortError") throw error; + if (error instanceof Error && error.name !== "AbortError") throw error; return; } } @@ -33,29 +33,34 @@ export class DownloadPool extends PQueue { doneSize = 0; totalSize = 0; elements: PoolFile[]; - cleanup: () => Promise; + cleanup?: () => Promise; overwrite: boolean; constructor( elements: PoolFile[], options: PoolOptions = { pQueueOptions: {}, overwrite: true }, ) { + //@ts-expect-error can't figure out what to put as arguments of PQueueOptions super(options.pQueueOptions); this.elements = elements; this.total = this.elements.length; this.cleanup = options.cleanup; - this.overwrite = options.overwrite; + if (options.overwrite) { + this.overwrite = true + } else { + this.overwrite = false + } } async run() { for (const element of this.elements) { - this.totalSize += element.size; + this.totalSize += element.size!; this.add(async () => { await downloadFile(element, this.overwrite); // update status here to ensure that it is run before "completed" events listeners this.done++; - this.doneSize += element.size; + this.doneSize += element.size!; }); } @@ -64,4 +69,4 @@ export class DownloadPool extends PQueue { await this.cleanup(); } } -} +} \ No newline at end of file diff --git a/src/utils/rules.ts b/src/utils/rules.ts index 2cdb1ae..3b4b536 100644 --- a/src/utils/rules.ts +++ b/src/utils/rules.ts @@ -33,7 +33,7 @@ export function isNeeded(element: Library | Argument): boolean { return true; } else return false; } else { - if (rule.os.name === os()) { + if (rule.os!.name === os()) { return false; } else return true; } diff --git a/src/utils/types.ts b/src/utils/types.ts index 22e3a9f..20ca5f1 100644 --- a/src/utils/types.ts +++ b/src/utils/types.ts @@ -83,7 +83,7 @@ export type Native = { url: string; }; -type NativeOS = "natives-linux" | "natives-windows" | "natives-macos"; +type NativeOS = "natives-linux" | "natives-windows" | "natives-macos" | "natives-osx"; type Rules = { action: "allow" | "disallow"; @@ -217,6 +217,7 @@ export type LauncherProfiles = { }; export type PoolOptions = { + //@ts-expect-error can't figure out what to put as arguments of PQueueOptions pQueueOptions: PQueueOptions; cleanup?: () => Promise; overwrite?: boolean; diff --git a/src/utils/unzip.ts b/src/utils/unzip.ts index a1ec4cc..771df7c 100644 --- a/src/utils/unzip.ts +++ b/src/utils/unzip.ts @@ -53,13 +53,13 @@ export class DownloadAndUnzipPool extends DownloadPool { async run() { for (const element of this.elements) { - this.totalSize += element.size; + this.totalSize += element.size!; this.add(async () => { await this.downloadAndUnzip(element, this.filters, this.unzipMode); // update status here to ensure that it is run before "completed" events listeners this.done++; - this.doneSize += element.size; + this.doneSize += element.size!; }); } diff --git a/tsconfig.json b/tsconfig.json index ff9e80c..f5ffc10 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,5 +5,6 @@ "target": "esnext", "moduleResolution": "bundler", "noEmit": true, + "strict": true } }