Skip to content

Commit 73c2ef8

Browse files
authored
Merge pull request #208 from shaoohh/fix/cross-platform-stdin-input
fix: support stdin-backed file options on Windows
2 parents 6998d22 + 84fc33b commit 73c2ef8

3 files changed

Lines changed: 33 additions & 4 deletions

File tree

src/commands/text/chat.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type {
1616
} from '../../types/api';
1717
import { readFileSync } from 'fs';
1818
import { isInteractive } from '../../utils/env';
19+
import { readTextFromPathOrStdin } from '../../utils/fs';
1920
import { promptText, failIfMissing } from '../../utils/prompt';
2021

2122
// ---------------------------------------------------------------------------
@@ -104,9 +105,7 @@ function parseMessages(flags: GlobalFlags): ParsedMessages {
104105

105106
if (flags.messagesFile) {
106107
const filePath = flags.messagesFile as string;
107-
const raw = filePath === '-'
108-
? readFileSync('/dev/stdin', 'utf-8')
109-
: readFileSync(filePath, 'utf-8');
108+
const raw = readTextFromPathOrStdin(filePath);
110109
const parsed = JSON.parse(raw) as Array<{ role: string; content: string | ContentBlock[] }>;
111110
for (const m of parsed) {
112111
if (m.role === 'system') {

src/utils/fs.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { readFileSync } from 'fs';
22

3+
export function resolveTextInput(path: string): string | number {
4+
// File descriptor 0 is stdin on every platform supported by Node. `/dev/stdin`
5+
// only exists on POSIX systems, so using it breaks `--*-file -` on Windows.
6+
return path === '-' ? 0 : path;
7+
}
8+
39
export function readTextFromPathOrStdin(path: string): string {
4-
return readFileSync(path === '-' ? '/dev/stdin' : path, 'utf-8');
10+
return readFileSync(resolveTextInput(path), 'utf-8');
511
}

test/utils/fs.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { describe, expect, it } from 'bun:test';
2+
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
3+
import { tmpdir } from 'node:os';
4+
import { join } from 'node:path';
5+
import { readTextFromPathOrStdin, resolveTextInput } from '../../src/utils/fs';
6+
7+
describe('readTextFromPathOrStdin', () => {
8+
it('uses file descriptor zero for the stdin marker on every platform', () => {
9+
expect(resolveTextInput('-')).toBe(0);
10+
});
11+
12+
it('keeps ordinary file paths unchanged and reads their text', () => {
13+
const dir = mkdtempSync(join(tmpdir(), 'mmx-fs-test-'));
14+
const path = join(dir, 'input.txt');
15+
writeFileSync(path, 'hello from a file', 'utf-8');
16+
17+
try {
18+
expect(resolveTextInput(path)).toBe(path);
19+
expect(readTextFromPathOrStdin(path)).toBe('hello from a file');
20+
} finally {
21+
rmSync(dir, { recursive: true, force: true });
22+
}
23+
});
24+
});

0 commit comments

Comments
 (0)