summaryrefslogtreecommitdiff
path: root/pbt/ts/src/fs.ts
blob: 133c8fdbb02642d0b5e7875e78137d74c29e7146 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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, () => {});
  }
};