-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c34e06
commit 79c4028
Showing
6 changed files
with
96 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import makeDebug from 'debug' | ||
import {readFile, readdir} from 'node:fs/promises' | ||
import {dirname, join} from 'node:path' | ||
|
||
import {memoizedWarn} from '../errors' | ||
import {TSConfig} from '../interfaces' | ||
import {mergeNestedObjects} from './util' | ||
|
||
const debug = makeDebug('read-tsconfig') | ||
|
||
function resolve(root: string, name: string): string | undefined { | ||
try { | ||
return require.resolve(name, {paths: [root]}) | ||
} catch { | ||
// return undefined | ||
} | ||
} | ||
|
||
async function upUntil(path: string, test: (path: string) => Promise<boolean>): Promise<string | undefined> { | ||
let result: boolean | undefined | ||
try { | ||
result = await test(path) | ||
} catch { | ||
result = false | ||
} | ||
|
||
if (result) return path | ||
|
||
const parent = dirname(path) | ||
if (parent === path) return | ||
|
||
return upUntil(parent, test) | ||
} | ||
|
||
export async function readTSConfig(root: string): Promise<TSConfig | undefined> { | ||
const found: Record<string, any>[] = [] | ||
|
||
let typescript: typeof import('typescript') | undefined | ||
try { | ||
typescript = require('typescript') | ||
} catch { | ||
try { | ||
typescript = require(root + '/node_modules/typescript') | ||
} catch {} | ||
} | ||
|
||
if (!typescript) { | ||
memoizedWarn( | ||
'Could not find typescript. Please ensure that typescript is a devDependency. Falling back to compiled source.', | ||
) | ||
return | ||
} | ||
|
||
const read = async (path: string): Promise<unknown> => { | ||
const localRoot = await upUntil(path, async (p) => | ||
// eslint-disable-next-line unicorn/no-await-expression-member | ||
(await readdir(p)).includes('package.json'), | ||
) | ||
if (!localRoot) return | ||
|
||
try { | ||
const contents = await readFile(path, 'utf8') | ||
const parsed = typescript?.parseConfigFileTextToJson(path, contents).config | ||
|
||
found.push(parsed) | ||
|
||
if (parsed.extends) { | ||
if (parsed.extends.startsWith('.')) { | ||
const nextPath = resolve(localRoot, parsed.extends) | ||
return nextPath ? read(nextPath) : undefined | ||
} | ||
|
||
const resolved = resolve(localRoot, parsed.extends) | ||
if (resolved) return read(resolved) | ||
} | ||
|
||
return parsed | ||
} catch (error) { | ||
debug(error) | ||
} | ||
} | ||
|
||
await read(join(root, 'tsconfig.json')) | ||
|
||
return { | ||
compilerOptions: mergeNestedObjects(found, 'compilerOptions'), | ||
'ts-node': mergeNestedObjects(found, 'ts-node'), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters