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

Close typescript watch program and complete iterator to ensure program can exit.
This commit is contained in:
Denis Frenademetz 2022-10-11 18:44:52 +02:00 committed by GitHub
parent b17893c358
commit f1a24d7498
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 66 additions and 37 deletions

View File

@ -144,22 +144,6 @@ export async function* tscExecutor(
assets: _options.assets, 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( const tsCompilationOptions = createTypeScriptCompilationOptions(
options, options,
context context
@ -175,7 +159,7 @@ export async function* tscExecutor(
tsCompilationOptions.rootDir = '.'; tsCompilationOptions.rootDir = '.';
} }
return yield* compileTypeScriptFiles( const typescriptCompilation = compileTypeScriptFiles(
options, options,
tsCompilationOptions, tsCompilationOptions,
async () => { async () => {
@ -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; export default tscExecutor;

View File

@ -16,33 +16,58 @@ function getErrorCountFromMessage(messageText: string) {
return Number.parseInt(ERROR_COUNT_REGEX.exec(messageText)[1]); 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, normalizedOptions: NormalizedExecutorOptions,
tscOptions: TypeScriptCompilationOptions, tscOptions: TypeScriptCompilationOptions,
postCompilationCallback: () => void | Promise<void> postCompilationCallback: () => void | Promise<void>
) { ): {
iterator: AsyncIterable<TypescriptCompilationResult>;
close: () => void | Promise<void>;
} {
const getResult = (success: boolean) => ({ const getResult = (success: boolean) => ({
success, success,
outfile: normalizedOptions.mainOutputPath, outfile: normalizedOptions.mainOutputPath,
}); });
return yield* createAsyncIterable<{ success: boolean; outfile: string }>( let tearDown: (() => void) | undefined;
async ({ next, done }) => {
if (normalizedOptions.watch) { return {
compileTypeScriptWatcher(tscOptions, async (d: Diagnostic) => { iterator: createAsyncIterable<TypescriptCompilationResult>(
if (d.code === TYPESCRIPT_FOUND_N_ERRORS_WATCHING_FOR_FILE_CHANGES) { async ({ next, done }) => {
await postCompilationCallback(); if (normalizedOptions.watch) {
next( const host = compileTypeScriptWatcher(
getResult(getErrorCountFromMessage(d.messageText as string) === 0) tscOptions,
); async (d: Diagnostic) => {
} if (
}); d.code === TYPESCRIPT_FOUND_N_ERRORS_WATCHING_FOR_FILE_CHANGES
} else { ) {
const { success } = compileTypeScript(tscOptions); await postCompilationCallback();
await postCompilationCallback(); next(
next(getResult(success)); getResult(
done(); getErrorCountFromMessage(d.messageText as string) === 0
)
);
}
}
);
tearDown = () => {
host.close();
done();
};
} else {
const { success } = compileTypeScript(tscOptions);
await postCompilationCallback();
next(getResult(success));
done();
}
} }
} ),
); close: () => tearDown?.(),
};
} }