-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-parse-config-host.mts
More file actions
135 lines (127 loc) · 4.03 KB
/
create-parse-config-host.mts
File metadata and controls
135 lines (127 loc) · 4.03 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
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
/**
* @file createParseConfigHost
* @module tsconfig-utils/lib/createParseConfigHost
*/
import matchFiles from '#internal/match-files'
import createModuleResolutionHost from '#lib/create-module-resolution-host'
import type { ModuleId } from '@flex-development/mlly'
import type {
Awaitable,
List,
ModuleResolutionHost,
ParseConfigHost,
ParseConfigHostOptions
} from '@flex-development/tsconfig-utils'
/**
* Create a configuration parser host.
*
* The parser host provides methods for accessing the file system
* and resolving module paths.
*
* > 👉 **Note**: The host can have both asynchronous and synchronous methods,
* > but when used with the native TypeScript compiler, all methods must return
* > synchronous values.
*
* @see {@linkcode ParseConfigHostOptions}
* @see {@linkcode ParseConfigHost}
*
* @this {void}
*
* @param {ParseConfigHostOptions | null | undefined} [options]
* Options for host creation
* @return {ParseConfigHost}
* The parse config host
*/
function createParseConfigHost(
this: void,
options?: ParseConfigHostOptions | null | undefined
): ParseConfigHost {
/**
* The module resolution host.
*
* @const {ModuleResolutionHost} host
*/
const host: ModuleResolutionHost = createModuleResolutionHost(options)
/**
* Whether to treat file names as case sensitive.
*
* @var {boolean} useCaseSensitiveFileNames
*/
let useCaseSensitiveFileNames: boolean = !!host.useCaseSensitiveFileNames
// determine if file names should be treated as case sensitive.
if (typeof host.useCaseSensitiveFileNames === 'function') {
useCaseSensitiveFileNames = !!host.useCaseSensitiveFileNames()
}
return {
directoryExists: host.directoryExists.bind(host),
fileExists: host.fileExists.bind(host),
getCurrentDirectory: host.getCurrentDirectory.bind(host),
getDirectories: host.getDirectories.bind(host),
readDirectory,
readFile: host.readFile.bind(host),
realpath: host.realpath.bind(host),
useCaseSensitiveFileNames
}
/**
* @template {Awaitable<ReadonlyArray<string>>} T
* The list of matched files
*
* @this {unknown}
*
* @param {ModuleId} parent
* The module id of the parent directory
* @param {List<string> | null | undefined} [extensions]
* The list of file extensions to filter for
* @param {List<string> | null | undefined} [exclude]
* The list of glob patterns matching files to exclude
* @param {List<string> | null | undefined} [include]
* The list of glob patterns matching files to include
* @param {number | null | undefined} [depth]
* The maximum search depth (inclusive)
* @return {T}
* The listed of matched files
*/
function readDirectory<T extends Awaitable<readonly string[]>>(
parent: ModuleId,
extensions?: List<string> | null | undefined,
exclude?: List<string> | null | undefined,
include?: List<string> | null | undefined,
depth?: number | null | undefined
): T
/**
* @this {ParseConfigHost}
*
* @param {ModuleId} parent
* The module id of the parent directory
* @param {List<string> | null | undefined} [extensions]
* The list of file extensions to filter for
* @param {List<string> | null | undefined} [exclude]
* The list of glob patterns matching files to exclude
* @param {List<string> | null | undefined} [include]
* The list of glob patterns matching files to include
* @param {number | null | undefined} [depth]
* The maximum search depth (inclusive)
* @return {Awaitable<ReadonlyArray<string>>}
* The listed of matched files
*/
function readDirectory(
this: ParseConfigHost,
parent: ModuleId,
extensions?: List<string> | null | undefined,
exclude?: List<string> | null | undefined,
include?: List<string> | null | undefined,
depth?: number | null | undefined
): Awaitable<readonly string[]> {
return matchFiles(
this,
parent,
extensions,
exclude,
include,
useCaseSensitiveFileNames,
depth,
options?.fs
)
}
}
export default createParseConfigHost