All files / cli TargetCatalogFile.ts

100% Statements 3/3
100% Branches 0/0
100% Functions 2/2
100% Lines 3/3

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                                                      2565x 2565x               2880x          
/**
 * ADR-049: the target catalog shipped with this compiler.
 *
 * Found by `Installation`, which 1.1 Discover owns. Read through the port the run is handed, like every other file
 * (#1653): a port models the whole filesystem, the compiler's installation
 * included, so a test double that stands in for one seeds the catalog, as
 * `MockFileSystem` does.
 *
 * Read and validated on each call. #1444 moved this module under a pass root,
 * where #1452 box 4 forbids mutable state, and it held a per-port `WeakMap`
 * cache written on first use. The cache is gone rather than moved, since moving
 * it would only put the same state somewhere no guard scans. A cold parse costs
 * about 2.6 ms (measured 2026-10-02, 20 cold reads), and a run makes one.
 */
import Installation from "../cnx/PARSE/1-Discover/Installation";
import TargetCatalogParser from "../cnx/PARSE/2-Parse/TargetCatalogParser";
import TargetDescriptions from "../cnx/PARSE/2-Parse/TargetDescriptions";
import type ITargetDescription from "../cnx/types/ITargetDescription";
import type IFileSystem from "../cnx/types/IFileSystem";
 
class TargetCatalogFile {
  /**
   * Every target name, aliases included, to the description it denotes.
   * The catalog is an installation file, so a port that models the filesystem
   * models the installation too.
   */
  static targets(fs: IFileSystem): ReadonlyMap<string, ITargetDescription> {
    const path = TargetCatalogFile.locate(fs);
    return TargetDescriptions.catalog(
      TargetCatalogParser.parse(fs.readFile(path)),
      path,
    );
  }
 
  /** The catalog's path; throws if the installation has none */
  static locate(fs: IFileSystem): string {
    return Installation.catalog(fs);
  }
}
 
export default TargetCatalogFile;