Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

2.5 KB
Raw Blame
import { readdirSync } from "node:fs";
import { posix } from "node:path";

export const MAC_HOPPER_APPLICATION_DIRECTORY = "/Applications";
export const MAC_HOPPER_APPLICATION_NAME = "Hopper Disassembler.app";
export const MAC_HOPPER_USER_APPLICATIONS_DIRECTORY = "Applications";

export const MAC_HOPPER_SYSTEM_LAUNCHER = posix.join(
  MAC_HOPPER_APPLICATION_DIRECTORY,
  MAC_HOPPER_APPLICATION_NAME,
  "Contents/MacOS/hopper",
);

export const macHopperUserApplicationPath = (homeDirectory: string): string =>
  posix.join(
    homeDirectory,
    MAC_HOPPER_USER_APPLICATIONS_DIRECTORY,
    MAC_HOPPER_APPLICATION_NAME,
  );

export const macHopperApplicationRoots = (
  homeDirectory: string,
): readonly string[] => [
  MAC_HOPPER_APPLICATION_DIRECTORY,
  posix.join(homeDirectory, MAC_HOPPER_USER_APPLICATIONS_DIRECTORY),
];

export const macHopperCanonicalLauncherCandidates = (
  homeDirectory: string,
): readonly string[] => [
  MAC_HOPPER_SYSTEM_LAUNCHER,
  posix.join(
    macHopperUserApplicationPath(homeDirectory),
    "Contents/MacOS/hopper",
  ),
];

/** Build deterministic candidates from the discovered app bundle names. */
export const macHopperRenamedLauncherCandidates = (
  homeDirectory: string,
  bundleNamesByRoot: ReadonlyMap<string, readonly string[]>,
): readonly string[] =>
  macHopperApplicationRoots(homeDirectory).flatMap((root) =>
    [...new Set(bundleNamesByRoot.get(root) ?? [])]
      .filter(
        (name) =>
          /^Hopper.*\.app$/iu.test(name) &&
          name.toLocaleLowerCase("en-US") !==
            MAC_HOPPER_APPLICATION_NAME.toLocaleLowerCase("en-US"),
      )
      .sort()
      .map((name) => posix.join(root, name, "Contents/MacOS/hopper")),
  );

const localBundleNames = (root: string): readonly string[] => {
  try {
    return readdirSync(root, { withFileTypes: true })
      .filter(
        (entry) => entry.isDirectory() && /^Hopper.*\.app$/iu.test(entry.name),
      )
      .map(({ name }) => name);
  } catch (cause: unknown) {
    // Missing or unreadable optional app directories contain no candidates.
    void cause;
    return [];
  }
};

/** Yield canonical paths first; enumerate renamed bundles only if needed. */
export function* macHopperLauncherCandidates(
  homeDirectory: string,
): Generator<string> {
  yield* macHopperCanonicalLauncherCandidates(homeDirectory);
  for (const root of macHopperApplicationRoots(homeDirectory))
    yield* macHopperRenamedLauncherCandidates(
      homeDirectory,
      new Map([[root, localBundleNames(root)]]),
    );
}