54 lines
1.6 KiB
JavaScript
54 lines
1.6 KiB
JavaScript
/**
|
||
* Pear often runs without `process.stdin` / `process.stdout`; Holepunch uses `bare-stdio`
|
||
* on fds 0–2 (same idea as pear-terminal's stdio singleton).
|
||
*/
|
||
|
||
import { bindReplDebugStream, isReplDebug, replDbg } from './debug-repl.js'
|
||
|
||
/**
|
||
* @returns {Promise<{ stdin: import('stream').Readable | null, stdout: import('stream').Writable | null }>}
|
||
*/
|
||
export async function resolveStdio() {
|
||
const p = globalThis.process
|
||
if (p?.stdin && p?.stdout) {
|
||
if (isReplDebug()) {
|
||
bindReplDebugStream(p.stdout)
|
||
replDbg(
|
||
'resolve-stdio',
|
||
'process stdin/stdout',
|
||
JSON.stringify({
|
||
stdinIsTTY: p.stdin.isTTY,
|
||
stdoutColumns: p.stdout.columns,
|
||
hasCursorTo: typeof p.stdout.cursorTo,
|
||
hasClearLine: typeof p.stdout.clearLine
|
||
})
|
||
)
|
||
}
|
||
return { stdin: p.stdin, stdout: p.stdout }
|
||
}
|
||
try {
|
||
const mod = await import('bare-stdio')
|
||
const io = mod.default?.in ? mod.default : mod
|
||
if (io?.in && io?.out) {
|
||
if (isReplDebug()) {
|
||
bindReplDebugStream(io.out)
|
||
replDbg(
|
||
'resolve-stdio',
|
||
'bare-stdio',
|
||
JSON.stringify({
|
||
stdinIsTTY: io.in.isTTY,
|
||
stdoutColumns: io.out.columns,
|
||
hasCursorTo: typeof io.out.cursorTo,
|
||
hasClearLine: typeof io.out.clearLine
|
||
})
|
||
)
|
||
}
|
||
return { stdin: io.in, stdout: io.out }
|
||
}
|
||
} catch (e) {
|
||
if (isReplDebug()) replDbg('resolve-stdio', 'bare-stdio unavailable', String(e))
|
||
}
|
||
if (isReplDebug()) replDbg('resolve-stdio', 'no stdin/stdout')
|
||
return { stdin: null, stdout: null }
|
||
}
|