-
Notifications
You must be signed in to change notification settings - Fork 170
/
fs.ts
64 lines (58 loc) · 1.58 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
import { dirname, join } from 'https://deno.land/std@0.106.0/path/mod.ts'
import { ensureDir } from 'https://deno.land/std@0.106.0/fs/ensure_dir.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
}
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
}
throw err
}
}
/** ensure and write text file. */
export async function ensureTextFile(name: string, content: string): Promise<void> {
const dir = dirname(name)
await ensureDir(dir)
await Deno.writeTextFile(name, content)
}
/** remove the file if it exists. */
export async function lazyRemove(name: string, options?: { recursive?: boolean }): Promise<void | Error> {
try {
await Deno.remove(name, options)
} catch (err) {
if (err instanceof Deno.errors.NotFound) {
return
}
return Promise.reject(err)
}
}
export async function findFile(wd: string, filenames: string[]) {
for (const filename of filenames) {
const fullPath = join(wd, filename)
if (await existsFile(fullPath)) {
return fullPath
}
}
return null
}