]> git.r.bdr.sh - rbdr/blog/blame_incremental - lib/utils.js
Use serde and time
[rbdr/blog] / lib / utils.js
... / ...
CommitLineData
1import { access, constants, mkdir, rm } from 'fs/promises';
2import { kFileNotFoundError } from './constants.js';
3
4// File system utilities
5
6export 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
21export 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};