aboutsummaryrefslogtreecommitdiff
path: root/lib/utils.js
blob: cee3f4708ad110f83fcbc01dadb2c7695c5a83b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { access, constants, mkdir, rm } from 'fs/promises';
import { kFileNotFoundError } from './constants.js';

// File system utilities

export const rmIfExists = async function rmIfExists(location) {

  try {
    await access(location, constants.F_OK);
    await rm(location, { recursive: true });
  }
  catch (error) {
    if (error.code === kFileNotFoundError) {
      return;
    }

    throw error;
  }
};

export const ensureDirectoryExists = async function ensureDirectoryExists(directory) {

  try {
    await access(directory);
  }
  catch (error) {
    if (error.code === kFileNotFoundError) {
      await mkdir(directory, { recursive: true });
      return;
    }

    throw error;
  }
};