writeMergeFiles

tcmts.cli.merge.writeMergeFiles · #7bnzy2ec · term · runs on the server

uses tcmts.cli.main.edit, which uses node:fs

The scratch files a merge kept apart leaves for settling it: each conflicted name's sides merged (as tcm edit writes them, a file named after it), and what stopped typechecking because of it, in one file (merge-fix.ts): only what failed itself, as what failed because something it uses did follows once that's fixed.

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { edit } from "#codebase/tcmts/cli/main";
import type { mergeLeft } from "#codebase/tcmts/cli/merge";
import { yellow } from "#codebase/tcmts/cli/style";

export function writeMergeFiles(
  cb: Codebase,
  left: ReturnType<typeof mergeLeft>,
  scratch: (f?: string, fallback?: string) => string,
) {
  const named = (jsx: boolean) => scratch(undefined, jsx ? "scratch.tsx" : "scratch.ts");
  for (const c of left.conflicts) {
    try {
      edit(cb, [c], named, true);
    } catch (e) {
      console.log(`  ${yellow(c)}: ${(e as Error).message}`);
    }
  }
  const roots = [
    ...new Set(
      left.failed
        .filter((f) => !/^depends on /.test(f.message) && !left.conflicts.includes(f.name))
        .map((f) => f.name),
    ),
  ];
  if (roots.length)
    try {
      edit(cb, roots, (jsx) => scratch(undefined, jsx ? "merge-fix.tsx" : "merge-fix.ts"), true);
    } catch (e) {
      console.log(`  ${yellow("to fix")}: ${(e as Error).message}`);
    }
}

uses

used by