]> git.r.bdr.sh - rbdr/blog/blame - lib/utils.js
Trim some resources
[rbdr/blog] / lib / utils.js
CommitLineData
d3f282a1
RBR
1const { access, constants, mkdir, rm } = require('fs/promises');
2const { kFileNotFoundError } = require('./constants');
3
4// File system utilities
5
6module.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};