Repository navigation
Expand file tree
/
Copy pathio.ts
More file actions
111 lines (106 loc) · 4.39 KB
/
Copy pathio.ts
File metadata and controls
111 lines (106 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { CliError } from './cli-error.js';
/**
* The CLI's IO seam — injected so the command and process logic is testable with no real
* TTY, stdout, or environment. The `bin` entry wires this to `process`; tests pass a capture.
*/
export interface CliIo {
/** Captured native TUI/prompt sink. Memory-only hosts omit it and cannot silently acquire the real TTY. */
readonly nativeStdout?: NodeJS.WriteStream;
/** Captured native renderer error sink; owned TUI acquisition never borrows a later process global. */
readonly nativeStderr?: NodeJS.WriteStream;
/** Native Ink input from the same captured invocation; line-only hosts retain stdin without this field. */
readonly nativeStdin?: NodeJS.ReadStream;
writeOut(text: string): void;
writeErr(text: string): void;
/** Actual delivery to the same stdout; enqueueing or cancellation is never a restoration ACK. */
writeOutAcknowledged(text: string): Promise<void>;
/** Acknowledge delivery to the same sink as writeErr; rejection never contains the stream's error text. */
writeErrAcknowledged(text: string): Promise<void>;
readonly env: Readonly<Record<string, string | undefined>>;
/** Whether stdout is a TTY (`process.stdout.isTTY`). */
readonly stdoutIsTty: boolean;
/** Whether stdin is a TTY (`process.stdin.isTTY`) — an interactive prompt (the `create` wizard) needs it to
* read keystrokes; a non-TTY stdin (a pipe/redirect) makes `@clack/prompts` fail, so callers guard on it. */
readonly stdinIsTty: boolean;
/** Input stream for line-reading surfaces (the plain `chat` loop). Always provided — {@link processIo} wires
* `process.stdin`, and `captureIo` supplies an empty stub; `drivePlain` reads it directly with no fallback,
* so a miswired test cannot silently read the real `process.stdin`. */
readonly stdin: NodeJS.ReadableStream;
}
/** The real-process IO seam used by the `bin` entry. */
export function processIo(): CliIo {
const { stdout, stderr, stdin } = process;
return {
nativeStdout: stdout,
nativeStderr: stderr,
nativeStdin: stdin,
writeOut: (text) => {
writeNative(stdout, text);
},
writeErr: (text) => {
writeNative(stderr, text);
},
writeOutAcknowledged: (text) => acknowledgeWrite(stdout, text),
writeErrAcknowledged: (text) => acknowledgeWrite(stderr, text),
env: process.env,
stdoutIsTty: stdout.isTTY === true,
stdinIsTty: stdin.isTTY === true,
stdin,
};
}
/** Classify failures at the exact native write, without blocking entered terminal restoration. */
function writeNative(stream: NodeJS.WriteStream, text: string): void {
try {
stream.write(text);
} catch (cause) {
throw new CliError('output_unavailable', '', { cause });
}
}
export function canWrite(stream: NodeJS.WriteStream): boolean {
return (
stream.writable &&
!stream.destroyed &&
!stream.writableEnded &&
!stream.writableFinished &&
stream.errored === null
);
}
/** Native write callbacks acknowledge delivery; returning from write() only acknowledges enqueueing. */
export function acknowledgeWrite(stream: NodeJS.WriteStream, text: string): Promise<void> {
return new Promise<void>((resolve, reject) => {
let pending = true;
const release = (): void => {
stream.removeListener('error', failed);
stream.removeListener('close', failed);
stream.removeListener('error', release);
stream.removeListener('close', release);
};
const finish = (success: boolean): void => {
if (!pending) return;
pending = false;
// A native failed write invokes its callback before destruction finishes (a file descriptor can
// close asynchronously). Keep this owned error sink until error/close; already-finished
// destruction can still have queued events, which run before the next immediate.
if (stream.destroyed && !stream.closed) {
stream.once('error', release);
stream.once('close', release);
} else setImmediate(release);
if (success) resolve();
else reject(new CliError('output_unavailable', ''));
};
const failed = (): void => finish(false);
stream.on('error', failed);
stream.on('close', failed);
if (!canWrite(stream)) {
finish(false);
return;
}
try {
stream.write(text, (error) =>
finish(error === undefined || error === null ? canWrite(stream) : false),
);
} catch {
finish(false);
}
});
}