import * as path from 'path' ; import * as fs from 'fs' ; import parseJsonNicely = require ( 'parse-json' ) ; export function parseJson ( text , filename ? ) { try { return JSON . parse ( text ) ; } catch ( err ) { return parseJsonNicely ( text , filename ) ; } } export function addUnique < T > ( arr , ... args ) { return [ ... new Set ( [ ... arr , ... args ] ) ] ; } export function overwriteObject ( dest , src ) { Object . keys ( dest ) . forEach ( key => { delete dest [ key ] ; } ) ; return Object . assign ( dest , src ) ; } export function findFile ( leafDir , basename ) { let dir = leafDir ; let prevDir ; do { const filename = path . join ( dir , basename ) ; if ( fs . existsSync ( filename ) ) return filename ; prevDir = dir ; dir = path . dirname ( dir ) ; } while ( dir !== prevDir ) ; return null ; } export function findConfigFile ( leafDir ) { return findFile ( leafDir , ".ts-to-openapirc" ) ; } export function findPackageJsonFile ( leafDir ) { return findFile ( leafDir , "package.json" ) ; } export function readJson ( filename ) { return parseJson ( fs . readFileSync ( filename , 'utf8' ) ) ; } export function stripUndefined < T extends { } > ( t ) { const ret = { } as T ; for ( const [ key , value ] of Object . entries ( t ) ) { if ( value !== undefined ) ret [ key as keyof typeof ret ] = value as any ; } return ret ; } export function extractDefinedProperties < T , K extends keyof T > ( obj , keys ) { const ret = { } as any ; keys . forEach ( key => { if ( obj [ key ] !== undefined ) ret [ key ] = obj [ key ] ; } ) ; return ret ; }