Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 3319af3

Browse files
committed
refactor(shared/fs): add lazyRemove function
1 parent 97453e4 commit 3319af3

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

shared/fs.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ensureDir, path } from '../deps.ts'
22

3-
/* check whether or not the given path exists as a directory */
3+
/* check whether or not the given path exists as a directory. */
44
export async function existsDir(path: string): Promise<boolean> {
55
try {
66
const fi = await Deno.lstat(path)
@@ -16,7 +16,7 @@ export async function existsDir(path: string): Promise<boolean> {
1616
}
1717
}
1818

19-
/* check whether or not the given path exists as a directory */
19+
/* check whether or not the given path exists as a directory. */
2020
export function existsDirSync(path: string) {
2121
try {
2222
const fi = Deno.lstatSync(path)
@@ -32,7 +32,7 @@ export function existsDirSync(path: string) {
3232
}
3333
}
3434

35-
/* check whether or not the given path exists as regular file */
35+
/* check whether or not the given path exists as regular file. */
3636
export async function existsFile(path: string): Promise<boolean> {
3737
try {
3838
const fi = await Deno.lstat(path)
@@ -48,7 +48,7 @@ export async function existsFile(path: string): Promise<boolean> {
4848
}
4949
}
5050

51-
/* check whether or not the given path exists as regular file */
51+
/* check whether or not the given path exists as regular file. */
5252
export function existsFileSync(path: string) {
5353
try {
5454
const fi = Deno.lstatSync(path)
@@ -64,9 +64,21 @@ export function existsFileSync(path: string) {
6464
}
6565
}
6666

67-
/** ensure and write a text file */
67+
/** ensure and write a text file. */
6868
export async function ensureTextFile(name: string, content: string): Promise<void> {
6969
const dir = path.dirname(name)
7070
await ensureDir(dir)
7171
await Deno.writeTextFile(name, content)
7272
}
73+
74+
/** remove the file if it exists. */
75+
export async function lazyRemove(name: string): Promise<void> {
76+
try {
77+
await Deno.remove(name)
78+
} catch (err) {
79+
if (err instanceof Deno.errors.NotFound) {
80+
return
81+
}
82+
return Promise.reject(err)
83+
}
84+
}

0 commit comments

Comments
 (0)