file-system.ts 770 B

1234567891011121314151617181920212223242526272829303132
  1. import { mkdtemp, rm, writeFile, mkdir } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. export async function createTempDir(
  5. prefix = "claude-test-",
  6. ): Promise<string> {
  7. return mkdtemp(join(tmpdir(), prefix));
  8. }
  9. export async function cleanupTempDir(dir: string): Promise<void> {
  10. await rm(dir, { recursive: true, force: true });
  11. }
  12. export async function writeTempFile(
  13. dir: string,
  14. name: string,
  15. content: string,
  16. ): Promise<string> {
  17. const path = join(dir, name);
  18. await writeFile(path, content, "utf-8");
  19. return path;
  20. }
  21. export async function createTempSubdir(
  22. dir: string,
  23. name: string,
  24. ): Promise<string> {
  25. const path = join(dir, name);
  26. await mkdir(path, { recursive: true });
  27. return path;
  28. }