import { writeFileSync } from 'fs' ; import { join } from 'path' ; import { Project } from 'ts-morph' ; import { isDefinedButNeverUsed , customTypes } from './model/utils' ; const specsFolder = join ( __dirname , '..' , '..' , 'specification' ) ; const tsConfigFilePath = join ( specsFolder , 'tsconfig.json' ) ; function findDanglingTypes ( ) { const project = new Project ( { tsConfigFilePath } ) ; const definedButNeverUsed = [ ] ; for ( const sourceFile of project . getSourceFiles ( ) ) { for ( const declaration of sourceFile . getClasses ( ) ) { if ( customTypes . includes ( declaration . getName ( ) ?? '' ) ) continue ; if ( isDefinedButNeverUsed ( declaration ) ) { definedButNeverUsed . push ( `${ declaration . getName ( ) ?? '' },${ formatDanglingPath ( declaration . getSourceFile ( ) . getFilePath ( ) ) }` ) ; } } for ( const declaration of sourceFile . getInterfaces ( ) ) { if ( isDefinedButNeverUsed ( declaration ) ) { definedButNeverUsed . push ( `${ declaration . getName ( ) ?? '' },${ formatDanglingPath ( declaration . getSourceFile ( ) . getFilePath ( ) ) }` ) ; } } for ( const declaration of sourceFile . getEnums ( ) ) { if ( isDefinedButNeverUsed ( declaration ) ) { definedButNeverUsed . push ( `${ declaration . getName ( ) ?? '' },${ formatDanglingPath ( declaration . getSourceFile ( ) . getFilePath ( ) ) }` ) ; } } for ( const declaration of sourceFile . getTypeAliases ( ) ) { if ( isDefinedButNeverUsed ( declaration ) ) { definedButNeverUsed . push ( `${ declaration . getName ( ) ?? '' },${ formatDanglingPath ( declaration . getSourceFile ( ) . getFilePath ( ) ) }` ) ; } } } writeFileSync ( join ( __dirname , '..' , '..' , 'output' , 'dangling-types' , 'dangling.csv' ) , definedButNeverUsed . join ( '\n' ) , 'utf8' ) ; } function formatDanglingPath ( path ) { return path . replace ( /.*[/\\]specification[/\\]?/ , '' ) ; } findDanglingTypes ( ) ; console . log ( 'Done!' ) ;