diff --git a/src/common/typescript/utils.ts b/src/common/typescript/utils.ts index 8ec226f..4c4e373 100644 --- a/src/common/typescript/utils.ts +++ b/src/common/typescript/utils.ts @@ -84,7 +84,7 @@ export function onHostEvent>) => void, - after?: (res: ReturnType>) => void, + after?: (res: ReturnType>, ...args: Parameters>) => void, ) { const originalFunction = host[functionName]; @@ -100,7 +100,7 @@ export function onHostEvent { + (_rootnames, _options, host) => { logger.verbose("We're about to create the program"); - // @ts-expect-error - host.readFile.enableDisplay(); - }, - () => { - // @ts-expect-error - const count = host.readFile.disableDisplay(); - logger.verbose(`Program created, read ${count} files`); - }, - ); - onHostEvent( - host, - 'afterProgramCreate', - (program) => { - logger.verbose('We finished making the program! Emitting...'); - const transformPathsToLocalModules = createTransformPathsToLocalModules(ts); - program.emit(undefined, undefined, undefined, undefined, { - after: [transformPathsToLocalModules], - afterDeclarations: [transformPathsToLocalModules], - }); - logger.verbose('Emit completed!'); + if (host) { + host.readFile = displayFilename(host.readFile, 'Reading', logger); + + // @ts-expect-error + host.readFile.enableDisplay(); + } }, - () => { - onAfterFilesEmitted?.(); + (_result, _rootnames, _options, host) => { + if (host) { + // @ts-expect-error + const count = host.readFile.disableDisplay(); + + logger.verbose(`Program created, read ${count} files`); + } }, ); - // `createWatchProgram` creates an initial program, watches files, and updates + onHostEvent(host, 'afterProgramEmitAndDiagnostics', (program) => { + const project = program.getCompilerOptions().configFilePath; + + logger.verbose( + typeof project === 'string' + ? `Emit completed for ${project.replace(process.cwd(), '')}!` + : 'Emit completed!', + ); + }); + + // `createSolutionBuilderWithWatch` creates an initial program, watches files, and updates // the program over time. - ts.createWatchProgram(host); + const solutionBuilder = ts.createSolutionBuilderWithWatch(host, [configPath], { + noEmit: false, + noEmitOnError: false, + inlineSourceMap: enableSourceMap, + inlineSources: enableSourceMap, + ...(enableSourceMap ? {sourceMap: false} : undefined), + }); + + const transformPathsToLocalModules = createTransformPathsToLocalModules(ts); + + solutionBuilder.build(undefined, undefined, undefined, () => ({ + after: [transformPathsToLocalModules], + afterDeclarations: [transformPathsToLocalModules], + })); function reportDiagnostic(diagnostic: Typescript.Diagnostic) { const formatHost = { @@ -93,5 +104,12 @@ export function watch( if (diagnostic.messageText) { logger.message(ts.flattenDiagnosticMessageText(diagnostic.messageText, ts.sys.newLine)); } + + if ( + diagnostic.code === COMPILATION_COMPLETE_WITH_ERROR || + diagnostic.code === COMPILATION_COMPLETE_WITH_N_ERRORS + ) { + onAfterFilesEmitted?.(); + } } }