File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ import type {
1616} from '../../types/api' ;
1717import { readFileSync } from 'fs' ;
1818import { isInteractive } from '../../utils/env' ;
19+ import { readTextFromPathOrStdin } from '../../utils/fs' ;
1920import { 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' ) {
Original file line number Diff line number Diff line change 11import { 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+
39export function readTextFromPathOrStdin ( path : string ) : string {
4- return readFileSync ( path === '-' ? '/dev/stdin' : path , 'utf-8' ) ;
10+ return readFileSync ( resolveTextInput ( path ) , 'utf-8' ) ;
511}
Original file line number Diff line number Diff line change 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+ } ) ;
You can’t perform that action at this time.
0 commit comments