Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362/** Container work-tree preparation: rsync source, recreate dependency symlink farms from the baked layer. Ported from the proven dev-script-mutation-test worktree module; the mechanics (excluding heavyweight artifacts, copying symlink farms without following them, sharing the pnpm store by symlink) encode hard-won container lessons and are not Stryker-shaped. @example ```ts await prepareWorkTree(); ``` */
import type { Dirent, } from 'node:fs';import { access, mkdir, readdir, symlink, writeFile,} from 'node:fs/promises';import { join, } from 'node:path';
import spawn from 'nano-spawn';
import { tagged, } from '@monochromatic-dev/module-logger/ts';import type { ForeignBorrowed, } from '@monochromatic-dev/ownership-marker-foreign-borrowed/ts';
import { BAKED_ROOT, SOURCE_MOUNT, WORK_MOUNT,} from '../mounts.ts';
/** Module logger for work-tree preparation. */const l = tagged({ tag: 'mutation-test-container', },);
/** Node dependency directory name. */const NODE_MODULES = 'node_modules';
/** pnpm virtual store directory name inside node_modules. */const PNPM_STORE = '.pnpm';
/** Rsync exclude patterns for generated or heavyweight repository artifacts. */export const RSYNC_EXCLUDES: readonly string[] = [ '**/node_modules', '**/dist', '**/.cache', '**/.git', '**/target', '**/output', '**/corpus',];
/** Copies current repository source into the writable work tree. @example ```ts await rsyncSourceToWorkTree(); ``` */export async function rsyncSourceToWorkTree(): Promise<void> { await spawn( 'rsync', [ '--archive', '--delete', ...RSYNC_EXCLUDES.flatMap(function excludeArgs(pattern,): readonly string[] { return [ '--exclude', pattern, ]; },), `${SOURCE_MOUNT}/`, `${WORK_MOUNT}/`, ], { stdout: 'inherit', stderr: 'inherit', }, );}
/** Returns whether a path exists. @param path - Path to probe. @returns True when path is accessible. @example ```ts await pathExists('/tmp'); ``` */async function pathExists(path: string,): Promise<boolean> { try { await access(path,); return true; } catch (error) { tagged({ tag: pathExists.name, l, },) .debug(`path probe negative for ${path}: ${String(error,)}`,); return false; }}
/** Recreates root node_modules from the baked layer, sharing the pnpm store by symlink instead of copying it. @example ```ts await recreateRootNodeModules(); ``` */async function recreateRootNodeModules(): Promise<void> { /** Root node_modules directory in writable work tree. */ const workRootNodeModules = join( WORK_MOUNT, NODE_MODULES, ); await mkdir( workRootNodeModules, { recursive: true, }, ); await spawn( 'rsync', [ '--archive', '--delete', '--exclude', PNPM_STORE, `${join( BAKED_ROOT, NODE_MODULES, )}/`, `${workRootNodeModules}/`, ], { stdout: 'inherit', stderr: 'inherit', }, ); await symlink( join( BAKED_ROOT, NODE_MODULES, PNPM_STORE, ), join( workRootNodeModules, PNPM_STORE, ), );}
/** Copies one package-local node_modules symlink farm from the baked layer. @param options - Package category and package name. @example ```ts await symlinkPackageNodeModules({ category: 'module', packageName: 'fs-path' }); ``` */async function symlinkPackageNodeModules(options: { readonly category: string; readonly packageName: string;},): Promise<void> { /** Baked package-local node_modules path. */ const bakedNodeModules = join( BAKED_ROOT, 'package', options.category, options.packageName, NODE_MODULES, );
if (!await pathExists(bakedNodeModules,)) return;
/** Writable work-tree package node_modules path. */ const workNodeModules = join( WORK_MOUNT, 'package', options.category, options.packageName, NODE_MODULES, ); await mkdir( workNodeModules, { recursive: true, }, ); await spawn( 'rsync', [ '--archive', '--delete', `${bakedNodeModules}/`, `${workNodeModules}/`, ], { stdout: 'inherit', stderr: 'inherit', }, );}
/** Recreates every package-local node_modules symlink farm. @example ```ts await symlinkWorkspacePackageNodeModules(); ``` */async function symlinkWorkspacePackageNodeModules(): Promise<void> { /** Workspace package category directories under baked tree. */ const packageCategories = await readdir( join( BAKED_ROOT, 'package', ), { withFileTypes: true, }, ); await Promise.all(packageCategories .filter(function isDirectory(category: ForeignBorrowed<Dirent>,): boolean { return category.isDirectory(); },) .map(async function symlinkCategory(category: ForeignBorrowed<Dirent>,): Promise<void> { /** Package directories under current category. */ const packages = await readdir( join( BAKED_ROOT, 'package', category.name, ), { withFileTypes: true, }, ); await Promise.all(packages .filter(function isDirectory(packageEntry: ForeignBorrowed<Dirent>,): boolean { return packageEntry.isDirectory(); },) .map(async function symlinkPackage(packageEntry: ForeignBorrowed<Dirent>,): Promise<void> { await symlinkPackageNodeModules({ category: category.name, packageName: packageEntry.name, },); },),); },),);}
/** Regular files of the smallest `.git` directory fs-path's git marker validator accepts, relative path to content: a symbolic HEAD is the only file it reads. */export const GIT_MARKER_FILES: Readonly<Record<string, string>> = { '.git/HEAD': 'ref: refs/heads/main\n',};
/** Directories of that smallest `.git`: the validator requires `objects` and `refs` under the common directory, which is `.git` itself when no `commondir` file redirects it. */export const GIT_MARKER_DIRECTORIES: readonly string[] = [ '.git/objects', '.git/refs',];
/** Writes the smallest `.git` directory that fs-path's `GIT_REPOSITORY` marker accepts under `dir`. The work tree is a copy of a git repository with `.git` excluded from the rsync, and package tests may legitimately walk up to a repository root; an empty `.git` directory is rejected by the validator (HEAD, objects, and refs are required), so those tests would otherwise walk past the work tree. @param dir - work tree root receiving the marker @example ```ts await materialiseGitMarker({ dir: WORK_MOUNT }); ``` */export async function materialiseGitMarker({ dir,}: { readonly dir: string;},): Promise<void> { await Promise.all(GIT_MARKER_DIRECTORIES.map(async function makeMarkerDirectory( relative: string, ): Promise<void> { await mkdir( join( dir, relative, ), { recursive: true, }, ); },),); await Promise.all(Object.entries(GIT_MARKER_FILES,) .map(function writeMarkerFile( [relative, content,]: readonly [ string, string, ], ): Promise<void> { return writeFile( join( dir, relative, ), content, ); },),); l.debug(`materialised git marker under ${dir}`,);}
/** Prepares the full work tree: source rsync plus dependency farms, then the `.git` marker from {@link materialiseGitMarker}. @example ```ts await prepareWorkTree(); ``` */export async function prepareWorkTree(): Promise<void> { await rsyncSourceToWorkTree(); await recreateRootNodeModules(); await symlinkWorkspacePackageNodeModules(); await materialiseGitMarker({ dir: WORK_MOUNT, },);}