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