diff --git a/packages/js/src/executors/tsc/tsc.impl.ts b/packages/js/src/executors/tsc/tsc.impl.ts index c84cf908ca2d23..d1abf89c961246 100644 --- a/packages/js/src/executors/tsc/tsc.impl.ts +++ b/packages/js/src/executors/tsc/tsc.impl.ts @@ -125,6 +125,15 @@ export async function* tscExecutor( assets: _options.assets, }); + const { iterator, close } = compileTypeScriptFiles( + options, + createTypeScriptCompilationOptions(options, context), + async () => { + await assetHandler.processAllAssetsOnce(); + updatePackageJson(options, context, target, dependencies); + } + ); + if (options.watch) { const disposeWatchAssetChanges = await assetHandler.watchAndProcessOnAssetChange(); @@ -136,19 +145,13 @@ export async function* tscExecutor( const handleTermination = async () => { await disposeWatchAssetChanges(); await disposePackageJsonChanges(); + close(); }; process.on('SIGINT', () => handleTermination()); process.on('SIGTERM', () => handleTermination()); } - return yield* compileTypeScriptFiles( - options, - createTypeScriptCompilationOptions(options, context), - async () => { - await assetHandler.processAllAssetsOnce(); - updatePackageJson(options, context, target, dependencies); - } - ); + return yield* iterator; } export default tscExecutor; diff --git a/packages/js/src/utils/typescript/compile-typescript-files.ts b/packages/js/src/utils/typescript/compile-typescript-files.ts index 3c5c68e8c2df46..469af3ef629bd1 100644 --- a/packages/js/src/utils/typescript/compile-typescript-files.ts +++ b/packages/js/src/utils/typescript/compile-typescript-files.ts @@ -16,33 +16,50 @@ function getErrorCountFromMessage(messageText: string) { return Number.parseInt(ERROR_COUNT_REGEX.exec(messageText)[1]); } -export async function* compileTypeScriptFiles( +export function compileTypeScriptFiles( normalizedOptions: NormalizedExecutorOptions, tscOptions: TypeScriptCompilationOptions, postCompilationCallback: () => void | Promise -) { +): { iterator: AsyncIterable; close: () => void } { const getResult = (success: boolean) => ({ success, outfile: normalizedOptions.mainOutputPath, }); - return yield* createAsyncIterable<{ success: boolean; outfile: string }>( - async ({ next, done }) => { - if (normalizedOptions.watch) { - compileTypeScriptWatcher(tscOptions, async (d: Diagnostic) => { - if (d.code === TYPESCRIPT_FOUND_N_ERRORS_WATCHING_FOR_FILE_CHANGES) { - await postCompilationCallback(); - next( - getResult(getErrorCountFromMessage(d.messageText as string) === 0) - ); - } - }); - } else { - const { success } = compileTypeScript(tscOptions); - await postCompilationCallback(); - next(getResult(success)); - done(); + let tearDown: (() => void) | undefined; + + return { + iterator: createAsyncIterable<{ success: boolean; outfile: string }>( + async ({ next, done }) => { + if (normalizedOptions.watch) { + const host = compileTypeScriptWatcher( + tscOptions, + async (d: Diagnostic) => { + if ( + d.code === TYPESCRIPT_FOUND_N_ERRORS_WATCHING_FOR_FILE_CHANGES + ) { + await postCompilationCallback(); + next( + getResult( + getErrorCountFromMessage(d.messageText as string) === 0 + ) + ); + } + } + ); + + tearDown = () => { + host.close(); + done(); + }; + } else { + const { success } = compileTypeScript(tscOptions); + await postCompilationCallback(); + next(getResult(success)); + done(); + } } - } - ); + ), + close: () => tearDown?.(), + }; }