testMirrorPackageLayouts

tcmts.cli.flight.testMirrorPackageLayouts · #5y6a7gfk · term

import { mirrorPackage } from "#codebase/tcmts/cli/flight";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";

export const testMirrorPackageLayouts = () => {
  const cache = fs.mkdtempSync(path.join(os.tmpdir(), "tcm-mirror-"));
  try {
    const file = (p: string, text = "x") => {
      fs.mkdirSync(path.dirname(path.join(cache, p)), { recursive: true });
      fs.writeFileSync(path.join(cache, p), text);
    };
    const has = (p: string) => {
      if (!fs.existsSync(path.join(cache, p))) throw new Error(`no ${p}`);
    };
    // npm's flat node_modules: a package by its name (its own node_modules with it), a scoped one by both parts
    file("node_modules/npm__react__19/index.js");
    file("node_modules/npm__react__19/node_modules/nested/index.js");
    file("node_modules/@scope/pkg/index.js");
    mirrorPackage(cache, "npm__react__19");
    mirrorPackage(cache, "@scope/pkg");
    has("server_modules/npm__react__19/index.js");
    has("server_modules/npm__react__19/node_modules/nested/index.js");
    has("server_modules/@scope/pkg/index.js");
    // pnpm's store: an entry, and the entries its links point at beside it
    file("node_modules/.pnpm/a@1/node_modules/a/index.js");
    file("node_modules/.pnpm/b@1/node_modules/b/index.js");
    fs.symlinkSync(
      "../../b@1/node_modules/b",
      path.join(cache, "node_modules/.pnpm/a@1/node_modules/b"),
    );
    mirrorPackage(cache, ".pnpm/a@1");
    has("server_modules/.pnpm/a@1/node_modules/a/index.js");
    has("server_modules/.pnpm/a@1/node_modules/b/index.js");
    has("server_modules/.pnpm/b@1/node_modules/b/index.js");
  } finally {
    fs.rmSync(cache, { recursive: true, force: true });
  }
}

uses