launch-unity 0.6.2 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/launch.js +63 -7
  2. package/package.json +1 -1
package/dist/launch.js CHANGED
@@ -25,12 +25,17 @@ function parseArgs(argv) {
25
25
  const unityArgs = doubleDashIndex >= 0 ? args.slice(doubleDashIndex + 1) : [];
26
26
  const positionals = [];
27
27
  let maxDepth = 3; // default 3; -1 means unlimited
28
+ let restart = false;
28
29
  for (let i = 0; i < cliArgs.length; i++) {
29
30
  const arg = cliArgs[i] ?? "";
30
31
  if (arg === "--help" || arg === "-h") {
31
32
  printHelp();
32
33
  process.exit(0);
33
34
  }
35
+ if (arg === "-r" || arg === "--restart") {
36
+ restart = true;
37
+ continue;
38
+ }
34
39
  if (arg.startsWith("--max-depth")) {
35
40
  const parts = arg.split("=");
36
41
  if (parts.length === 2) {
@@ -79,7 +84,7 @@ function parseArgs(argv) {
79
84
  projectPath = resolve(positionals[0] ?? "");
80
85
  platform = String(positionals[1] ?? "");
81
86
  }
82
- const options = { unityArgs, searchMaxDepth: maxDepth };
87
+ const options = { unityArgs, searchMaxDepth: maxDepth, restart };
83
88
  if (projectPath !== undefined) {
84
89
  options.projectPath = projectPath;
85
90
  }
@@ -104,6 +109,7 @@ Forwarding:
104
109
 
105
110
  Flags:
106
111
  -h, --help Show this help message
112
+ -r, --restart Kill running Unity and restart
107
113
  --max-depth <N> Search depth when PROJECT_PATH is omitted (default 3, -1 unlimited)
108
114
  `;
109
115
  process.stdout.write(help);
@@ -386,6 +392,51 @@ async function handleStaleLockfile(projectPath) {
386
392
  console.warn(`Failed to delete UnityLockfile: ${message}`);
387
393
  }
388
394
  }
395
+ const KILL_POLL_INTERVAL_MS = 100;
396
+ const KILL_TIMEOUT_MS = 10000;
397
+ function isProcessAlive(pid) {
398
+ try {
399
+ process.kill(pid, 0);
400
+ return true;
401
+ }
402
+ catch {
403
+ return false;
404
+ }
405
+ }
406
+ function killProcess(pid) {
407
+ try {
408
+ process.kill(pid, "SIGKILL");
409
+ }
410
+ catch {
411
+ // Process already exited
412
+ }
413
+ }
414
+ async function waitForProcessExit(pid) {
415
+ const start = Date.now();
416
+ while (Date.now() - start < KILL_TIMEOUT_MS) {
417
+ if (!isProcessAlive(pid)) {
418
+ return true;
419
+ }
420
+ await new Promise((resolve) => setTimeout(resolve, KILL_POLL_INTERVAL_MS));
421
+ }
422
+ return false;
423
+ }
424
+ async function killRunningUnity(projectPath) {
425
+ const processInfo = await findRunningUnityProcess(projectPath);
426
+ if (!processInfo) {
427
+ console.log("No running Unity process found for this project.");
428
+ return;
429
+ }
430
+ const pid = processInfo.pid;
431
+ console.log(`Killing Unity (PID: ${pid})...`);
432
+ killProcess(pid);
433
+ const exited = await waitForProcessExit(pid);
434
+ if (!exited) {
435
+ console.error(`Error: Failed to kill Unity (PID: ${pid}) within ${KILL_TIMEOUT_MS / 1000}s.`);
436
+ process.exit(1);
437
+ }
438
+ console.log("Unity killed.");
439
+ }
389
440
  function hasBuildTargetArg(unityArgs) {
390
441
  for (const arg of unityArgs) {
391
442
  if (arg === "-buildTarget") {
@@ -526,12 +577,17 @@ async function main() {
526
577
  resolvedProjectPath = found;
527
578
  }
528
579
  ensureProjectPath(resolvedProjectPath);
529
- const runningProcess = await findRunningUnityProcess(resolvedProjectPath);
530
- if (runningProcess) {
531
- console.log(`Unity process already running for project: ${resolvedProjectPath} (PID: ${runningProcess.pid})`);
532
- await focusUnityProcess(runningProcess.pid);
533
- process.exit(0);
534
- return;
580
+ if (options.restart) {
581
+ await killRunningUnity(resolvedProjectPath);
582
+ }
583
+ else {
584
+ const runningProcess = await findRunningUnityProcess(resolvedProjectPath);
585
+ if (runningProcess) {
586
+ console.log(`Unity process already running for project: ${resolvedProjectPath} (PID: ${runningProcess.pid})`);
587
+ await focusUnityProcess(runningProcess.pid);
588
+ process.exit(0);
589
+ return;
590
+ }
535
591
  }
536
592
  await handleStaleLockfile(resolvedProjectPath);
537
593
  const resolved = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "launch-unity",
3
- "version": "0.6.2",
3
+ "version": "0.7.0",
4
4
  "description": "Open a Unity project with the matching Editor version (macOS/Windows)",
5
5
  "type": "module",
6
6
  "main": "dist/launch.js",