UNPKG

histoire

Version:

A fast story book powered by Vite

37 lines (36 loc) • 1.66 kB
import { mkdir, mkdtemp, realpath, rm } from 'node:fs/promises'; import { basename, dirname, join, resolve } from 'node:path'; import { isPathWithinRoot } from '../../mcp/project/containment.js'; /** Resolves existing parent aliases before checking an output path that may not exist yet. */ export async function canonicalOutput(path) { try { return await realpath(path); } catch (error) { if (error.code !== 'ENOENT') throw error; const parent = dirname(path); if (parent === path) throw error; return join(await canonicalOutput(parent), basename(path)); } } /** Prevents a misconfigured replacement from renaming or deleting the project itself. */ export async function assertNodeOutputSafe(projectRoot, output) { const root = await realpath(projectRoot); const outDir = await canonicalOutput(resolve(output)); if (isPathWithinRoot(outDir, root)) throw new Error('Node output cannot replace project root or an ancestor'); } /** Allocates a fresh staging root beside the final output for same-filesystem renames. */ export async function createNodeBuildLayout(output) { const outDir = resolve(output); const parent = dirname(outDir); await mkdir(parent, { recursive: true }); const stagingDir = await mkdtemp(join(parent, `.${basename(outDir)}-histoire-node-`)); const publicDir = join(stagingDir, 'public'); const privateDir = join(stagingDir, 'private'); await mkdir(publicDir); await mkdir(privateDir); return { outDir, stagingDir, publicDir, privateDir, discard: () => rm(stagingDir, { recursive: true, force: true }) }; }