Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

2.9 KB
Raw Blame
// Bundles src/register.ts into the plugin's hooks module.
//
//   node build.mjs           write plugins/claude-code/hooks/lens/lens.js
//   node build.mjs --check   exit 1 when the committed bundle has drifted
//
// The mod runtime loads only files inside the plugin dir, so the shared
// packages are inlined. Never minified: the engine's static check reads `$`
// calls by name. No keepNames either: its helper sets each function's name
// through Object.defineProperty, reflective code the plugin directory holds
// for review, and nothing in Lens reads a function's name. Output is
// deterministic (relative paths, no legal comments, LF) so a rebuild on any
// OS matches the committed file.

import { readFile, writeFile, mkdir } from "node:fs/promises";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { build } from "esbuild";

const here = dirname(fileURLToPath(import.meta.url));
const repoRoot = resolve(here, "../..");
const outFile = resolve(repoRoot, "plugins/claude-code/hooks/lens/lens.js");
const check = process.argv.includes("--check");

const result = await build({
  entryPoints: [resolve(here, "src/register.ts")],
  absWorkingDir: here,
  bundle: true,
  format: "esm",
  platform: "neutral",
  mainFields: ["module", "main"],
  minify: false,
  legalComments: "none",
  charset: "utf8",
  write: false,
  logLevel: "warning",
});

const banner = "// Generated by packages/claude-mod (npm run build:mod). Do not edit.\n";
const output = banner + result.outputFiles[0].text.replace(/\r\n/g, "\n");

// The plugin directory reads every `on` in the bundle as register's `on`: a
// variable or parameter of that name anywhere else fails its check. Ceiling:
// a regex, not a parse; it catches declarations and parameters, the usual way
// a source file brings the name back.
const onDeclared = [...output.matchAll(/\b(?:const|let|var|function)\s+on\b|[(,]\s*on\s*(?:[,)=]|:\s)/g)].filter(
  (m) => !output.startsWith("function register(on,", m.index - "function register".length),
);
if (onDeclared.length > 0) {
  const lines = onDeclared.map((m) => output.slice(0, m.index).split("\n").length);
  console.error(`lens.js declares "on" outside register (lines ${lines.join(", ")}). Rename it in the source.`);
  process.exit(1);
}

if (check) {
  let current = "";
  try {
    current = (await readFile(outFile, "utf8")).replace(/\r\n/g, "\n");
  } catch (err) {
    // A missing bundle is drift, reported below; any other read error fails loudly.
    if (err?.code !== "ENOENT") throw new Error(`cannot read ${outFile}`, { cause: err });
  }
  if (current !== output) {
    console.error(`${outFile} is out of date. Run: npm run build:mod -w @repowise-dev/claude-mod`);
    process.exit(1);
  }
  console.log("lens.js is up to date");
} else {
  await mkdir(dirname(outFile), { recursive: true });
  await writeFile(outFile, output);
  console.log(`wrote ${outFile} (${Buffer.byteLength(output)} bytes)`);
}