-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathis-directory.mts
More file actions
72 lines (66 loc) · 1.62 KB
/
is-directory.mts
File metadata and controls
72 lines (66 loc) · 1.62 KB
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
/**
* @file isDirectory
* @module mlly/lib/isDirectory
*/
import constant from '#internal/constant'
import dfs from '#internal/fs'
import isModuleId from '#lib/is-module-id'
import type { Awaitable, FileSystem } from '@flex-development/mlly'
import when from '@flex-development/when'
export default isDirectory
/**
* Check if a directory exists.
*
* > 👉 **Note**: Returns a promise if `fs.stat` is async.
*
* @see {@linkcode Awaitable}
* @see {@linkcode FileSystem}
*
* @template {Awaitable<boolean>} T
* The result of the check
*
* @this {void}
*
* @param {unknown} id
* The module id to check
* @param {FileSystem | null | undefined} fs
* The file system API
* @return {T}
* `true` if directory exists at `id`, `false` otherwise
*/
function isDirectory<T extends Awaitable<boolean>>(
this: void,
id: unknown,
fs?: FileSystem | null | undefined
): T
/**
* Check if a directory exists.
*
* > 👉 **Note**: Returns a promise if `fs.stat` is async.
*
* @see {@linkcode Awaitable}
* @see {@linkcode FileSystem}
*
* @this {void}
*
* @param {unknown} id
* The module id to check
* @param {FileSystem | null | undefined} fs
* The file system API
* @return {Awaitable<boolean>}
* `true` if directory exists at `id`, `false` otherwise
*/
function isDirectory(
this: void,
id: unknown,
fs?: FileSystem | null | undefined
): Awaitable<boolean> {
if (typeof id === 'string' && id.startsWith('file:')) id = new URL(id)
if (!isModuleId(id)) return false
fs ??= dfs
try {
return when(fs.stat(id), stats => stats.isDirectory(), constant(false))
} catch {
return false // swallow error.
}
}