UNPKG

histoire

Version:

A fast story book powered by Vite

82 lines (81 loc) • 3.71 kB
import { createHash, randomUUID } from 'node:crypto'; import { hostname } from 'node:os'; import { normalize, resolve } from 'pathe'; /** Reserved generated run directory, shared only as a parent of isolated outputs. */ export const HISTOIRE_TEMP_PATH = '.histoire/tmp'; /** Resolves generated output ancestry without acquiring or deleting a run. */ export function getProjectTempDir(root) { return resolve(root, HISTOIRE_TEMP_PATH); } /** Caches kept across runs (optimizer output, story catalog). */ export function getProjectCacheDir(root) { return resolve(root, '.histoire', 'cache'); } /** File published by the background story collection cache. */ export function getCollectionCacheFile(root) { return resolve(getProjectCacheDir(root), 'collection', 'stories.bin'); } /** * Builds the temp directory owned by a single run. * * Runs empty their own directory when they start, so a directory shared between * runs (a dev-server test run concurrent with a `histoire build`, two CLI runs…) * would delete the generated files of any run still in flight. * @param root The project root. * @param kind Name of the generated artifacts, used as parent directory. */ export function getRunTempDir(root, kind) { // The owner tag lets later runs remove directories of runs that were killed. return resolve(getProjectTempDir(root), kind, `run-${Date.now().toString(36)}-${getRunOwnerTag()}-${randomUUID().slice(0, 8)}`); } /** Short tag of this host, so runs never judge processes of another machine sharing the project. */ export function getHostTag() { return createHash('sha256').update(hostname()).digest('hex').slice(0, 6); } /** `p<pid>h<host>` segment of this process' run directories. */ function getRunOwnerTag() { return `p${process.pid.toString(36)}h${getHostTag()}`; } /** * Produces a short stable disambiguator for a value used inside a generated * file name. Generated temp paths sanitize or flatten their source value * (`a/b` and `a b` both become `a_b`), so distinct sources can otherwise * resolve to the same file and silently overwrite each other. * @param value The raw value the temp path is derived from. */ export function hashPathSegment(value) { return createHash('sha256').update(value).digest('hex').slice(0, 8); } /** Replaces every character that cannot appear in a generated file name. */ function sanitizeFileSegment(value) { return value.replace(/[^\w.-]/g, '_'); } /** * Builds the file-name segment a generated file is written under. * * Always disambiguated by {@link hashPathSegment}: the sanitized form is lossy * (`a/b` and `a b` both become `a_b`), so two sources would otherwise be * written to the same file and one of them silently dropped from the run. * @param value The raw value the segment is derived from. * @param sanitize How to turn the raw value into a file-name-safe form. */ export function toTempPathSegment(value, sanitize = sanitizeFileSegment) { return `${sanitize(value)}.${hashPathSegment(value)}`; } /** * Throws when two generated temp paths resolve to the same file, which would * silently drop one of them from the run. * @param entries Generated paths with a human-readable label of their source. * @param kind Name of the generated artifact, used in the error message. */ export function assertNoDuplicateTempPaths(entries, kind) { const seen = new Map(); for (const entry of entries) { const key = normalize(entry.path); const existing = seen.get(key); if (existing) { throw new Error(`Duplicate generated Histoire ${kind} path "${entry.path}" for ${existing} and ${entry.label}.`); } seen.set(key, entry.label); } }