import ts from "typescript" ; import fs from "fs" ; import path from "path" ; import getConfig from "./getConfig" ; const cwd = process . cwd ( ) ; const config = getConfig ( ) ; export async function typeCheck ( entries , dir ) { const compilerOptions = await getCompilerOptions ( dir ) ; compilerOptions . noEmit = true ; const program = ts . createProgram ( { options : compilerOptions , rootNames : entries } ) ; const diagnosticsHost = ts . createCompilerHost ( compilerOptions ) ; const diagnostics = ts . getPreEmitDiagnostics ( program ) ; if ( diagnostics . length > 0 ) { const formatted = ts . formatDiagnosticsWithColorAndContext ( diagnostics , diagnosticsHost ) ; console . log ( formatted ) ; return false ; } return true ; } async function getCompilerOptions ( dir ) { const basePath = path . join ( cwd , dir ) ; const configFile = config . tsconfigPath || ts . findConfigFile ( basePath , ( name ) => fs . existsSync ( name ) ) ; if ( configFile ) { const configJson = await fs . promises . readFile ( configFile , { encoding : "utf8" } ) ; const config = ts . parseConfigFileTextToJson ( configFile , configJson ) ; if ( config . error ) { const host = ts . createCompilerHost ( config . config ) ; const message = ts . formatDiagnostic ( config . error , host ) ; console . log ( message ) ; } const { options : compilerOptions } = ts . convertCompilerOptionsFromJson ( config . config . compilerOptions , basePath ) ; return compilerOptions ; } return ts . getDefaultCompilerOptions ( ) ; }