| 14 | dispose?: (dir: string) => Promise<T> |
| 15 | } |
| 16 | export async function tmpdir<T>(options?: TmpDirOptions<T>) { |
| 17 | const dirpath = sanitizePath(path.join(os.tmpdir(), "arctic-test-" + Math.random().toString(36).slice(2))) |
| 18 | await fs.mkdir(dirpath, { recursive: true }) |
| 19 | if (options?.git) { |
| 20 | await $`git init`.cwd(dirpath).quiet() |
| 21 | await $`git commit --allow-empty -m "root commit ${dirpath}"`.cwd(dirpath).quiet() |
| 22 | } |
| 23 | const extra = await options?.init?.(dirpath) |
| 24 | const realpath = sanitizePath(await fs.realpath(dirpath)) |
| 25 | const result = { |
| 26 | [Symbol.asyncDispose]: async () => { |
| 27 | await options?.dispose?.(dirpath) |
| 28 | // await fs.rm(dirpath, { recursive: true, force: true }) |
| 29 | }, |
| 30 | path: realpath, |
| 31 | extra: extra as T, |
| 32 | } |
| 33 | return result |
| 34 | } |