-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-module-resolution-host.mts
207 lines (185 loc) · 4.5 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
* @file createModuleResolutionHost
* @module tsconfig-utils/lib/createModuleResolutionHost
*/
import dfs from '#internal/fs'
import toPath from '#internal/to-path'
import withTrailingSlash from '#internal/with-trailing-slash'
import { isDirectory, isFile, type ModuleId } from '@flex-development/mlly'
import pathe from '@flex-development/pathe'
import type {
FileSystem,
ModuleResolutionHost,
ModuleResolutionHostOptions
} from '@flex-development/tsconfig-utils'
import { ok } from 'devlop'
export default createModuleResolutionHost
/**
* Union of values used to treat filenames as case-sensitive.
*
* @internal
*/
type UseCaseSensitiveFileNames = ((this: void) => boolean) | boolean | undefined
/**
* Create a module resolution host.
*
* @see {@linkcode ModuleResolutionHostOptions}
* @see {@linkcode ModuleResolutionHost}
*
* @this {void}
*
* @param {ModuleResolutionHostOptions | null | undefined} [options]
* Host options
* @return {ModuleResolutionHost}
* Module resolution host object
*/
function createModuleResolutionHost(
this: void,
options?: ModuleResolutionHostOptions | null | undefined
): ModuleResolutionHost {
/**
* File system API.
*
* @var {FileSystem} fs
*/
let fs: FileSystem = dfs
/**
* Path to root directory.
*
* @var {string} root
*/
let root: string = pathe.cwd()
/**
* Boolean indicating filenames should be treated as case-sensitive, a
* function that returns such a boolean, or `undefined` for default treatment
* of filename casing.
*
* @var {UseCaseSensitiveFileNames} useCaseSensitiveFileNames
*/
let useCaseSensitiveFileNames: UseCaseSensitiveFileNames = undefined
if (options) {
if (options.fs) {
fs = options.fs
}
if (options.root !== null && options.root !== undefined) {
root = toPath(options.root)
}
if (
options.useCaseSensitiveFileNames !== null &&
options.useCaseSensitiveFileNames !== undefined
) {
useCaseSensitiveFileNames = options.useCaseSensitiveFileNames
}
}
return {
directoryExists,
fileExists,
getCurrentDirectory,
getDirectories,
readFile,
realpath,
useCaseSensitiveFileNames
}
/**
* 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 {
ok(fs, 'expected `fs`')
return isDirectory(id, fs)
}
/**
* 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 {
ok(fs, 'expected `fs`')
return isFile(id, fs)
}
/**
* Get the path to current working directory.
*
* @this {void}
*
* @return {string}
* Path to current working directory
*/
function getCurrentDirectory(): string {
return withTrailingSlash(root)
}
/**
* 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)) {
names = fs
.readdirSync(id = toPath(id), { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name)
}
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.readFileSync(toPath(id)))
}
return contents
}
/**
* Get the resolved 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.realpathSync(toPath(id))
}
}