-
Notifications
You must be signed in to change notification settings - Fork 170
/
fs.ts
84 lines (78 loc) · 1.89 KB
/
fs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { ensureDir, path } from '../deps.ts'
/* check whether or not the given path exists as a directory. */
export async function existsDir(path: string): Promise<boolean> {
try {
const fi = await Deno.lstat(path)
if (fi.isDirectory) {
return true
}
return false
} catch (err) {
if (err instanceof Deno.errors.NotFound) {
return false
}
return Promise.reject(err)
}
}
/* check whether or not the given path exists as a directory. */
export function existsDirSync(path: string) {
try {
const fi = Deno.lstatSync(path)
if (fi.isDirectory) {
return true
}
return false
} catch (err) {
if (err instanceof Deno.errors.NotFound) {
return false
}
throw err
}
}
/* check whether or not the given path exists as regular file. */
export async function existsFile(path: string): Promise<boolean> {
try {
const fi = await Deno.lstat(path)
if (fi.isFile) {
return true
}
return false
} catch (err) {
if (err instanceof Deno.errors.NotFound) {
return false
}
return Promise.reject(err)
}
}
/* check whether or not the given path exists as regular file. */
export function existsFileSync(path: string) {
try {
const fi = Deno.lstatSync(path)
if (fi.isFile) {
return true
}
return false
} catch (err) {
if (err instanceof Deno.errors.NotFound) {
return false
}
throw err
}
}
/** ensure and write a text file. */
export async function ensureTextFile(name: string, content: string): Promise<void> {
const dir = path.dirname(name)
await ensureDir(dir)
await Deno.writeTextFile(name, content)
}
/** remove the file if it exists. */
export async function lazyRemove(name: string): Promise<void> {
try {
await Deno.remove(name)
} catch (err) {
if (err instanceof Deno.errors.NotFound) {
return
}
return Promise.reject(err)
}
}