]> git.r.bdr.sh - rbdr/blog/blame - lib/utils.js
Add blog status, correctly shift
[rbdr/blog] / lib / utils.js
CommitLineData
6cd62e79
RBR
1import { access, constants, mkdir, rm } from 'fs/promises';
2import { kFileNotFoundError } from './constants.js';
d3f282a1
RBR
3
4// File system utilities
5
10a76a5b 6export const rmIfExists = async function rmIfExists(location) {
d3f282a1 7
6cd62e79
RBR
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;
d3f282a1 15 }
d3f282a1 16
6cd62e79
RBR
17 throw error;
18 }
10a76a5b 19};
d3f282a1 20
10a76a5b 21export const ensureDirectoryExists = async function ensureDirectoryExists(directory) {
d3f282a1 22
6cd62e79
RBR
23 try {
24 await access(directory);
25 }
26 catch (error) {
27 if (error.code === kFileNotFoundError) {
28 await mkdir(directory, { recursive: true });
29 return;
d3f282a1 30 }
d3f282a1 31
6cd62e79 32 throw error;
d3f282a1 33 }
10a76a5b 34};