import * as ts from 'typescript' ; import * as tsconfigPaths from 'tsconfig-paths' ; import { ExtraCommandLineOptions , File , Imports , LocationInFile , TsConfig , TsConfigPaths , } from '../types' ; import { relative , resolve } from 'path' ; import { FromWhat } from './common' ; import { addExportCore } from './export' ; import { addImportCore } from './import' ; import { isNodeDisabledViaComment } from './comment' ; import { processNode } from './nodeProcessor' ; import { readFileSync } from 'fs' ; const extractFilename = ( rootDir , path ) : string => { let name = relative ( rootDir , path ) . replace ( /([\\/]index)?\.[^.]*$/ , '' ) ; if ( ! ! name . match ( /\.d$/ ) ) { name = name . substr ( 0 , name . length - 2 ) ; } return name ; } ; const mapFile = ( rootDir , path , file , baseUrl , paths ? , extraOptions ? ) : File => { const imports = { } ; const exportNames = [ ] ; const exportLocations = [ ] ; const name = extractFilename ( rootDir , path ) ; const baseDir = resolve ( rootDir , baseUrl ) ; const tsconfigPathsMatcher = ( ! ! paths && tsconfigPaths . createMatchPath ( baseDir , paths ) ) || undefined ; const addImport = ( fw ) : string | undefined => { return addImportCore ( fw , rootDir , path , imports , baseDir , baseUrl , tsconfigPathsMatcher ) ; } ; const addExport = ( exportName , node ) : void => { addExportCore ( exportName , file , node , exportLocations , exportNames , extraOptions ) ; } ; ts . forEachChild ( file , ( node ) => { if ( isNodeDisabledViaComment ( node , file ) ) { return ; } processNode ( node , path , addImport , addExport , imports , exportNames , extraOptions ) ; } ) ; return { path : name , fullPath : path , imports , exports : exportNames , exportLocations , } ; } ; const parseFile = ( rootDir , path , baseUrl , paths ? , extraOptions ? ) : File => mapFile ( rootDir , path , ts . createSourceFile ( path , readFileSync ( path , { encoding : 'utf8' } ) , ts . ScriptTarget . ES2015 , true ) , baseUrl , paths , extraOptions ) ; const parsePaths = ( rootDir , { baseUrl , files : filePaths , paths } , extraOptions ? ) : File [ ] => { const includeDeclarationFiles = ! extraOptions ?. excludeDeclarationFiles ; const files = filePaths . filter ( ( p ) => includeDeclarationFiles || ! p . includes ( '.d.' ) ) . map ( ( path ) => parseFile ( rootDir , resolve ( rootDir , path ) , baseUrl , paths , extraOptions ) ) ; return files ; } ; export default ( rootDir , TsConfig , extraOptions ? ) : File [ ] => { return parsePaths ( rootDir , TsConfig , extraOptions ) ; } ;