]>
Commit | Line | Data |
---|---|---|
6cd62e79 RBR |
1 | import { access, constants, mkdir, rm } from 'fs/promises'; |
2 | import { kFileNotFoundError } from './constants.js'; | |
d3f282a1 RBR |
3 | |
4 | // File system utilities | |
5 | ||
6cd62e79 | 6 | export 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 | } | |
19 | } | |
d3f282a1 | 20 | |
6cd62e79 | 21 | export 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 | } |
6cd62e79 | 34 | } |