// A TAKE-SCOPED EDIT (slice C of "the apps do the work", 4 Nov): run an // existing manifest command -- `umtool posts set-item`, `posts roles`, `posts // narrate`, `posts reread`, `posts fx` -- against a TAKE of the project // instead of the project's manifest. // // withTakeScope(projectDir, take, fn) fn runs with the project's manifest // READ as the take's (takeManifest.mjs // resolveTakeManifest: the project's now // + the style's patch + the take's) and // every write landing in the take's // patch (takeEdit.mjs editTakeManifest) // // The commands are not rewritten for it. They read through readManifest // (projects/report.mjs), take a token from manifestToken, and write through // manifest.mjs's `structural` -- and those three ask takeScopeOf(dir) first: // // readManifest(dir) the take's resolved manifest // manifestToken(dir) take.json's mtime (the take's own token) // structural(dir, …) the mutation applied to the take's manifest, written // into takes//take.json's patch with an undo point // of the take's own (`umtool timeline

--take undo`) // writeManifestAtomic REFUSED (TakeScopeRefused): a writer that has not been // routed to the take would otherwise change the // project's manifest, which a take-scoped edit never does // withEditNotes no edit notes: the take's patch is not the generated // manifest, so there is nothing for its generator to port // // Files a scoped command makes (a new read's wav, its parts) land in the // project's own dirs exactly as an unscoped one's do -- the take renders from // the project's dir (takeMaker.mjs renderManifestPath), so it finds them -- and // their names come from nextTakeNumber over the take's entry AND the files on // disk, so a take's read never overwrites one the project's manifest names. // // The scope is an AsyncLocalStorage store, so it follows the command through // every await and never leaks into a concurrent request of the app. It names // ONE project dir: a read or write of any other project inside it is the // plain, unscoped one. // // Plain ESM with no imports of the writers (they import this): the CLI runs it // under bare node. import { AsyncLocalStorage } from "node:async_hooks"; import { stat } from "node:fs/promises"; import path from "node:path"; import { TAKE_FILE, isTakeId, takesDirOf } from "./takes.mjs"; /** @type {AsyncLocalStorage<{ dir: string, take: string }>} */ const als = new AsyncLocalStorage(); export class TakeScopeRefused extends Error { /** @param {string} message */ constructor(message) { super(message); this.name = "TakeScopeRefused"; } } /** * Run `fn` with `projectDir`'s manifest read and written as take `take`'s. * @template T * @param {string} projectDir * @param {string} take * @param {() => Promise} fn * @returns {Promise} */ export function withTakeScope(projectDir, take, fn) { if (!isTakeId(take)) throw new TakeScopeRefused(`"${take}" is not a take id (lower-case letters, digits, dashes)`); return als.run({ dir: path.resolve(projectDir), take }, fn); } /** * The take a read or write of `dir` is scoped to, or null (no scope, or a scope of another project). * @param {string} dir * @returns {{ dir: string, take: string } | null} */ export function takeScopeOf(dir) { const s = als.getStore(); if (!s || typeof dir !== "string") return null; return path.resolve(dir) === s.dir ? s : null; } /** The scoped take's token (its take.json's mtime, takeEdit.mjs takeToken's), or null when it has none. */ export async function scopedTakeToken(scope) { const st = await stat(path.join(takesDirOf(scope.dir), scope.take, TAKE_FILE)).catch(() => null); return st ? String(Math.round(st.mtimeMs)) : null; }