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,
});
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
@ -175,7 +159,7 @@ export async function* tscExecutor(
tsCompilationOptions.rootDir = '.';
}
return yield* compileTypeScriptFiles(
const typescriptCompilation = compileTypeScriptFiles(
options,
tsCompilationOptions,
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;

View File

@ -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?.(),
};
}