All files / src/lib/compiler romUsage.ts

28% Statements 7/25
0% Branches 0/11
0% Functions 0/5
28% Lines 7/25

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 85 86 87 88 89 90 911x 1x 1x 1x 1x                                                     1x                                                                                                                   1x  
import fs from "fs-extra";
import Path from "path";
import ensureBuildTools from "lib/compiler/ensureBuildTools";
import spawn from "lib/helpers/cli/spawn";
import l10n from "shared/lib/lang/l10n";
 
type RomUsageOptions = {
  buildRoot: string;
  tmpPath: string;
  progress: (msg: string) => void;
  warnings: (msg: string) => void;
};
 
export type UsageData = {
  banks: Array<{
    name: string;
    type: string;
    baseBankNum: string;
    isBanked: string;
    isMergedBank: string;
    rangeStart: string;
    rangeEnd: string;
    size: string;
    used: string;
    free: string;
    usedPercent: string;
    freePercent: string;
    miniGraph: string;
  }>;
};
 
const romUsage = async ({
  buildRoot = "/tmp",
  tmpPath = "/tmp",
  warnings = (_msg) => {},
  progress = (_msg) => {},
}: RomUsageOptions) => {
  const env = { ...process.env };
 
  const buildToolsPath = await ensureBuildTools(tmpPath);
  const buildToolsVersion = await fs.readFile(
    `${buildToolsPath}/tools_version`,
    "utf8"
  );
 
  env.PATH = [
    Path.join(buildToolsPath, "gbdk", "bin"),
    env.PATH ?? env.Path,
  ].join(Path.delimiter);
 
  env.GBDKDIR = `${buildToolsPath}/gbdk/`;
  env.GBS_TOOLS_VERSION = buildToolsVersion;
 
  const options = {
    cwd: buildRoot,
    env,
    shell: true,
  };
 
  const romusageCommand =
    process.platform === "win32"
      ? `"${buildToolsPath}\\gbdk\\bin\\romusage.exe"`
      : "romusage";
  const romusageArgs = [`${buildRoot}/build/rom/game.map`, `-g`, `-sH`, `-sJ`];
 
  let output = "";
  progress(`${l10n("COMPILER_ROMUSAGE")}...`);
  const { completed: romusageCompleted } = spawn(
    romusageCommand,
    romusageArgs,
    options,
    {
      onLog: (msg) => {
        output += msg;
      },
      onError: (msg) => {
        Iif (msg.indexOf("Romusage") > -1) {
          return;
        }
        warnings(msg);
      },
    }
  );
 
  await romusageCompleted;
 
  return JSON.parse(output) as UsageData;
};
 
export default romUsage;