import fs from 'node:fs'; import path from 'node:path'; import { execFileSync } from 'node:child_process'; // Fresh immutable trees can use Windows' parallel copier. Hash manifests and // archive verification remain authoritative; existing outputs are never reused. export function copyReleaseTree(source, destination) { if (process.platform !== 'win32' || !fs.statSync(source).isDirectory() || fs.existsSync(destination)) { fs.cpSync(source, destination, { recursive: true }); return; } const inspect = directory => { if (fs.lstatSync(directory).isSymbolicLink()) throw new Error('Release source cannot contain links: ' + directory); for (const entry of fs.readdirSync(directory, { withFileTypes: true })) { const file = path.join(directory, entry.name); if (entry.isSymbolicLink() || !entry.isDirectory() && !entry.isFile()) throw new Error('Release source must contain regular files: ' + file); if (entry.isDirectory()) inspect(file); } }; inspect(source); fs.mkdirSync(path.dirname(destination), { recursive: true }); try { execFileSync('robocopy.exe', [path.resolve(source), path.resolve(destination), '/E', '/COPY:DAT', '/DCOPY:DAT', '/R:0', '/W:0', '/MT:16', '/XJ', '/NFL', '/NDL', '/NJH', '/NJS', '/NP'], { windowsHide: true, stdio: 'pipe', maxBuffer: 8 * 1024 * 1024 }); } catch (error) { // Robocopy uses 1..7 for successful copies with differences, 8+ for errors. if (!Number.isInteger(error.status) || error.status < 0 || error.status >= 8) throw error; } }