Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "java-runner-client",
"version": "2.1.4",
"version": "2.1.5",
"description": "Run and manage Java processes with profiles, console I/O, and system tray support",
"main": "dist/main/main.js",
"scripts": {
Expand Down
105 changes: 95 additions & 10 deletions src/main/ProcessManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ import {
} from './shared/types/Process.types';
import { Profile } from './shared/types/Profile.types';
import { ProcessIPC } from './ipc/Process.ipc';
import { DEFAULT_JAR_RESOLUTION } from './shared/config/JarResolution.config';

// Inline resolution to avoid circular IPC dependency
import fs from 'fs';
import { patternToRegex } from './shared/config/JarResolution.config';
import type { JarResolutionConfig } from './shared/types/JarResolution.types';
import { ProfileIPC } from './ipc/Profile.ipc';
import { saveProfile } from './Store';

const SELF_PROCESS_NAME = 'Java Client Runner';

Expand All @@ -34,6 +42,83 @@ interface ManagedProcess {
intentionallyStopped: boolean;
}

function parseVersion(str: string): number[] {
return str
.split(/[.\-_]/)
.map((p) => parseInt(p, 10))
.filter((n) => !isNaN(n));
}

function compareVersionArrays(a: number[], b: number[]): number {
const len = Math.max(a.length, b.length);
for (let i = 0; i < len; i++) {
const diff = (b[i] ?? 0) - (a[i] ?? 0);
if (diff !== 0) return diff;
}
return 0;
}

function resolveJarPath(profile: Profile): { jarPath: string; error?: string } {
const res = profile.jarResolution ?? DEFAULT_JAR_RESOLUTION;

if (!res.enabled) {
return { jarPath: profile.jarPath };
}

if (!res.baseDir) return { jarPath: '', error: 'Dynamic JAR: no base directory set.' };

let entries: fs.Dirent[];
try {
entries = fs.readdirSync(res.baseDir, { withFileTypes: true });
} catch {
return { jarPath: '', error: `Dynamic JAR: cannot read directory "${res.baseDir}".` };
}

const jars = entries.filter((e) => e.isFile() && e.name.endsWith('.jar'));

let matchRegex: RegExp;
if (res.strategy === 'regex' && res.regexOverride?.trim()) {
try {
matchRegex = new RegExp(res.regexOverride.trim(), 'i');
} catch {
return { jarPath: '', error: 'Dynamic JAR: invalid regular expression.' };
}
} else {
matchRegex = patternToRegex(res.pattern);
}

const matched = jars.filter((e) => matchRegex.test(e.name));
if (matched.length === 0) {
return { jarPath: '', error: 'Dynamic JAR: no files matched the pattern.' };
}

let chosen: string;

if (res.strategy === 'latest-modified') {
const withMtime = matched.map((e) => {
const full = path.join(res.baseDir, e.name);
try {
return { name: e.name, mtime: fs.statSync(full).mtimeMs };
} catch {
return { name: e.name, mtime: 0 };
}
});
chosen = withMtime.sort((a, b) => b.mtime - a.mtime)[0].name;
} else if (res.strategy === 'regex') {
chosen = matched[0].name;
} else {
const versionRegex = patternToRegex(res.pattern);
const withVersions = matched.map((e) => {
const m = versionRegex.exec(e.name);
return { name: e.name, version: parseVersion(m?.[1] ?? '') };
});
withVersions.sort((a, b) => compareVersionArrays(a.version, b.version));
chosen = withVersions[0].name;
}

return { jarPath: path.join(res.baseDir, chosen) };
}

class ProcessManager {
private processes = new Map<string, ManagedProcess>();
private activityLog: ProcessLogEntry[] = [];
Expand All @@ -48,27 +133,30 @@ class ProcessManager {
this.window = win;
}

private buildArgs(profile: Profile): { cmd: string; args: string[] } {
private buildArgs(profile: Profile, resolvedJarPath: string): { cmd: string; args: string[] } {
const cmd = profile.javaPath || 'java';
const args: string[] = [];
for (const a of profile.jvmArgs) if (a.enabled && a.value.trim()) args.push(a.value.trim());
for (const p of profile.systemProperties)
if (p.enabled && p.key.trim())
args.push(p.value.trim() ? `-D${p.key.trim()}=${p.value.trim()}` : `-D${p.key.trim()}`);
args.push('-jar', profile.jarPath);
args.push('-jar', resolvedJarPath);
for (const a of profile.programArgs) if (a.enabled && a.value.trim()) args.push(a.value.trim());
return { cmd, args };
}

start(profile: Profile): { ok: boolean; error?: string } {
if (this.processes.has(profile.id)) return { ok: false, error: 'Process already running' };
if (!profile.jarPath) return { ok: false, error: 'No JAR file specified' };

const { jarPath, error: resolveError } = resolveJarPath(profile);
if (resolveError) return { ok: false, error: resolveError };
if (!jarPath) return { ok: false, error: 'No JAR file specified' };

this.cancelRestartTimer(profile.id);
this.profileSnapshots.set(profile.id, profile);

const { cmd, args } = this.buildArgs(profile);
const cwd = profile.workingDir || path.dirname(profile.jarPath);
const { cmd, args } = this.buildArgs(profile, jarPath);
const cwd = profile.workingDir || path.dirname(jarPath);

this.pushSystem('start', profile.id, 'pending', `Starting: ${cmd} ${args.join(' ')}`);
this.pushSystem('info-workdir', profile.id, 'pending', `Working dir: ${cwd}`);
Expand All @@ -92,7 +180,7 @@ class ProcessManager {
process: proc,
profileId: profile.id,
profileName: profile.name,
jarPath: profile.jarPath,
jarPath,
startedAt: Date.now(),
intentionallyStopped: false,
};
Expand All @@ -110,7 +198,7 @@ class ProcessManager {
id: uuidv4(),
profileId: profile.id,
profileName: profile.name,
jarPath: profile.jarPath,
jarPath,
pid,
startedAt: managed.startedAt,
};
Expand Down Expand Up @@ -253,8 +341,6 @@ class ProcessManager {
this.activityLog = [];
}

// ── Process Scanner ──────────────────────────────────────────────────────────

private isProtected(name: string, cmd: string): boolean {
return PROTECTED_PROCESS_NAMES.some(
(n) =>
Expand Down Expand Up @@ -416,7 +502,6 @@ class ProcessManager {
}
}

// Only kills non-protected java processes
killAllJava(): { ok: boolean; killed: number } {
const procs = this.scanAllProcesses().filter((p) => p.isJava && !p.protected);
let killed = 0;
Expand Down
135 changes: 0 additions & 135 deletions src/main/RestAPI.routes.ts

This file was deleted.

24 changes: 3 additions & 21 deletions src/main/RestAPI.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,7 @@
import http from 'http';
import { routes } from './RestAPI.routes';
import { routeConfig } from './shared/config/API.config';
import { getSettings } from './Store';
import { REST_API_CONFIG } from './shared/config/API.config';

type Params = Record<string, string>;

// ─── Types ────────────────────────────────────────────────────────────────────

type CompiledRoute = {
method: string;
path: string;
pattern: RegExp;
keys: string[];
handler: (ctx: {
req: http.IncomingMessage;
res: http.ServerResponse;
params: Params;
body: unknown;
}) => void | Promise<void>;
};
import { routes } from './rest-api/_index';
import { REST_API_CONFIG, routeConfig, RouteKey } from './shared/config/API.config';
import { CompiledRoute, Params } from './shared/types/RestAPI.types';

// ─── Helpers ──────────────────────────────────────────────────────────────────

Expand Down
Loading
Loading