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 committed Aug 25, 2022
1 parent 2673a04 commit 156c698
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 24 deletions.
15 changes: 11 additions & 4 deletions packages/js/src/executors/tsc/tsc.impl.ts
Expand Up @@ -82,6 +82,15 @@ export async function* tscExecutor(
assets: _options.assets,
});

const { iterator, close } = compileTypeScriptFiles(
options,
context,
async () => {
await assetHandler.processAllAssetsOnce();
updatePackageJson(options, context, target, dependencies);
}
);

if (options.watch) {
const disposeWatchAssetChanges =
await assetHandler.watchAndProcessOnAssetChange();
Expand All @@ -93,15 +102,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, 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 @@ -24,11 +24,11 @@ function getErrorCountFromMessage(messageText: string) {
return Number.parseInt(ERROR_COUNT_REGEX.exec(messageText)[1]);
}

export async function* compileTypeScriptFiles(
export function compileTypeScriptFiles(
normalizedOptions: NormalizedExecutorOptions,
context: ExecutorContext,
postCompilationCallback: () => void | Promise<void>
) {
): { iterator: AsyncIterable<any>; close: () => void } {
const getResult = (success: boolean) => ({
success,
outfile: normalizedOptions.mainOutputPath,
Expand Down Expand Up @@ -60,23 +60,40 @@ export async function* compileTypeScriptFiles(
getCustomTransformers,
};

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 156c698

Please sign in to comment.