updateScratch
uses node:fs
Update the codebase with the scratch file file (holding text), as tcm update <file> --clear does (a file kept as
written, --into where it goes, unless it's Markdown, whose first line says): it's run, on the branch the studio
serves, so what it does, refuses, and says is the command's, and the file goes once what it held is saved. What it
said, and the history entry it made.
import { HttpError } from "#codebase/tcmts/server"; import { editableCodebase, keptFile, runTcm, scratchPath, setKeptInto } from "#codebase/tcmts/studio"; import fs from "node:fs"; export async function updateScratch( file: string, text: string, ): Promise<{ said: string; entry?: string }> { const cb = editableCodebase(); const abs = scratchPath(cb, file, text); fs.writeFileSync(abs, text); const kept = /\.tsx?$/.test(file) ? undefined : keptFile(cb, abs, text); const into = kept && !file.endsWith(".md") ? ["--into", kept.slice(0, Math.max(0, kept.lastIndexOf(".")))] : []; const before = cb.store.head(); const out = await runTcm(cb.lay.root, ["update", abs, "--clear", ...into], { branch: true }); if (out.code !== 0) throw new HttpError( 400, out.text.split("\n").slice(0, 12).join("\n") || `tcm update ${file} failed`, ); if (kept) setKeptInto(cb, abs, undefined); const after = cb.store.head(); return { said: out.text, ...(after !== before ? { entry: after } : {}) }; }
uses
- tcmts.server.HttpError
- tcmts.studio.editableCodebase
- tcmts.studio.keptFile
- tcmts.studio.runTcm
- tcmts.studio.scratchPath
- tcmts.studio.setKeptInto