Skip to content

Commit 9b1b4c9

Browse files
fixes for Windows
1 parent fe69502 commit 9b1b4c9

File tree

4 files changed

+71
-16
lines changed

4 files changed

+71
-16
lines changed

src/providers/espresso.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { io, Socket } from 'socket.io-client';
88
import TestingBotError from '../models/testingbot_error';
99
import utils from '../utils';
1010
import Upload from '../upload';
11+
import platform from '../utils/platform';
1112

1213
export interface EspressoRunInfo {
1314
id: number;
@@ -395,7 +396,7 @@ export default class Espresso {
395396
}
396397

397398
private clearLine(): void {
398-
process.stdout.write('\r\x1b[K');
399+
platform.clearLine();
399400
}
400401

401402
private formatElapsedTime(seconds: number): string {
@@ -534,14 +535,12 @@ export default class Espresso {
534535
this.handleShutdown();
535536
};
536537

537-
process.on('SIGINT', this.signalHandler);
538-
process.on('SIGTERM', this.signalHandler);
538+
platform.setupSignalHandlers(this.signalHandler);
539539
}
540540

541541
private removeSignalHandlers(): void {
542542
if (this.signalHandler) {
543-
process.removeListener('SIGINT', this.signalHandler);
544-
process.removeListener('SIGTERM', this.signalHandler);
543+
platform.removeSignalHandlers(this.signalHandler);
545544
this.signalHandler = null;
546545
}
547546
}

src/providers/maestro.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import TestingBotError from '../models/testingbot_error';
1313
import utils from '../utils';
1414
import Upload from '../upload';
1515
import { detectPlatformFromFile } from '../utils/file-type-detector';
16+
import platform from '../utils/platform';
1617

1718
export interface MaestroRunAssets {
1819
logs?: string[];
@@ -720,7 +721,7 @@ export default class Maestro {
720721
}
721722

722723
private clearLine(): void {
723-
process.stdout.write('\r\x1b[K');
724+
platform.clearLine();
724725
}
725726

726727
private formatElapsedTime(seconds: number): string {
@@ -852,7 +853,7 @@ export default class Maestro {
852853
responseType: 'arraybuffer',
853854
headers: {
854855
'User-Agent': utils.getUserAgent(),
855-
}
856+
},
856857
});
857858

858859
await fs.promises.writeFile(filePath, response.data);
@@ -1112,14 +1113,12 @@ export default class Maestro {
11121113
this.handleShutdown();
11131114
};
11141115

1115-
process.on('SIGINT', this.signalHandler);
1116-
process.on('SIGTERM', this.signalHandler);
1116+
platform.setupSignalHandlers(this.signalHandler);
11171117
}
11181118

11191119
private removeSignalHandlers(): void {
11201120
if (this.signalHandler) {
1121-
process.removeListener('SIGINT', this.signalHandler);
1122-
process.removeListener('SIGTERM', this.signalHandler);
1121+
platform.removeSignalHandlers(this.signalHandler);
11231122
this.signalHandler = null;
11241123
}
11251124
}

src/providers/xcuitest.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { io, Socket } from 'socket.io-client';
88
import TestingBotError from '../models/testingbot_error';
99
import utils from '../utils';
1010
import Upload from '../upload';
11+
import platform from '../utils/platform';
1112

1213
export interface XCUITestRunInfo {
1314
id: number;
@@ -395,7 +396,7 @@ export default class XCUITest {
395396
}
396397

397398
private clearLine(): void {
398-
process.stdout.write('\r\x1b[K');
399+
platform.clearLine();
399400
}
400401

401402
private formatElapsedTime(seconds: number): string {
@@ -537,14 +538,12 @@ export default class XCUITest {
537538
this.handleShutdown();
538539
};
539540

540-
process.on('SIGINT', this.signalHandler);
541-
process.on('SIGTERM', this.signalHandler);
541+
platform.setupSignalHandlers(this.signalHandler);
542542
}
543543

544544
private removeSignalHandlers(): void {
545545
if (this.signalHandler) {
546-
process.removeListener('SIGINT', this.signalHandler);
547-
process.removeListener('SIGTERM', this.signalHandler);
546+
platform.removeSignalHandlers(this.signalHandler);
548547
this.signalHandler = null;
549548
}
550549
}

src/utils/platform.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Cross-platform utilities for terminal operations and signal handling
3+
*/
4+
5+
const isWindows = process.platform === 'win32';
6+
7+
/**
8+
* Clear the current line in the terminal.
9+
* Uses ANSI escape codes on Unix/macOS, and space overwrite on Windows.
10+
*/
11+
export function clearLine(): void {
12+
if (isWindows) {
13+
// Windows fallback: overwrite with spaces and return to start
14+
// Use readline if available for better Windows support
15+
if (process.stdout.clearLine && process.stdout.cursorTo) {
16+
process.stdout.clearLine(0);
17+
process.stdout.cursorTo(0);
18+
} else {
19+
// Fallback: write spaces to clear typical line width
20+
process.stdout.write('\r' + ' '.repeat(120) + '\r');
21+
}
22+
} else {
23+
// Unix/macOS: ANSI escape sequence
24+
process.stdout.write('\r\x1b[K');
25+
}
26+
}
27+
28+
/**
29+
* Setup signal handlers for graceful shutdown.
30+
* SIGINT (Ctrl+C) works on all platforms.
31+
* SIGTERM only works on Unix/macOS.
32+
*/
33+
export function setupSignalHandlers(handler: () => void): void {
34+
process.on('SIGINT', handler);
35+
36+
// SIGTERM is not supported on Windows
37+
if (!isWindows) {
38+
process.on('SIGTERM', handler);
39+
}
40+
}
41+
42+
/**
43+
* Remove signal handlers.
44+
*/
45+
export function removeSignalHandlers(handler: () => void): void {
46+
process.removeListener('SIGINT', handler);
47+
48+
if (!isWindows) {
49+
process.removeListener('SIGTERM', handler);
50+
}
51+
}
52+
53+
export default {
54+
isWindows,
55+
clearLine,
56+
setupSignalHandlers,
57+
removeSignalHandlers,
58+
};

0 commit comments

Comments
 (0)