import Fs from 'fs' ; import Path from 'path' ; import { parseConfigFileTextToJson } from 'typescript' ; type TsConfigFile = ReturnType < typeof parseConfigFileTextToJson > [ 'config' ] ; export function parseTsConfig ( tsConfigPath ) { const { error , config } = parseConfigFileTextToJson ( tsConfigPath , Fs . readFileSync ( tsConfigPath , 'utf8' ) ) ; if ( error ) { throw error ; } return config ; } export function getOutputsDeep ( tsConfigPaths ) { const tsConfigs = new Map < string , TsConfigFile > ( ) ; const read = ( path ) => { const cached = tsConfigs . get ( path ) ; if ( cached ) { return cached ; } const config = parseTsConfig ( path ) ; tsConfigs . set ( path , config ) ; return config ; } ; const outputDirs = [ ] ; const seen = new Set < TsConfigFile > ( ) ; const traverse = ( path ) => { const config = read ( path ) ; if ( seen . has ( config ) ) { return ; } seen . add ( config ) ; const dirname = Path . dirname ( path ) ; const relativeOutDir = config . compilerOptions ?. outDir ; if ( relativeOutDir ) { outputDirs . push ( Path . resolve ( dirname , relativeOutDir ) ) ; } const refs = config . references ; for ( const ref of refs ?? [ ] ) { traverse ( Path . resolve ( dirname , ref . path ) ) ; } } ; for ( const path of tsConfigPaths ) { traverse ( path ) ; } return outputDirs ; }