import chalk = require ( 'chalk' ) ; import analyzeTsConfig from './app' ; import { extractOptionsFromFiles , hasValidArgs } from './argsParser' ; import { LocationInFile , Analysis , ExtraCommandLineOptions } from './types' ; import { USAGE } from './usage' ; enum ExitCode { NoUnusedExportsFound = 0 , UnusedExportsFound = 1 , BadArgsOrException = 2 } const getLocationInFile = ( location ) : string => { if ( ! location ) { return '' ; } return `[${ location . line },${ location . character }]` ; } ; const showMessages = ( files , showMessage , analysis , options ) : void => { const filesCountMessage = `${ chalk . bold ( files . length . toString ( ) ) } module${ files . length == 1 ? '' : 's' } with unused exports` ; showMessage ( files . length ? chalk . red ( filesCountMessage ) : filesCountMessage ) ; if ( options ?. showLineNumber ) { files . forEach ( ( path ) => { analysis [ path ] . forEach ( ( unusedExport ) => { showMessage ( `${ path }${ getLocationInFile ( unusedExport . location ) }: ${ chalk . bold . yellow ( unusedExport . exportName ) }` ) ; } ) ; } ) ; } else { files . forEach ( ( path ) => showMessage ( `${ path }: ${ chalk . bold . yellow ( analysis [ path ] . map ( ( r ) => r . exportName ) . join ( ', ' ) ) }` ) ) ; } } ; export const runCli = ( exitWith , showError , showMessage , [ tsconfig , ... tsFiles ] ) : ExitCode => { if ( ! hasValidArgs ( showError , tsconfig , tsFiles ) ) { showError ( USAGE ) ; return exitWith ( ExitCode . BadArgsOrException ) ; } try { const analysis = analyzeTsConfig ( tsconfig , tsFiles . length ? tsFiles : undefined ) ; const files = Object . keys ( analysis ) ; const options = extractOptionsFromFiles ( tsFiles ) . options ; const hideMessages = options ?. silent && files . length === 0 ; if ( ! hideMessages ) { showMessages ( files , showMessage , analysis , options ) ; } const MAX_ALLOWED_EXIT_CODE = 127 ; if ( options ?. exitWithCount ) { return exitWith ( Math . min ( MAX_ALLOWED_EXIT_CODE , files . length ) ) ; } else if ( options ?. exitWithUnusedTypesCount ) { const totalIssues = files . map ( ( f ) => analysis [ f ] . length ) . reduce ( ( previous , current ) => previous + current , 0 ) ; return exitWith ( Math . min ( MAX_ALLOWED_EXIT_CODE , totalIssues ) ) ; } const maxIssues = options ?. maxIssues || 0 ; return exitWith ( files . length <= maxIssues ? ExitCode . NoUnusedExportsFound : ExitCode . UnusedExportsFound ) ; } catch ( e ) { showError ( e ) ; return exitWith ( ExitCode . BadArgsOrException ) ; } } ;