Skip to content

Commit

Permalink
fix(js): close typescript watch program on SIGINT/SIGTERM
Browse files Browse the repository at this point in the history
Close typescript watch program and complete iterator to ensure program can exit.
  • Loading branch information
skrtheboss authored and Chau Tran committed Sep 26, 2022
1 parent 25e53b3 commit 4c27e3a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 28 deletions.
19 changes: 11 additions & 8 deletions packages/js/src/executors/tsc/tsc.impl.ts
Expand Up @@ -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();
Expand All @@ -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;
57 changes: 37 additions & 20 deletions packages/js/src/utils/typescript/compile-typescript-files.ts
Expand Up @@ -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<void>
) {
): { iterator: AsyncIterable<any>; 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?.(),
};
}

0 comments on commit 4c27e3a

Please sign in to comment.