From 6d15be1bb7b7cfd0f1967d911427ef2f5cf74a86 Mon Sep 17 00:00:00 2001 From: Mike Donnalley Date: Mon, 6 Nov 2023 09:03:06 -0700 Subject: [PATCH] fix: dont drop ts-node options if there are no extends --- src/util/fs.ts | 6 +++--- src/util/util.ts | 8 ++++++++ test/util/util.test.ts | 34 +++++++++++++++++++++++++++++++++- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/util/fs.ts b/src/util/fs.ts index 2afc49591..0e09e7c02 100644 --- a/src/util/fs.ts +++ b/src/util/fs.ts @@ -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(...pathParts: string[]): T { return JSON.parse(readFileSync(join(...pathParts), 'utf8')) } @@ -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} } diff --git a/src/util/util.ts b/src/util/util.ts index 03022a8a6..6b3048a27 100644 --- a/src/util/util.ts +++ b/src/util/util.ts @@ -97,3 +97,11 @@ export function mapValues, TResult>( return o }, {} as any) } + +function get(obj: Record, path: string): unknown { + return path.split('.').reduce((o, p) => o?.[p], obj) +} + +export function mergeNestedObjects(objs: Record[], path: string): Record { + return Object.fromEntries(objs.flatMap((o) => Object.entries(get(o, path) ?? {})).reverse()) +} diff --git a/test/util/util.test.ts b/test/util/util.test.ts index 1507bf752..d2c00b57b 100644 --- a/test/util/util.test.ts +++ b/test/util/util.test.ts @@ -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', () => { @@ -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, + }) + }) +})