Skip to content

Commit

Permalink
fix: handle execution in non tty
Browse files Browse the repository at this point in the history
  • Loading branch information
kazushisan committed Sep 11, 2024
1 parent 16fc7ea commit 9ce74e4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
11 changes: 11 additions & 0 deletions lib/process.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
declare module 'node:process' {
import { Socket } from 'node:net';

interface Process extends NodeJS.Process {
stdout: Socket | NodeJS.Process['stdout'];
}

const process: Process;

export = process;
}
18 changes: 12 additions & 6 deletions lib/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ import { Logger } from './util/Logger.js';
import { stdout } from 'node:process';
import { CliEditTracker } from './util/CliEditTracker.js';

const createNodeJsLogger = (): Logger => ({
write: stdout.write.bind(stdout),
clearLine: stdout.clearLine.bind(stdout),
cursorTo: stdout.cursorTo.bind(stdout),
isTTY: stdout.isTTY,
});
const createNodeJsLogger = (): Logger =>
'isTTY' in stdout && stdout.isTTY
? {
write: stdout.write.bind(stdout),
clearLine: stdout.clearLine.bind(stdout),
cursorTo: stdout.cursorTo.bind(stdout),
isTTY: true,
}
: {
write: stdout.write.bind(stdout),
isTTY: false,
};

export const remove = ({
configPath,
Expand Down
17 changes: 11 additions & 6 deletions lib/util/Logger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
export interface Logger {
write(text: string): void;
clearLine(dir: -1 | 0 | 1): void;
cursorTo(x: number): void;
isTTY: boolean;
}
export type Logger =
| {
write(text: string): void;
clearLine(dir: -1 | 0 | 1): void;
cursorTo(x: number): void;
isTTY: true;
}
| {
write(text: string): void;
isTTY: false;
};

0 comments on commit 9ce74e4

Please sign in to comment.