| 446 | * ``` |
| 447 | */ |
| 448 | const $writeCsvOutputStream = (filePath: string, options: CsvInputOptionsNode) => { |
| 449 | fs.access(filePath, fs.constants.F_OK, (err) => { |
| 450 | if (err) { |
| 451 | throw new Error("ENOENT: no such file or directory"); |
| 452 | } |
| 453 | |
| 454 | let isFirstRow = true |
| 455 | const fileOutputStream = fs.createWriteStream(filePath) |
| 456 | const csvOutputStream = new stream.Writable({ objectMode: true }) |
| 457 | |
| 458 | csvOutputStream._write = (chunk: DataFrame | Series, encoding, callback) => { |
| 459 | |
| 460 | if (chunk instanceof DataFrame) { |
| 461 | |
| 462 | if (isFirstRow) { |
| 463 | isFirstRow = false |
| 464 | fileOutputStream.write($toCSV(chunk, { header: true, ...options })); |
| 465 | callback(); |
| 466 | } else { |
| 467 | fileOutputStream.write($toCSV(chunk, { header: false, ...options })); |
| 468 | callback(); |
| 469 | } |
| 470 | |
| 471 | } else if (chunk instanceof Series) { |
| 472 | |
| 473 | fileOutputStream.write($toCSV(chunk)); |
| 474 | callback(); |
| 475 | |
| 476 | } else { |
| 477 | csvOutputStream.emit("error", new Error("ValueError: Intermediate chunk must be either a Series or DataFrame")) |
| 478 | } |
| 479 | |
| 480 | } |
| 481 | |
| 482 | csvOutputStream.on("finish", () => { |
| 483 | fileOutputStream.end() |
| 484 | }) |
| 485 | |
| 486 | return csvOutputStream |
| 487 | }); |
| 488 | } |
| 489 | |
| 490 | |
| 491 | |