-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread-tsconfig.mts
145 lines (130 loc) · 3.3 KB
/
read-tsconfig.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
/**
* @file readTsconfig
* @module tsconfig-utils/lib/readTsconfig
*/
import withTrailingSlash from '#internal/with-trailing-slash'
import {
cwd as cwdUrl,
getSource,
moduleResolve,
toUrl,
type ModuleId
} from '@flex-development/mlly'
import pathe from '@flex-development/pathe'
import type { Tsconfig } from '@flex-development/tsconfig-types'
import type {
ReadTsconfigOptions,
ResolvedTsconfig
} from '@flex-development/tsconfig-utils'
import { isObjectCurly } from '@flex-development/tutils'
import JSON from 'json5'
import stripBom from 'strip-bom'
/**
* Resolve and read a tsconfig file.
*
* @see {@linkcode ModuleId}
* @see {@linkcode ReadTsconfigOptions}
* @see {@linkcode Tsconfig}
*
* @async
*
* @this {void}
*
* @param {ModuleId} id
* Module id of tsconfig file, or path to tsconfig file
* @param {ReadTsconfigOptions | null | undefined} [options]
* Read options
* @return {Promise<ResolvedTsconfig | null>}
* Resolved tsconfig or `null` if tsconfig file is not found
*/
async function readTsconfig(
this: void,
id: ModuleId,
options?: ReadTsconfigOptions | null | undefined
): Promise<ResolvedTsconfig | null> {
/**
* URL of current working directory.
*
* @const {URL} cwd
*/
const cwd: URL = toUrl(withTrailingSlash(options?.cwd ?? cwdUrl()))
/**
* Module extensions to probe for.
*
* @const {Set<string>} extensions
*/
const extensions: Set<string> = new Set<string>(['.json', '.jsonc', '.json5'])
/**
* URL of parent module.
*
* @const {URL} parent
*/
const parent: URL = new URL('parent.json', cwd)
/**
* Module specifiers to try resolving.
*
* @const {[string, ModuleId][]} tries
*/
const tries: [string, ModuleId][] = [
[String(id), options?.parent ?? parent],
[pathe.dot + pathe.sep + String(id), parent]
]
/**
* URL of tsconfig file.
*
* @var {URL | null} url
*/
let url: URL | null = null
for (const [specifier, parent] of tries) {
try {
url = await moduleResolve(
specifier,
parent,
options?.conditions,
options?.mainFields,
options?.preserveSymlinks,
options?.fs
)
} catch {
url = null
if (!extensions.has(pathe.extname(specifier))) {
for (const ext of extensions) {
try {
url = await moduleResolve(
pathe.addExt(specifier, ext),
parent,
options?.conditions,
options?.mainFields,
options?.preserveSymlinks,
options?.fs
)
break
} catch {
url = null
}
}
}
}
if (url !== null && url.protocol !== 'node:') {
/**
* Resolved tsconfig.
*
* @const {ResolvedTsconfig} resolved
*/
const resolved: ResolvedTsconfig = { tsconfig: {}, url }
/**
* Contents of tsconfig file.
*
* @const {string | null | undefined} contents
*/
const contents: string | null | undefined = await getSource(url, options)
if (typeof contents === 'string') {
resolved.tsconfig = JSON.parse(stripBom(contents.trim() || 'null'))
if (!isObjectCurly(resolved.tsconfig)) resolved.tsconfig = {}
}
return resolved
}
}
return null
}
export default readTsconfig