Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(js): close typescript watch program on SIGINT/SIGTERM #11722

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 21 additions & 17 deletions packages/js/src/executors/tsc/tsc.impl.ts
Expand Up @@ -144,22 +144,6 @@ export async function* tscExecutor(
assets: _options.assets,
});

if (options.watch) {
const disposeWatchAssetChanges =
await assetHandler.watchAndProcessOnAssetChange();
const disposePackageJsonChanges = await watchForSingleFileChanges(
join(context.root, projectRoot),
'package.json',
() => updatePackageJson(options, context, target, dependencies)
);
const handleTermination = async () => {
await disposeWatchAssetChanges();
await disposePackageJsonChanges();
};
process.on('SIGINT', () => handleTermination());
process.on('SIGTERM', () => handleTermination());
}

const tsCompilationOptions = createTypeScriptCompilationOptions(
options,
context
Expand All @@ -175,7 +159,7 @@ export async function* tscExecutor(
tsCompilationOptions.rootDir = '.';
}

return yield* compileTypeScriptFiles(
const typescriptCompilation = compileTypeScriptFiles(
options,
tsCompilationOptions,
async () => {
Expand All @@ -188,6 +172,26 @@ export async function* tscExecutor(
);
}
);

if (options.watch) {
const disposeWatchAssetChanges =
await assetHandler.watchAndProcessOnAssetChange();
const disposePackageJsonChanges = await watchForSingleFileChanges(
join(context.root, projectRoot),
'package.json',
() => updatePackageJson(options, context, target, dependencies)
);
const handleTermination = async (exitCode: number) => {
await disposeWatchAssetChanges();
await disposePackageJsonChanges();
await typescriptCompilation.close();
process.exit(exitCode);
};
process.on('SIGINT', () => handleTermination(128 + 2));
process.on('SIGTERM', () => handleTermination(128 + 15));
}

return yield* typescriptCompilation.iterator;
}

export default tscExecutor;
65 changes: 45 additions & 20 deletions packages/js/src/utils/typescript/compile-typescript-files.ts
Expand Up @@ -16,33 +16,58 @@ function getErrorCountFromMessage(messageText: string) {
return Number.parseInt(ERROR_COUNT_REGEX.exec(messageText)[1]);
}

export async function* compileTypeScriptFiles(
export interface TypescriptCompilationResult {
success: boolean;
outfile: string;
}

export function compileTypeScriptFiles(
normalizedOptions: NormalizedExecutorOptions,
tscOptions: TypeScriptCompilationOptions,
postCompilationCallback: () => void | Promise<void>
) {
): {
iterator: AsyncIterable<TypescriptCompilationResult>;
close: () => void | Promise<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<TypescriptCompilationResult>(
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?.(),
};
}