aboutsummaryrefslogtreecommitdiff
path: root/lib/utils.js
blob: 24b9407a46645c869bafb1099474d4eba232dc22 (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
35
36
const { access, constants, mkdir, rm } = require('fs/promises');
const { kFileNotFoundError } = require('./constants');

// File system utilities

module.exports = {
  async rmIfExists(location) {

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

      throw error;
    }
  },

  async ensureDirectoryExists(directory) {

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

      throw error;
    }
  }
};