linkModules

tcmts.cli.vitest.linkModules · #wzar3xij · term · runs on the server

uses node:fs

The test root's node_modules: everything in the cache (the pinned aliases generated modules import), plus the test tools under their own names, which Vitest's browser mode looks them up by.

import fs from "node:fs";
import path from "node:path";

export function linkModules(dir: string, cache: string, plain: Record<string, string>) {
  const link = (target: string, at: string) => {
    if (fs.lstatSync(at, { throwIfNoEntry: false })) {
      if (fs.lstatSync(at).isSymbolicLink() && fs.readlinkSync(at) === target) return;
      fs.rmSync(at, { recursive: true, force: true });
    }
    fs.mkdirSync(path.dirname(at), { recursive: true });
    fs.symlinkSync(target, at, "dir");
  };
  fs.mkdirSync(dir, { recursive: true });
  for (const name of fs.existsSync(cache) ? fs.readdirSync(cache) : []) if (name !== ".bin") link(path.join(cache, name), path.join(dir, name));
  for (const [name, target] of Object.entries(plain)) link(fs.realpathSync(target), path.join(dir, name));
}

used by