Skip to content

Commit

Permalink
feat: implement manual restart for ts watchmode
Browse files Browse the repository at this point in the history
  • Loading branch information
sheerlox committed Apr 2, 2023
1 parent 0711124 commit 72646c9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
9 changes: 9 additions & 0 deletions lib/compiler/helpers/manual-restart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function listenForManualRestart(callback: () => void) {
const stdinListener = (data: Buffer) => {
if (data.toString().replace('\n', '') === 'rs') {
process.stdin.removeListener('data', stdinListener);
callback();
}
};
process.stdin.on('data', stdinListener);
}
18 changes: 16 additions & 2 deletions lib/compiler/watch-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as ts from 'typescript';
import { Configuration } from '../configuration';
import { CLI_ERRORS } from '../ui/errors';
import { getValueOrDefault } from './helpers/get-value-or-default';
import { listenForManualRestart } from './helpers/manual-restart';
import { TsConfigProvider } from './helpers/tsconfig-provider';
import { tsconfigPathsBeforeHookFactory } from './hooks/tsconfig-paths.hook';
import { PluginsLoader } from './plugins-loader';
Expand Down Expand Up @@ -68,7 +69,9 @@ export class WatchCompiler {
host: ts.CompilerHost,
oldProgram: ts.EmitAndSemanticDiagnosticsBuilderProgram,
) => {
const tsconfigPathsPlugin = options ? tsconfigPathsBeforeHookFactory(options) : null;
const tsconfigPathsPlugin = options
? tsconfigPathsBeforeHookFactory(options)
: null;
const program = origCreateProgram(
rootNames,
options,
Expand Down Expand Up @@ -118,7 +121,18 @@ export class WatchCompiler {
return program as any;
};

tsBin.createWatchProgram(host);
const watchProgram = tsBin.createWatchProgram(host);

listenForManualRestart(() => {
watchProgram.close();
this.run(
configuration,
configFilename,
appName,
tsCompilerOptions,
onSuccess,
);
});
}

private createDiagnosticReporter(
Expand Down

0 comments on commit 72646c9

Please sign in to comment.