Skip to content

Commit

Permalink
fix(js): close typescript watch program on SIGINT/SIGTERM (#11722)
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 Oct 11, 2022
1 parent b17893c commit f1a24d7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 37 deletions.
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?.(),
};
}

0 comments on commit f1a24d7

Please sign in to comment.