"tsc --watch should clear screen on new compilation" Two: Electric Boogaloo (#20389)

* tsc --watch should clear screen on new compilation

* added optional clearScreen method to System]
* implemented via `x1Bc`, reset screen
* fixes 13020

* Feedback on if statements; api .d.ts baseline additions

* Stopped clearing screen in tsc.js's reportWatchMode

* Added unit tests
This commit is contained in:
Josh Goldberg 2017-12-02 11:11:49 -08:00 committed by Mohamed Hegazy
parent e90bf5e81b
commit 08c6dc99c6
6 changed files with 52 additions and 0 deletions

View file

@ -72,6 +72,7 @@ namespace ts {
/*@internal*/ debugMode?: boolean;
setTimeout?(callback: (...args: any[]) => void, ms: number, ...args: any[]): any;
clearTimeout?(timeoutId: any): void;
clearScreen?(): void;
}
export interface FileWatcher {
@ -436,6 +437,9 @@ namespace ts {
}
const nodeSystem: System = {
clearScreen: () => {
process.stdout.write("\x1Bc");
},
args: process.argv.slice(2),
newLine: _os.EOL,
useCaseSensitiveFileNames,

View file

@ -493,6 +493,10 @@ namespace ts {
}
function updateProgram() {
if (watchingHost.system.clearScreen) {
watchingHost.system.clearScreen();
}
timerToUpdateProgram = undefined;
reportWatchDiagnostic(createCompilerDiagnostic(Diagnostics.File_change_detected_Starting_incremental_compilation));

View file

@ -2027,4 +2027,37 @@ declare module "fs" {
assert.equal(host.readFile(outputFile1), file1.content + host.newLine);
});
});
describe("tsc-watch console clearing", () => {
it("doesn't clear the console when it starts", () => {
const file = {
path: "f.ts",
content: ""
};
const host = createWatchedSystem([file]);
createWatchModeWithoutConfigFile([file.path], host);
host.runQueuedTimeoutCallbacks();
host.checkScreenClears(0);
});
it("clears the console on recompile", () => {
const file = {
path: "f.ts",
content: ""
};
const host = createWatchedSystem([file]);
createWatchModeWithoutConfigFile([file.path], host);
const modifiedFile = {
...file,
content: "//"
};
host.reloadFS([modifiedFile]);
host.runQueuedTimeoutCallbacks();
host.checkScreenClears(1);
});
});
}

View file

@ -251,6 +251,7 @@ interface Array<T> {}`
private toPath: (f: string) => Path;
private timeoutCallbacks = new Callbacks();
private immediateCallbacks = new Callbacks();
private screenClears = 0;
readonly watchedDirectories = createMultiMap<TestDirectoryWatcher>();
readonly watchedDirectoriesRecursive = createMultiMap<TestDirectoryWatcher>();
@ -604,6 +605,10 @@ interface Array<T> {}`
this.timeoutCallbacks.unregister(timeoutId);
}
clearScreen(): void {
this.screenClears += 1;
}
checkTimeoutQueueLengthAndRun(expected: number) {
this.checkTimeoutQueueLength(expected);
this.runQueuedTimeoutCallbacks();
@ -638,6 +643,10 @@ interface Array<T> {}`
this.immediateCallbacks.unregister(timeoutId);
}
checkScreenClears(expected: number): void {
assert.equal(this.screenClears, expected);
}
createDirectory(directoryName: string): void {
const folder = this.toFolder(directoryName);

View file

@ -2762,6 +2762,7 @@ declare namespace ts {
realpath?(path: string): string;
setTimeout?(callback: (...args: any[]) => void, ms: number, ...args: any[]): any;
clearTimeout?(timeoutId: any): void;
clearScreen?(): void;
}
interface FileWatcher {
close(): void;

View file

@ -2762,6 +2762,7 @@ declare namespace ts {
realpath?(path: string): string;
setTimeout?(callback: (...args: any[]) => void, ms: number, ...args: any[]): any;
clearTimeout?(timeoutId: any): void;
clearScreen?(): void;
}
interface FileWatcher {
close(): void;