newScratch

tcmts.studio.newScratch · #sals74co · term · runs on the server

uses node:fs

A new, empty scratch file, scratch.ts or the first scratch-<n>.ts there isn't: its name.

import { editableCodebase, scratchPath } from "#codebase/tcmts/studio";
import fs from "node:fs";

export async function newScratch(): Promise<string> {
  const cb = editableCodebase();
  for (let n = 1; ; n++) {
    const file = n === 1 ? "scratch.ts" : `scratch-${n}.ts`;
    const abs = scratchPath(cb, file);
    if (fs.existsSync(abs) || fs.existsSync(abs.replace(/\.ts$/, ".tsx"))) continue;
    fs.writeFileSync(abs, "");
    return file;
  }
}

uses

used by