syntaxai/tdd.md · main · scripts / measure-graph-depth.ts

measure-graph-depth.ts 64 lines · 1894 bytes raw
#!/usr/bin/env bun
// measure-graph-depth — CLI for the §5 polyglot graphDepth metric.
// Given a path to a checked-out Go module or Rust Cargo workspace,
// emit the measured longest dependency chain as JSON to stdout.
//
// Usage:
//   bun scripts/measure-graph-depth.ts <repo-path> --lang go
//   bun scripts/measure-graph-depth.ts <repo-path> --lang rust
//
// Module-granularity per language: Go = package directory (multiple
// .go files in one directory share imports); Rust = crate (Cargo
// workspace member). See /sama/v2 §5 (operational) and the source
// comment at the top of src/b32_graph_depth_polyglot.ts.

import { computeGoGraphDepth } from "../src/c14_go_graph_depth.ts";
import { computeRustGraphDepth } from "../src/c14_rust_graph_depth.ts";

const args = process.argv.slice(2);

const usage = (): never => {
  console.error(
    "Usage: bun scripts/measure-graph-depth.ts <repo-path> --lang go|rust",
  );
  process.exit(2);
};

if (args.length < 3) usage();

const repoPath = args[0]!;
let lang: "go" | "rust" | null = null;

for (let i = 1; i < args.length; i++) {
  const a = args[i];
  if (a === "--lang") {
    const v = args[++i];
    if (v !== "go" && v !== "rust") {
      console.error(`--lang must be "go" or "rust", got: ${v}`);
      process.exit(2);
    }
    lang = v;
  } else {
    console.error(`unknown argument: ${a}`);
    usage();
  }
}

if (lang === null) usage();

const result =
  lang === "go" ? computeGoGraphDepth(repoPath) : computeRustGraphDepth(repoPath);

const output: Record<string, unknown> = {
  language: result.language,
  repoPath,
  ...(lang === "go"
    ? { modulePath: (result as { modulePath: string }).modulePath }
    : { workspaceName: (result as { workspaceName: string }).workspaceName }),
  nodeCount: result.nodeCount,
  edgeCount: result.edgeCount,
  depth: result.depth,
};

console.log(JSON.stringify(output, null, 2));