All files / src/lib/handheld run.ts

76.47% Statements 26/34
56.52% Branches 13/23
62.5% Functions 5/8
82.75% Lines 24/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 852x 2x 2x     2x         3x   3x 3x 3x 2x         2x       2x 2x                   2x 1x 1x                                   2x         2x   2x       2x 2x 2x     2x 2x                 2x      
import { spawn } from "child_process";
import { existsSync, readFileSync } from "fs";
import Path from "path";
import type { HandheldAction, HandheldConfig } from "shared/lib/handheld/types";
 
export function hardwareCommand(
  action: HandheldAction,
  config: HandheldConfig,
  output: string,
) {
  Iif (!["build", "load", "flash"].includes(action))
    throw new Error("Unknown handheld action");
  Iif (!config.python.trim()) throw new Error("Choose a Python executable");
  Iif (action === "build") return [];
  if (!config.port.trim()) throw new Error("Choose the board's USB UART port");
  const script = Path.join(
    config.fpga,
    "scripts",
    action === "flash" ? "gbafpga.py" : "load-studio-lcd.py",
  );
  Iif (!existsSync(script))
    throw new Error(
      "Select a GBA-FPGA checkout containing the hardware scripts",
    );
  const report = Path.join(output, "hardware.json");
  Iif (action === "load")
    return [
      script,
      "--port",
      config.port,
      "--firmware",
      Path.join(output, "firmware", "game.tang.bin"),
      "--report",
      report,
    ];
  if (!Number.isInteger(config.cableIndex) || config.cableIndex < 0)
    throw new Error("Cable index must be a non-negative integer");
  return [
    script,
    "flash",
    "studio_lcd",
    "--game",
    Path.join(output, "firmware"),
    "--port",
    config.port,
    "--cable-index",
    String(config.cableIndex),
    ...(config.location?.trim() ? ["--location", config.location.trim()] : []),
    "--report",
    report,
    ...(config.sram ? ["--sram"] : []),
    ...(config.gowin ? ["--tool", "gowin", "--gowin", config.gowin] : []),
  ];
}
 
export function runTool(
  executable: string,
  args: string[],
  log: (text: string) => void,
): Promise<void> {
  return new Promise((resolve, reject) => {
    // Argument arrays preserve paths with spaces; never invoke a shell.
    const child = spawn(executable, args, {
      windowsHide: true,
      env: { ...process.env, PYTHONUNBUFFERED: "1" },
    });
    child.stdout.on("data", (chunk) => log(chunk.toString()));
    child.stderr.on("data", (chunk) => log(chunk.toString()));
    child.on("error", (error) =>
      reject(new Error(`Could not start ${executable}: ${error.message}`)),
    );
    child.on("close", (code) =>
      code === 0
        ? resolve()
        : reject(
            new Error(`Hardware command failed (${code}). See the log above.`),
          ),
    );
  });
}
 
export function readHardwareReport(output: string) {
  return JSON.parse(readFileSync(Path.join(output, "hardware.json"), "utf8"));
}