csv-to-json-streamer 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +104 -0
  2. package/package.json +26 -0
package/index.js ADDED
@@ -0,0 +1,104 @@
1
+ #!/usr/bin/env node
2
+ import fs from "fs";
3
+ import csv from "csv-parser";
4
+ import { Transform, pipeline } from "stream";
5
+ import { Command } from "commander";
6
+
7
+ // 1. Your Custom Streams
8
+ class ProgressLogger extends Transform {
9
+ constructor(logInterval = 10000) {
10
+ super({ objectMode: true });
11
+ this.rowCount = 0;
12
+ this.logInterval = logInterval;
13
+ this.startTime = Date.now();
14
+ }
15
+
16
+ _transform(chunk, encoding, callback) {
17
+ this.rowCount++;
18
+ if (this.rowCount % this.logInterval === 0) {
19
+ const elapsed = ((Date.now() - this.startTime) / 1000).toFixed(2);
20
+ console.log(`[Progress] Processed ${this.rowCount} rows in ${elapsed}s...`);
21
+ }
22
+ this.push(chunk);
23
+ callback();
24
+ }
25
+
26
+ _flush(callback) {
27
+ const totalTime = ((Date.now() - this.startTime) / 1000).toFixed(2);
28
+ console.log(`[Summary] Finished reading ${this.rowCount} total rows in ${totalTime}s.`);
29
+ callback();
30
+ }
31
+ }
32
+
33
+ class JSONFormatter extends Transform {
34
+ constructor() {
35
+ super({ writableObjectMode: true });
36
+ this.first = true;
37
+ }
38
+
39
+ _transform(obj, encoding, callback) {
40
+ if (this.first) {
41
+ this.push("[\n");
42
+ this.first = false;
43
+ } else {
44
+ this.push(",\n");
45
+ }
46
+ this.push(JSON.stringify(obj));
47
+ callback();
48
+ }
49
+
50
+ _flush(callback) {
51
+ if (!this.first) {
52
+ this.push("\n]");
53
+ } else {
54
+ this.push("[]");
55
+ }
56
+ callback();
57
+ }
58
+ }
59
+
60
+ // 2. Set up the CLI using Commander
61
+ const program = new Command();
62
+
63
+ program
64
+ .name('csv2json')
65
+ .description('Convert large CSV files to JSON efficiently using streams.')
66
+ .version('1.0.0')
67
+ .requiredOption('-i, --input <path>', 'Path to the input CSV file')
68
+ .option('-o, --output <path>', 'Path to the output JSON file', 'output.json')
69
+ .option('-l, --log-interval <number>', 'How often to log progress', '50000')
70
+ .action((options) => {
71
+ const inputFilePath = options.input;
72
+ const outputFilePath = options.output;
73
+ const logInterval = parseInt(options.logInterval, 10);
74
+
75
+ if (!fs.existsSync(inputFilePath)) {
76
+ console.error(`[Error] Input file not found: ${inputFilePath}`);
77
+ process.exit(1);
78
+ }
79
+
80
+ console.log(`[Start] Initiating pipeline from ${inputFilePath} to ${outputFilePath}...`);
81
+
82
+ pipeline(
83
+ fs.createReadStream(inputFilePath),
84
+ csv({
85
+ mapHeaders: ({ header }) => header.trim(),
86
+ mapValues: ({ value }) => value?.trim(),
87
+ }),
88
+ new ProgressLogger(logInterval),
89
+ new JSONFormatter(),
90
+ fs.createWriteStream(outputFilePath),
91
+ (err) => {
92
+ if (err) {
93
+ console.error("[Error] Pipeline failed:", err);
94
+ process.exit(1);
95
+ } else {
96
+ console.log(`[Success] Pipeline succeeded. Data written to ${outputFilePath}.`);
97
+ process.exit(0);
98
+ }
99
+ }
100
+ );
101
+ });
102
+
103
+ // Parse the arguments triggered by the user
104
+ program.parse(process.argv);
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "csv-to-json-streamer",
3
+ "version": "1.0.0",
4
+ "description": "A memory-efficient CLI tool to stream large CSV files into JSON.",
5
+ "main": "index.js",
6
+ "type": "module",
7
+ "bin": {
8
+ "csv2json": "index.js"
9
+ },
10
+ "scripts": {
11
+ "test": "echo \"Error: no test specified\" && exit 1"
12
+ },
13
+ "keywords": [
14
+ "cli",
15
+ "csv",
16
+ "json",
17
+ "stream",
18
+ "parser"
19
+ ],
20
+ "author": "Your Name",
21
+ "license": "MIT",
22
+ "dependencies": {
23
+ "commander": "^11.1.0",
24
+ "csv-parser": "^3.0.0"
25
+ }
26
+ }