diff options
Diffstat (limited to 'pbt/ts/src/fs.ts')
| -rw-r--r-- | pbt/ts/src/fs.ts | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/pbt/ts/src/fs.ts b/pbt/ts/src/fs.ts new file mode 100644 index 0000000..133c8fd --- /dev/null +++ b/pbt/ts/src/fs.ts @@ -0,0 +1,16 @@ +import {mkdtemp, realpath} from 'fs/promises'; +import {tmpdir} from 'os'; +import path = require('path'); +import fs = require('fs'); + +export const withTempFile = <A>(fn: (filepath: string) => A) => + withTempDir(dir => fn(path.join(dir, 'file'))); + +export const withTempDir = async <A>(fn: (path: string) => A) => { + const dir = await mkdtemp((await realpath(tmpdir())) + path.sep); + try { + return await fn(dir); + } finally { + fs.rmdir(dir, () => {}); + } +}; |
