diff --git a/js/src/bin/arrow2csv.ts b/js/src/bin/arrow2csv.ts index ef260af82bace..3014123a60b5b 100644 --- a/js/src/bin/arrow2csv.ts +++ b/js/src/bin/arrow2csv.ts @@ -44,39 +44,6 @@ type ToStringState = { maxColWidths: number[]; }; -try { - const sources = argv.help ? [] : [ - ...files.map((file) => () => fs.createReadStream(file)), - ...(process.stdin.isTTY ? [] : [() => process.stdin]) - ].filter(Boolean) as (() => NodeJS.ReadableStream)[]; - - let reader: RecordBatchReader | null; - let hasReaders = false; - - for (const source of sources) { - if (state.closed) { break; } - for await (reader of recordBatchReaders(source)) { - hasReaders = true; - const transformToString = batchesToString(state, reader.schema); - await pipeTo( - reader.pipe(transformToString), - process.stdout, { end: false } - ).catch(() => state.closed = true); // Handle EPIPE errors - } - if (state.closed) { break; } - } - - const code = hasReaders ? 0 : print_usage(); - - process.exit(+code || 0); -} catch (err: any) { - if (err) { - console.error(`${err?.stack || err}`); - } - - process.exit(process.exitCode || 1); -} - function pipeTo(source: NodeJS.ReadableStream, sink: NodeJS.WritableStream, opts?: { end: boolean }) { return new Promise((resolve, reject) => { @@ -337,3 +304,36 @@ function print_usage() { ])); return 1; } + +try { + const sources = argv.help ? [] : [ + ...files.map((file) => () => fs.createReadStream(file)), + ...(process.stdin.isTTY ? [] : [() => process.stdin]) + ].filter(Boolean) as (() => NodeJS.ReadableStream)[]; + + let reader: RecordBatchReader | null; + let hasReaders = false; + + for (const source of sources) { + if (state.closed) { break; } + for await (reader of recordBatchReaders(source)) { + hasReaders = true; + const transformToString = batchesToString(state, reader.schema); + await pipeTo( + reader.pipe(transformToString), + process.stdout, { end: false } + ).catch(() => state.closed = true); // Handle EPIPE errors + } + if (state.closed) { break; } + } + + const code = hasReaders ? 0 : print_usage(); + + process.exit(+code || 0); +} catch (err: any) { + if (err) { + console.error(`${err?.stack || err}`); + } + + process.exit(process.exitCode || 1); +}