11// @ts -check
22import * as fs from 'fs' ;
3- import { TypeProcessor } from './processor.js' ;
3+ import os from 'os' ;
4+ import path from 'path' ;
45import { parseArgs } from 'util' ;
56import ts from 'typescript' ;
6- import path from 'path ' ;
7+ import { TypeProcessor } from './processor.js ' ;
78
89class DiagnosticEngine {
910 /**
@@ -73,7 +74,24 @@ class DiagnosticEngine {
7374}
7475
7576function printUsage ( ) {
76- console . error ( 'Usage: ts2swift <d.ts file path>... [-p <tsconfig.json path>] [--global <d.ts>]... [-o output.swift]' ) ;
77+ console . error ( `Usage: ts2swift <input> [options]
78+
79+ <input> Path to a .d.ts file, or "-" to read from stdin
80+
81+ Options:
82+ -o, --output <path> Write Swift to <path>. Use "-" for stdout (default).
83+ -p, --project <path> Path to tsconfig.json (default: tsconfig.json).
84+ --global <path> Add a .d.ts as a global declaration file (repeatable).
85+ --log-level <level> One of: verbose, info, warning, error (default: info).
86+ -h, --help Show this help.
87+
88+ Examples:
89+ ts2swift lib.d.ts
90+ ts2swift lib.d.ts -o Generated.swift
91+ ts2swift lib.d.ts -p ./tsconfig.build.json -o Sources/Bridge/API.swift
92+ cat lib.d.ts | ts2swift - -o Generated.swift
93+ ts2swift lib.d.ts --global dom.d.ts --global lib.d.ts
94+ ` ) ;
7795}
7896
7997/**
@@ -185,7 +203,18 @@ export function main(args) {
185203 process . exit ( 1 ) ;
186204 }
187205
188- const filePaths = options . positionals ;
206+ /** @type {string[] } */
207+ let filePaths = options . positionals ;
208+ /** @type {(() => void)[] } cleanup functions to run after completion */
209+ const cleanups = [ ] ;
210+
211+ if ( filePaths [ 0 ] === '-' ) {
212+ const content = fs . readFileSync ( 0 , 'utf-8' ) ;
213+ const stdinTempPath = path . join ( os . tmpdir ( ) , `ts2swift-stdin-${ process . pid } -${ Date . now ( ) } .d.ts` ) ;
214+ fs . writeFileSync ( stdinTempPath , content ) ;
215+ cleanups . push ( ( ) => fs . unlinkSync ( stdinTempPath ) ) ;
216+ filePaths = [ stdinTempPath ] ;
217+ }
189218 const logLevel = /** @type {keyof typeof DiagnosticEngine.LEVELS } */ ( ( ( ) => {
190219 const logLevel = options . values [ "log-level" ] || "info" ;
191220 if ( ! Object . keys ( DiagnosticEngine . LEVELS ) . includes ( logLevel ) ) {
@@ -210,6 +239,10 @@ export function main(args) {
210239 diagnosticEngine . print ( "error" , String ( err ) ) ;
211240 }
212241 process . exit ( 1 ) ;
242+ } finally {
243+ for ( const cleanup of cleanups ) {
244+ cleanup ( ) ;
245+ }
213246 }
214247 // Write to file or stdout
215248 if ( options . values . output && options . values . output !== "-" ) {
0 commit comments