-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
resolve-tsconfig.js
82 lines (73 loc) · 1.88 KB
/
resolve-tsconfig.js
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
import { isAbsolute, join } from 'node:path'
import { statSync } from 'node:fs'
import { packageConfigSync } from 'pkg-conf'
import { DEFAULT_TSCONFIG_LOCATIONS } from './constants.js'
/**
*
* @param {string} pathToValidate
* @returns {boolean}
*/
const _isValidPath = (pathToValidate) => {
try {
statSync(pathToValidate)
} catch {
return false
}
return true
}
/**
*
* @param {string} cwd
* @param {string} settingsProjectPath
* @returns {string | undefined}
*/
export const _resolveTSConfigPath = (cwd, settingsProjectPath) => {
let settingsPath = settingsProjectPath
if (!isAbsolute(settingsPath)) {
settingsPath = join(cwd, settingsProjectPath)
}
if (_isValidPath(settingsPath)) {
return settingsPath
}
return undefined
}
/**
*
* @param {string} cwd
* @returns {string | undefined}
*/
export const _getTSConfigFromDefaultLocations = (cwd) => {
for (const tsFile of DEFAULT_TSCONFIG_LOCATIONS) {
const absolutePath = _resolveTSConfigPath(cwd, tsFile)
if (absolutePath !== undefined) {
return absolutePath
}
}
return undefined
}
/**
*
* @param {string} cwd
* @param {string | undefined} cliProjectOption
* @returns {string}
*/
export const _getTSConfigPath = (cwd, cliProjectOption) => {
/** @type {string | undefined} */
let tsConfigPath = _getTSConfigFromDefaultLocations(cwd)
if (cliProjectOption !== undefined) {
tsConfigPath = _resolveTSConfigPath(cwd, cliProjectOption)
} else {
const settings = packageConfigSync('ts-standard', { cwd })
if (settings.project != null) {
tsConfigPath = _resolveTSConfigPath(cwd, settings.project)
}
}
if (tsConfigPath === undefined) {
console.error(
'Unable to locate the project file. A project file (tsconfig.json or ' +
'tsconfig.eslint.json) is required in order to use ts-standard.'
)
return process.exit(1)
}
return tsConfigPath
}