// Writes into a SERVED public dir (export/public) that never follow a link. // // In a git worktree the gitignored entries of export/public are symlinks into // the primary checkout (plans/tools/implementer-rules.md), and writeFile / cp // write THROUGH a link: a worktree's compose — e2e:2origin's `build:hub` — // wrote its empty hub pool into the primary's hub-sites.json, corpus.json, // llms.txt, robots.txt and _headers. Removing the path first (fs.rm reads it // with lstat) removes only the link, so the write lands in this checkout and // the link's target is untouched. Where the path is a real file — the primary // checkout, a docker build's /site/public — the result is what a plain write // gave: the same path with the new content. // // ownDir is the same rule for a directory a compose writes INTO: a linked // directory is replaced by an empty real one (its target untouched), a real one // is kept as it is, and a missing one is created. import { cp, lstat, mkdir, rm, unlink, writeFile } from "node:fs/promises"; export async function writePublicFile( file: string, data: string | Uint8Array, ): Promise { await rm(file, { force: true }); await writeFile(file, data); } export async function copyPublicFile(src: string, dest: string): Promise { await rm(dest, { force: true }); await cp(src, dest); } export async function ownDir(dir: string): Promise { try { if ((await lstat(dir)).isSymbolicLink()) await unlink(dir); } catch (err) { if ((err as NodeJS.ErrnoException).code !== "ENOENT") throw err; } await mkdir(dir, { recursive: true }); }