Skip to content

Commit

Permalink
"tsc --watch should clear screen on new compilation" Two: Electric Bo…
Browse files Browse the repository at this point in the history
…ogaloo (#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
  • Loading branch information
Josh Goldberg authored and mhegazy committed Dec 2, 2017
1 parent e90bf5e commit 08c6dc9
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/compiler/sys.ts
Expand Up @@ -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 {
Expand Down Expand Up @@ -436,6 +437,9 @@ namespace ts {
}

const nodeSystem: System = {
clearScreen: () => {
process.stdout.write("\x1Bc");
},
args: process.argv.slice(2),
newLine: _os.EOL,
useCaseSensitiveFileNames,
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/watch.ts
Expand Up @@ -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));

Expand Down
33 changes: 33 additions & 0 deletions src/harness/unittests/tscWatchMode.ts
Expand Up @@ -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);
});
});
}
9 changes: 9 additions & 0 deletions src/harness/virtualFileSystemWithWatch.ts
Expand Up @@ -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>();
Expand Down Expand Up @@ -604,6 +605,10 @@ interface Array<T> {}`
this.timeoutCallbacks.unregister(timeoutId);
}

clearScreen(): void {
this.screenClears += 1;
}

checkTimeoutQueueLengthAndRun(expected: number) {
this.checkTimeoutQueueLength(expected);
this.runQueuedTimeoutCallbacks();
Expand Down Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/typescript.d.ts
Expand Up @@ -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;
Expand Down

0 comments on commit 08c6dc9

Please sign in to comment.