-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-module-resolution-host.mts
169 lines (152 loc) · 3.46 KB
/
create-module-resolution-host.mts
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* @file createModuleResolutionHost
* @module tsconfig-utils/lib/createModuleResolutionHost
*/
import dfs from '#internal/fs'
import type { ModuleId } from '@flex-development/mlly'
import pathe from '@flex-development/pathe'
import type {
FileSystem,
ModuleResolutionHost
} from '@flex-development/tsconfig-utils'
import { ok } from 'devlop'
/**
* Create a module resolution host.
*
* @see {@linkcode FileSystem}
* @see {@linkcode ModuleResolutionHost}
*
* @this {void}
*
* @param {FileSystem | null | undefined} [fs]
* File system API
* @return {ModuleResolutionHost}
* Module resolution host object
*/
function createModuleResolutionHost(
this: void,
fs?: FileSystem | null | undefined
): ModuleResolutionHost {
fs ??= dfs
return {
directoryExists,
fileExists,
getCurrentDirectory,
getDirectories,
readFile,
realpath,
useCaseSensitiveFileNames: undefined
}
/**
* Check if a directory exists at `id`.
*
* @this {void}
*
* @param {ModuleId} id
* The module id to check
* @return {boolean}
* `true` if directory exists at `id`, `false` otherwise
*/
function directoryExists(id: ModuleId): boolean {
try {
ok(fs, 'expected `fs`')
return fs.stat(pathe.toPath(id)).isDirectory()
} catch {
return false
}
}
/**
* Check if a file exists at `id`.
*
* @this {void}
*
* @param {ModuleId} id
* The module id to check
* @return {boolean}
* `true` if file exists at `id`, `false` otherwise
*/
function fileExists(id: ModuleId): boolean {
try {
ok(fs, 'expected `fs`')
return fs.stat(pathe.toPath(id)).isFile()
} catch {
return false
}
}
/**
* Get the path to current working directory.
*
* @this {void}
*
* @return {string}
* Path to current working directory
*/
function getCurrentDirectory(): string {
return pathe.cwd() + pathe.sep
}
/**
* Get a list of subdirectories.
*
* @this {void}
*
* @param {ModuleId} id
* The directory path or URL to read
* @return {string[]}
* List of subdirectory names
*/
function getDirectories(id: ModuleId): string[] {
ok(fs, 'expected `fs`')
/**
* List of subdirectory names.
*
* @var {string[]} names
*/
let names: string[] = []
if (directoryExists(id)) {
id = pathe.toPath(id)
names = fs.readdir(id).filter(x => {
ok(typeof id === 'string', 'expected `id` to be a string')
return directoryExists(pathe.join(id, x))
})
}
return names
}
/**
* Get the contents of a file.
*
* @this {void}
*
* @param {ModuleId} id
* The file path or URL to read
* @return {Buffer | string}
* File contents or `undefined` if file does not exist at `id`
*/
function readFile(id: ModuleId): string | undefined {
ok(fs, 'expected `fs`')
/**
* File contents.
*
* @var {string | undefined} contents
*/
let contents: string | undefined
if (fileExists(id)) {
contents = String(fs.readFile(pathe.toPath(id)))
}
return contents
}
/**
* Get the canonical pathname of `id`.
*
* @this {void}
*
* @param {ModuleId} id
* The path or `file:` URL to handle
* @return {string}
* Canonical pathname of `id`
*/
function realpath(this: void, id: ModuleId): string {
ok(fs, 'expected `fs`')
return fs.realpath(pathe.toPath(id))
}
}
export default createModuleResolutionHost