Skip to content

Commit

Permalink
fix: dont drop ts-node options if there are no extends
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Nov 6, 2023
1 parent 6062edf commit 6d15be1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/util/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ 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 @@ -73,8 +75,6 @@ export function existsSync(path: string): boolean {
export async function readTSConfig(path: string) {
const {parse} = await import('tsconfck')
const result = await parse(path)
const tsNodeOpts = Object.fromEntries(
(result.extended ?? []).flatMap((e) => Object.entries(e.tsconfig['ts-node'] ?? {})).reverse(),
)
const tsNodeOpts = mergeNestedObjects(result.extended ?? [result], 'tsconfig.ts-node')
return {...result.tsconfig, 'ts-node': tsNodeOpts}
}
8 changes: 8 additions & 0 deletions src/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,11 @@ export function mapValues<T extends Record<string, any>, TResult>(
return o
}, {} as any)
}

function get(obj: Record<string, any>, path: string): unknown {
return path.split('.').reduce((o, p) => o?.[p], obj)
}

export function mergeNestedObjects(objs: Record<string, any>[], path: string): Record<string, any> {
return Object.fromEntries(objs.flatMap((o) => Object.entries(get(o, path) ?? {})).reverse())
}
34 changes: 33 additions & 1 deletion test/util/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {expect} from 'chai'

import {capitalize, castArray, isNotFalsy, isTruthy, last, maxBy, sumBy} from '../../src/util/util'
import {capitalize, castArray, isNotFalsy, isTruthy, last, maxBy, mergeNestedObjects, sumBy} from '../../src/util/util'

describe('capitalize', () => {
it('capitalizes the string', () => {
Expand Down Expand Up @@ -96,3 +96,35 @@ describe('castArray', () => {
expect(castArray()).to.deep.equal([])
})
})

describe('mergeNestedObjects', () => {
it('should merge nested objects', () => {
const a = {
tsconfig: {
compilerOptions: {
outDir: 'dist',
rootDir: 'src',
},
'ts-node': {
transpileOnly: true,
},
},
}

const b = {
tsconfig: {
compilerOptions: {
outDir: 'dist',
rootDir: 'src',
},
'ts-node': {
transpileOnly: false,
},
},
}

expect(mergeNestedObjects([a, b], 'tsconfig.ts-node')).to.deep.equal({
transpileOnly: true,
})
})
})

0 comments on commit 6d15be1

Please sign in to comment.