Skip to content

Commit

Permalink
feat: no longer use tsconfck
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Dec 1, 2023
1 parent 0c34e06 commit 79c4028
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 21 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"strip-ansi": "^6.0.1",
"supports-color": "^8.1.1",
"supports-hyperlinks": "^2.2.0",
"tsconfck": "^3.0.0",
"widest-line": "^3.1.0",
"wordwrap": "^1.0.0",
"wrap-ansi": "^7.0.0"
Expand Down
7 changes: 4 additions & 3 deletions src/config/ts-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import * as TSNode from 'ts-node'
import {memoizedWarn} from '../errors'
import {Plugin, TSConfig} from '../interfaces'
import {settings} from '../settings'
import {existsSync, readTSConfig} from '../util/fs'
import {existsSync} from '../util/fs'
import {readTSConfig} from '../util/read-tsconfig'
import {isProd} from '../util/util'
import Cache from './cache'
import {Debug} from './util'
// eslint-disable-next-line new-cap
const debug = Debug('ts-node')

export const TS_CONFIGS: Record<string, TSConfig> = {}
export const TS_CONFIGS: Record<string, TSConfig | undefined> = {}
const REGISTERED = new Set<string>()

function isErrno(error: any): error is NodeJS.ErrnoException {
Expand All @@ -22,7 +23,7 @@ async function loadTSConfig(root: string): Promise<TSConfig | undefined> {
try {
if (TS_CONFIGS[root]) return TS_CONFIGS[root]

TS_CONFIGS[root] = await readTSConfig(join(root, 'tsconfig.json'))
TS_CONFIGS[root] = await readTSConfig(root)

return TS_CONFIGS[root]
} catch (error) {
Expand Down
9 changes: 0 additions & 9 deletions src/util/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import {Stats, existsSync as fsExistsSync, readFileSync} from 'node:fs'
import {readFile, stat} from 'node:fs/promises'
import {join} from 'node:path'

import {mergeNestedObjects} from './util'

export function requireJson<T>(...pathParts: string[]): T {
return JSON.parse(readFileSync(join(...pathParts), 'utf8'))
}
Expand Down Expand Up @@ -71,10 +69,3 @@ export async function safeReadJson<T>(path: string): Promise<T | undefined> {
export function existsSync(path: string): boolean {
return fsExistsSync(path)
}

export async function readTSConfig(path: string) {
const {parse} = await import('tsconfck')
const result = await parse(path)
const tsNodeOpts = mergeNestedObjects(result.extended ?? [result], 'tsconfig.ts-node')
return {...result.tsconfig, 'ts-node': tsNodeOpts}
}
89 changes: 89 additions & 0 deletions src/util/read-tsconfig.ts
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'),
}
}
4 changes: 2 additions & 2 deletions test/config/ts-node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as tsNode from 'ts-node'

import * as configTsNode from '../../src/config/ts-node'
import {Interfaces, settings} from '../../src/index'
import * as util from '../../src/util/fs'
import * as util from '../../src/util/read-tsconfig'

const root = resolve(__dirname, 'fixtures/typescript')
const tsSource = 'src/hooks/init.ts'
Expand Down Expand Up @@ -77,7 +77,7 @@ describe('tsPath', () => {
})

it('should resolve .js with no rootDir and outDir', async () => {
sandbox.stub(util, 'readTSConfig').resolves({compilerOptions: {strict: true}})
sandbox.stub(util, 'readTSConfig').resolves({compilerOptions: {sourceMap: true}})
const result = await configTsNode.tsPath(root, jsCompiled)
expect(result).to.equal(join(root, jsCompiled))
})
Expand Down
7 changes: 1 addition & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,7 @@
dependencies:
"@types/color-convert" "*"

"@types/debug@^4.1.12":
"@types/debug@^4.1.10":
version "4.1.12"
resolved "https://registry.yarnpkg.com/@types/debug/-/debug-4.1.12.tgz#a155f21690871953410df4b6b6f53187f0500917"
integrity sha512-vIChWdVG3LG1SMxEvI/AK+FWJthlrqlTu7fbrlywTkkaONwk/UAGaULXRlf8vkzFBLVm0zkMdCquhL5aOjhXPQ==
Expand Down Expand Up @@ -6700,11 +6700,6 @@ ts-node@^10.8.1, ts-node@^10.9.1:
v8-compile-cache-lib "^3.0.1"
yn "3.1.1"

tsconfck@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/tsconfck/-/tsconfck-3.0.0.tgz#b469f1ced12973bbec3209a55ed8de3bb04223c9"
integrity sha512-w3wnsIrJNi7avf4Zb0VjOoodoO0woEqGgZGQm+LHH9przdUI+XDKsWAXwxHA1DaRTjeuZNcregSzr7RaA8zG9A==

tsconfig-paths@^3.10.1, tsconfig-paths@^3.14.2:
version "3.14.2"
resolved "https://registry.yarnpkg.com/tsconfig-paths/-/tsconfig-paths-3.14.2.tgz#6e32f1f79412decd261f92d633a9dc1cfa99f088"
Expand Down

0 comments on commit 79c4028

Please sign in to comment.