import * as path from 'path' ; import * as ts from 'typescript' ; import { isInBuildMode , performBuild , performCheck } from './build' ; import { createCompilerHost } from './compiler_host' ; import { createDiagnosticsReporter , FORMAT_DIAGNOSTIC_HOST } from './report' ; /**
 * A simple tsc wrapper that runs TSConformance checks over the source files
 * and emits code for files without conformance violations.
 */ function main ( args ) { if ( isInBuildMode ( args ) ) { return performBuild ( args . slice ( 1 ) ) === 0 ? 0 : 1 ; } let parsedConfig = ts . parseCommandLine ( args ) ; if ( parsedConfig . errors . length !== 0 ) { ts . sys . write ( ts . formatDiagnostics ( parsedConfig . errors , FORMAT_DIAGNOSTIC_HOST ) ) ; return 1 ; } if ( parsedConfig . fileNames . length === 0 ) { let tsConfigFilePath = parsedConfig . options . project ; if ( tsConfigFilePath === undefined ) { tsConfigFilePath = ts . findConfigFile ( '.' , ts . sys . fileExists ) ; if ( tsConfigFilePath === undefined ) { ts . sys . write ( 'tsec: Cannot find project configuration.' ) ; ts . sys . write ( ts . sys . newLine ) ; return 1 ; } } if ( ts . sys . directoryExists ( tsConfigFilePath ) ) { tsConfigFilePath = path . resolve ( tsConfigFilePath , 'tsconfig.json' ) ; } if ( ! ts . sys . fileExists ( tsConfigFilePath ) ) { ts . sys . write ( 'tsec: Cannot find project configuration.' ) ; ts . sys . write ( ts . sys . newLine ) ; return 1 ; } const parseConfigFileHost = { ... ts . sys , onUnRecoverableConfigFileDiagnostic : ( diagnostic ) => { ts . sys . write ( ts . formatDiagnostic ( diagnostic , FORMAT_DIAGNOSTIC_HOST ) ) ; ts . sys . exit ( 1 ) ; } } ; parsedConfig = ts . getParsedCommandLineOfConfigFile ( tsConfigFilePath , parsedConfig . options , parseConfigFileHost ) ! ; } const diagnostics = [ ... parsedConfig . errors ] ; const compilerHost = createCompilerHost ( parsedConfig ) ; const program = ts . createProgram ( parsedConfig . fileNames , parsedConfig . options , compilerHost ) ; diagnostics . push ( ... ts . getPreEmitDiagnostics ( program ) , ... performCheck ( program ) ) ; if ( diagnostics . length !== 0 && parsedConfig . options . noEmitOnError === true ) { program . getCompilerOptions ( ) . noEmit = true ; } const result = program . emit ( ) ; diagnostics . push ( ... result . diagnostics ) ; const reportDiagnostics = createDiagnosticsReporter ( parsedConfig . options ) ; const errorCount = reportDiagnostics ( diagnostics , true ) ; return errorCount === 0 ? 0 : 1 ; } process . exitCode = main ( process . argv . slice ( 2 ) ) ;