| 1 | import { mkdtemp, rm } from 'node:fs/promises'; |
| 2 | import { tmpdir } |
| 3 | import path from 'node:path |
| 4 | |
| 5 | /** Runs `work` in a fresh scratch directory named after `prefix`, removed afterwards whatever happens. */ |
| 6 | export async function withTempDir |
| 7 | prefix: string, |
| 8 | work: (directory |
| 9 | ): Promise<T |
| 10 | const directory = |
| 11 | try { |
| 12 | return await work |
| 13 | } finally { |
| 14 | await rm(directory |
| 15 | } |
| 16 | } |