]> git.r.bdr.sh - rbdr/blog/blob - lib/utils.js
24b9407a46645c869bafb1099474d4eba232dc22
[rbdr/blog] / lib / utils.js
1 const { access, constants, mkdir, rm } = require('fs/promises');
2 const { kFileNotFoundError } = require('./constants');
3
4 // File system utilities
5
6 module.exports = {
7 async rmIfExists(location) {
8
9 try {
10 await access(location, constants.F_OK);
11 await rm(location, { recursive: true });
12 }
13 catch (error) {
14 if (error.code === kFileNotFoundError) {
15 return;
16 }
17
18 throw error;
19 }
20 },
21
22 async ensureDirectoryExists(directory) {
23
24 try {
25 await access(directory);
26 }
27 catch (error) {
28 if (error.code === kFileNotFoundError) {
29 await mkdir(directory, { recursive: true });
30 return;
31 }
32
33 throw error;
34 }
35 }
36 };