jtcsv 2.2.7 → 3.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 (140) hide show
  1. package/README.md +31 -1
  2. package/bin/jtcsv.js +891 -821
  3. package/bin/jtcsv.ts +2534 -0
  4. package/csv-to-json.js +168 -145
  5. package/dist/jtcsv-core.cjs.js +1407 -0
  6. package/dist/jtcsv-core.cjs.js.map +1 -0
  7. package/dist/jtcsv-core.esm.js +1379 -0
  8. package/dist/jtcsv-core.esm.js.map +1 -0
  9. package/dist/jtcsv-core.umd.js +1413 -0
  10. package/dist/jtcsv-core.umd.js.map +1 -0
  11. package/dist/jtcsv-full.cjs.js +1912 -0
  12. package/dist/jtcsv-full.cjs.js.map +1 -0
  13. package/dist/jtcsv-full.esm.js +1880 -0
  14. package/dist/jtcsv-full.esm.js.map +1 -0
  15. package/dist/jtcsv-full.umd.js +1918 -0
  16. package/dist/jtcsv-full.umd.js.map +1 -0
  17. package/dist/jtcsv-workers.esm.js +759 -0
  18. package/dist/jtcsv-workers.esm.js.map +1 -0
  19. package/dist/jtcsv-workers.umd.js +773 -0
  20. package/dist/jtcsv-workers.umd.js.map +1 -0
  21. package/dist/jtcsv.cjs.js +61 -19
  22. package/dist/jtcsv.cjs.js.map +1 -1
  23. package/dist/jtcsv.esm.js +61 -19
  24. package/dist/jtcsv.esm.js.map +1 -1
  25. package/dist/jtcsv.umd.js +61 -19
  26. package/dist/jtcsv.umd.js.map +1 -1
  27. package/errors.js +188 -2
  28. package/examples/advanced/conditional-transformations.js +446 -0
  29. package/examples/advanced/conditional-transformations.ts +446 -0
  30. package/examples/advanced/csv-parser.worker.js +89 -0
  31. package/examples/advanced/csv-parser.worker.ts +89 -0
  32. package/examples/advanced/nested-objects-example.js +306 -0
  33. package/examples/advanced/nested-objects-example.ts +306 -0
  34. package/examples/advanced/performance-optimization.js +504 -0
  35. package/examples/advanced/performance-optimization.ts +504 -0
  36. package/examples/advanced/run-demo-server.js +116 -0
  37. package/examples/advanced/run-demo-server.ts +116 -0
  38. package/examples/advanced/web-worker-usage.html +874 -0
  39. package/examples/async-multithreaded-example.ts +335 -0
  40. package/examples/cli-advanced-usage.md +288 -0
  41. package/examples/cli-batch-processing.ts +38 -0
  42. package/examples/cli-tool.js +0 -3
  43. package/examples/cli-tool.ts +183 -0
  44. package/examples/error-handling.js +21 -7
  45. package/examples/error-handling.ts +356 -0
  46. package/examples/express-api.js +0 -3
  47. package/examples/express-api.ts +164 -0
  48. package/examples/large-dataset-example.js +0 -3
  49. package/examples/large-dataset-example.ts +204 -0
  50. package/examples/ndjson-processing.js +1 -1
  51. package/examples/ndjson-processing.ts +456 -0
  52. package/examples/plugin-excel-exporter.js +3 -4
  53. package/examples/plugin-excel-exporter.ts +406 -0
  54. package/examples/react-integration.tsx +637 -0
  55. package/examples/schema-validation.ts +640 -0
  56. package/examples/simple-usage.js +254 -254
  57. package/examples/simple-usage.ts +194 -0
  58. package/examples/streaming-example.js +4 -5
  59. package/examples/streaming-example.ts +419 -0
  60. package/examples/web-workers-advanced.ts +28 -0
  61. package/index.d.ts +1 -3
  62. package/index.js +15 -1
  63. package/json-save.js +9 -3
  64. package/json-to-csv.js +168 -21
  65. package/package.json +69 -10
  66. package/plugins/express-middleware/README.md +21 -2
  67. package/plugins/express-middleware/example.js +3 -4
  68. package/plugins/express-middleware/example.ts +135 -0
  69. package/plugins/express-middleware/index.d.ts +1 -1
  70. package/plugins/express-middleware/index.js +270 -118
  71. package/plugins/express-middleware/index.ts +557 -0
  72. package/plugins/fastify-plugin/index.js +2 -4
  73. package/plugins/fastify-plugin/index.ts +443 -0
  74. package/plugins/hono/index.ts +226 -0
  75. package/plugins/nestjs/index.ts +201 -0
  76. package/plugins/nextjs-api/examples/ConverterComponent.tsx +386 -0
  77. package/plugins/nextjs-api/examples/api-convert.js +0 -2
  78. package/plugins/nextjs-api/examples/api-convert.ts +67 -0
  79. package/plugins/nextjs-api/index.tsx +339 -0
  80. package/plugins/nextjs-api/route.js +2 -3
  81. package/plugins/nextjs-api/route.ts +370 -0
  82. package/plugins/nuxt/index.ts +94 -0
  83. package/plugins/nuxt/runtime/composables/useJtcsv.ts +100 -0
  84. package/plugins/nuxt/runtime/plugin.ts +71 -0
  85. package/plugins/remix/index.js +1 -1
  86. package/plugins/remix/index.ts +260 -0
  87. package/plugins/sveltekit/index.js +1 -1
  88. package/plugins/sveltekit/index.ts +301 -0
  89. package/plugins/trpc/index.ts +267 -0
  90. package/src/browser/browser-functions.ts +402 -0
  91. package/src/browser/core.js +92 -0
  92. package/src/browser/core.ts +152 -0
  93. package/src/browser/csv-to-json-browser.d.ts +3 -0
  94. package/src/browser/csv-to-json-browser.js +36 -14
  95. package/src/browser/csv-to-json-browser.ts +264 -0
  96. package/src/browser/errors-browser.ts +303 -0
  97. package/src/browser/extensions/plugins.js +92 -0
  98. package/src/browser/extensions/plugins.ts +93 -0
  99. package/src/browser/extensions/workers.js +39 -0
  100. package/src/browser/extensions/workers.ts +39 -0
  101. package/src/browser/globals.d.ts +5 -0
  102. package/src/browser/index.ts +192 -0
  103. package/src/browser/json-to-csv-browser.d.ts +3 -0
  104. package/src/browser/json-to-csv-browser.js +13 -3
  105. package/src/browser/json-to-csv-browser.ts +262 -0
  106. package/src/browser/streams.js +12 -2
  107. package/src/browser/streams.ts +336 -0
  108. package/src/browser/workers/csv-parser.worker.ts +377 -0
  109. package/src/browser/workers/worker-pool.ts +548 -0
  110. package/src/core/delimiter-cache.js +22 -8
  111. package/src/core/delimiter-cache.ts +310 -0
  112. package/src/core/node-optimizations.ts +449 -0
  113. package/src/core/plugin-system.js +29 -11
  114. package/src/core/plugin-system.ts +400 -0
  115. package/src/core/transform-hooks.ts +558 -0
  116. package/src/engines/fast-path-engine-new.ts +347 -0
  117. package/src/engines/fast-path-engine.ts +854 -0
  118. package/src/errors.ts +72 -0
  119. package/src/formats/ndjson-parser.ts +469 -0
  120. package/src/formats/tsv-parser.ts +334 -0
  121. package/src/index-with-plugins.js +16 -9
  122. package/src/index-with-plugins.ts +395 -0
  123. package/src/types/index.ts +255 -0
  124. package/src/utils/bom-utils.js +259 -0
  125. package/src/utils/bom-utils.ts +373 -0
  126. package/src/utils/encoding-support.js +124 -0
  127. package/src/utils/encoding-support.ts +155 -0
  128. package/src/utils/schema-validator.js +19 -19
  129. package/src/utils/schema-validator.ts +819 -0
  130. package/src/utils/transform-loader.js +1 -1
  131. package/src/utils/transform-loader.ts +389 -0
  132. package/src/utils/zod-adapter.js +170 -0
  133. package/src/utils/zod-adapter.ts +280 -0
  134. package/src/web-server/index.js +10 -10
  135. package/src/web-server/index.ts +683 -0
  136. package/src/workers/csv-multithreaded.ts +310 -0
  137. package/src/workers/csv-parser.worker.ts +227 -0
  138. package/src/workers/worker-pool.ts +409 -0
  139. package/stream-csv-to-json.js +26 -8
  140. package/stream-json-to-csv.js +1 -0
package/bin/jtcsv.ts ADDED
@@ -0,0 +1,2534 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * jtcsv CLI - Complete Command Line Interface
5
+ *
6
+ * Full-featured command-line interface for JSON↔CSV conversion
7
+ * with streaming, batch processing, and all security features.
8
+ */
9
+
10
+ import * as fs from 'fs';
11
+ import * as path from 'path';
12
+ import * as readline from 'readline';
13
+ const { pipeline } = require('stream/promises');
14
+ import * as jtcsv from '../index.js';
15
+ const transformLoader = require('../src/utils/transform-loader');
16
+ const schemaValidator = require('../src/utils/schema-validator');
17
+
18
+ const VERSION = require('../package.json').version;
19
+
20
+ type ConversionResult = {
21
+ records?: number;
22
+ rows?: number;
23
+ bytes?: number;
24
+ time?: number;
25
+ };
26
+
27
+ type BatchFileResult = ConversionResult & {
28
+ file: string;
29
+ success: boolean;
30
+ error?: string;
31
+ };
32
+
33
+ type BatchSummary = {
34
+ totalFiles: number;
35
+ successful: number;
36
+ totalRecords?: number;
37
+ totalRows?: number;
38
+ jsonFiles?: number;
39
+ csvFiles?: number;
40
+ otherFiles?: number;
41
+ time: number;
42
+ results: BatchFileResult[];
43
+ };
44
+
45
+ // Function to apply transform from JavaScript file
46
+ async function applyTransform(data, transformFile: any): Promise<any> {
47
+ try {
48
+ const transformPath = path.resolve(process.cwd(), transformFile);
49
+ const transformModule = require(transformPath);
50
+
51
+ // Check if module exports a function
52
+ if (typeof transformModule === 'function') {
53
+ return transformModule(data);
54
+ } else if (typeof transformModule.default === 'function') {
55
+ return transformModule.default(data);
56
+ } else if (typeof transformModule.transform === 'function') {
57
+ return transformModule.transform(data);
58
+ } else {
59
+ throw new Error(
60
+ `Transform file must export a function. Found: ${typeof transformModule}`
61
+ );
62
+ }
63
+ } catch (error: any) {
64
+ throw new Error(
65
+ `Failed to apply transform from ${transformFile}: ${error.message}`
66
+ );
67
+ }
68
+ }
69
+
70
+ // ANSI colors for terminal output
71
+ const colors = {
72
+ reset: '\x1b[0m',
73
+ bright: '\x1b[1m',
74
+ dim: '\x1b[2m',
75
+ red: '\x1b[31m',
76
+ green: '\x1b[32m',
77
+ yellow: '\x1b[33m',
78
+ blue: '\x1b[34m',
79
+ magenta: '\x1b[35m',
80
+ cyan: '\x1b[36m',
81
+ white: '\x1b[37m'
82
+ };
83
+
84
+ function color(text, colorName: any): any {
85
+ return (colors as any)[colorName] + text + colors.reset;
86
+ }
87
+
88
+ function showHelp(): void {
89
+ console.log(`
90
+ ${color('jtcsv CLI v' + VERSION, 'cyan')}
91
+ ${color('The Complete JSON↔CSV Converter for Node.js', 'dim')}
92
+
93
+ ${color('USAGE:', 'bright')}
94
+ jtcsv [command] [options] [file...]
95
+
96
+ ${color('MAIN COMMANDS:', 'bright')}
97
+ ${color('json-to-csv', 'green')} Convert JSON to CSV (alias: json2csv)
98
+ ${color('csv-to-json', 'green')} Convert CSV to JSON (alias: csv2json)
99
+ ${color('ndjson-to-csv', 'green')} Convert NDJSON to CSV
100
+ ${color('csv-to-ndjson', 'green')} Convert CSV to NDJSON
101
+ ${color('ndjson-to-json', 'green')} Convert NDJSON to JSON array
102
+ ${color('json-to-ndjson', 'green')} Convert JSON array to NDJSON
103
+ ${color('save-json', 'yellow')} Save data as JSON file
104
+ ${color('save-csv', 'yellow')} Save data as CSV file
105
+ ${color('stream', 'yellow')} Streaming conversion for large files
106
+ ${color('batch', 'yellow')} Batch process multiple files
107
+ ${color('preprocess', 'magenta')} Preprocess JSON with deep unwrapping
108
+ ${color('unwrap', 'magenta')} Flatten nested JSON structures (alias: flatten)
109
+ ${color('tui', 'magenta')} Launch Terminal User Interface (@jtcsv/tui)
110
+ ${color('web', 'magenta')} Launch Web Interface (http://localhost:3000)
111
+ ${color('help', 'blue')} Show this help message
112
+ ${color('version', 'blue')} Show version information
113
+
114
+ ${color('STREAMING SUBCOMMANDS:', 'bright')}
115
+ ${color('stream json-to-csv', 'dim')} Stream JSON to CSV
116
+ ${color('stream csv-to-json', 'dim')} Stream CSV to JSON
117
+ ${color('stream file-to-csv', 'dim')} Stream file to CSV
118
+ ${color('stream file-to-json', 'dim')} Stream file to JSON
119
+
120
+ ${color('BATCH SUBCOMMANDS:', 'bright')}
121
+ ${color('batch json-to-csv', 'dim')} Batch convert JSON files to CSV
122
+ ${color('batch csv-to-json', 'dim')} Batch convert CSV files to JSON
123
+ ${color('batch process', 'dim')} Process mixed file types
124
+
125
+ ${color('EXAMPLES:', 'bright')}
126
+ ${color('Convert JSON file to CSV:', 'dim')}
127
+ jtcsv json-to-csv input.json output.csv --delimiter=,
128
+
129
+ ${color('Convert CSV file to JSON:', 'dim')}
130
+ jtcsv csv-to-json input.csv output.json --parse-numbers --auto-detect
131
+
132
+ ${color('Save data as JSON file:', 'dim')}
133
+ jtcsv save-json data.json output.json --pretty
134
+
135
+ ${color('Save data as CSV file:', 'dim')}
136
+ jtcsv save-csv data.csv output.csv --delimiter=, --transform=transform.js
137
+
138
+ ${color('Stream large JSON file to CSV:', 'dim')}
139
+ jtcsv stream json-to-csv large.json output.csv --max-records=1000000
140
+
141
+ ${color('Stream CSV file to JSON:', 'dim')}
142
+ jtcsv stream csv-to-json large.csv output.json --max-rows=500000
143
+
144
+ ${color('Preprocess complex JSON:', 'dim')}
145
+ jtcsv preprocess complex.json simplified.json --max-depth=3
146
+
147
+ ${color('Batch convert JSON files:', 'dim')}
148
+ jtcsv batch json-to-csv "data/*.json" "output/" --delimiter=;
149
+
150
+ ${color('Launch TUI interface:', 'dim')}
151
+ jtcsv tui
152
+
153
+ ${color('Launch Web interface:', 'dim')}
154
+ jtcsv web --port=3000
155
+
156
+ ${color('CONVERSION OPTIONS:', 'bright')}
157
+ ${color('--delimiter=', 'cyan')}CHAR CSV delimiter (default: ;)
158
+ ${color('--auto-detect', 'cyan')} Auto-detect delimiter (default: true)
159
+ ${color('--candidates=', 'cyan')}LIST Delimiter candidates (default: ;,\t|)
160
+ ${color('--no-headers', 'cyan')} Exclude headers from CSV output
161
+ ${color('--parse-numbers', 'cyan')} Parse numeric values in CSV
162
+ ${color('--parse-booleans', 'cyan')} Parse boolean values in CSV
163
+ ${color('--no-trim', 'cyan')} Don't trim whitespace from CSV values
164
+ ${color('--no-fast-path', 'cyan')} Disable fast-path parser (force quote-aware)
165
+ ${color('--fast-path-mode=', 'cyan')}MODE Fast path output mode (objects|compact)
166
+ ${color('--rename=', 'cyan')}JSON Rename columns (JSON map)
167
+ ${color('--template=', 'cyan')}JSON Column order template (JSON object)
168
+ ${color('--no-injection-protection', 'cyan')} Disable CSV injection protection
169
+ ${color('--no-rfc4180', 'cyan')} Disable RFC 4180 compliance
170
+ ${color('--max-records=', 'cyan')}N Maximum records to process
171
+ ${color('--max-rows=', 'cyan')}N Maximum rows to process
172
+ ${color('--pretty', 'cyan')} Pretty print JSON output
173
+ ${color('--schema=', 'cyan')}JSON JSON schema for validation and formatting
174
+ ${color('--transform=', 'cyan')}JS Custom transform function (JavaScript file)
175
+ ${color('PREPROCESS OPTIONS:', 'bright')}
176
+ ${color('--max-depth=', 'cyan')}N Maximum recursion depth (default: 5)
177
+ ${color('--flatten', 'cyan')} Flatten nested objects into dot notation
178
+ ${color('--flatten-separator=', 'cyan')}CHAR Separator for flattened keys (default: .)
179
+ ${color('--flatten-max-depth=', 'cyan')}N Maximum flattening depth (default: 3)
180
+ ${color('--array-handling=', 'cyan')}MODE Array handling: stringify|join|expand (default: stringify)
181
+ ${color('--unwrap-arrays', 'cyan')} Unwrap arrays to strings
182
+ ${color('--stringify-objects', 'cyan')} Stringify complex objects
183
+ ${color('STREAMING OPTIONS:', 'bright')}
184
+ ${color('--chunk-size=', 'cyan')}N Chunk size in bytes (default: 65536)
185
+ ${color('--buffer-size=', 'cyan')}N Buffer size in records (default: 1000)
186
+ ${color('--add-bom', 'cyan')} Add UTF-8 BOM for Excel compatibility
187
+ ${color('BATCH OPTIONS:', 'bright')}
188
+ ${color('--recursive', 'cyan')} Process directories recursively
189
+ ${color('--pattern=', 'cyan')}GLOB File pattern to match
190
+ ${color('--output-dir=', 'cyan')}DIR Output directory for batch processing
191
+ ${color('--overwrite', 'cyan')} Overwrite existing files
192
+ ${color('--parallel=', 'cyan')}N Parallel processing limit (default: 4)
193
+ ${color('GENERAL OPTIONS:', 'bright')}
194
+ ${color('--silent', 'cyan')} Suppress all output except errors
195
+ ${color('--verbose', 'cyan')} Show detailed progress information
196
+ ${color('--debug', 'cyan')} Show debug information
197
+ ${color('--dry-run', 'cyan')} Show what would be done without actually doing it
198
+ ${color('SECURITY FEATURES:', 'bright')}
199
+ • CSV injection protection (enabled by default)
200
+ • Path traversal protection
201
+ • Input validation and sanitization
202
+ • Size limits to prevent DoS attacks
203
+ • Schema validation support
204
+
205
+ ${color('PERFORMANCE FEATURES:', 'bright')}
206
+ • Streaming for files >100MB
207
+ • Batch processing with parallel execution
208
+ • Memory-efficient preprocessing
209
+ • Configurable buffer sizes
210
+
211
+ ${color('LEARN MORE:', 'dim')}
212
+ GitHub: https://github.com/Linol-Hamelton/jtcsv
213
+ Issues: https://github.com/Linol-Hamelton/jtcsv/issues
214
+ Documentation: https://github.com/Linol-Hamelton/jtcsv#readme
215
+ `);
216
+ }
217
+
218
+ function showVersion(): void {
219
+ console.log(`jtcsv v${VERSION}`);
220
+ console.log(`Node.js ${process.version}`);
221
+ console.log(`Platform: ${process.platform} ${process.arch}`);
222
+ }
223
+
224
+ // ============================================================================
225
+ // CONVERSION FUNCTIONS
226
+ // ============================================================================
227
+
228
+ async function convertJsonToCsv(inputFile, outputFile, options: any): Promise<ConversionResult> {
229
+ const startTime = Date.now();
230
+
231
+ try {
232
+ // Read input file
233
+ const inputData = await fs.promises.readFile(inputFile, 'utf8');
234
+ const jsonData = JSON.parse(inputData);
235
+
236
+ if (!Array.isArray(jsonData)) {
237
+ throw new Error('JSON data must be an array of objects');
238
+ }
239
+
240
+ if (!options.silent) {
241
+ console.log(
242
+ color(
243
+ `Converting ${jsonData.length.toLocaleString()} records...`,
244
+ 'dim'
245
+ )
246
+ );
247
+ }
248
+
249
+ if (options.transform) {
250
+ if (!options.silent) {
251
+ console.log(
252
+ color(`Applying transform from: ${options.transform}`, 'dim')
253
+ );
254
+ }
255
+ let transformedData;
256
+ try {
257
+ transformedData = transformLoader.applyTransform(
258
+ jsonData,
259
+ options.transform
260
+ );
261
+ if (!options.silent) {
262
+ console.log(
263
+ color(
264
+ `✓ Transform applied to ${transformedData.length} records`,
265
+ 'green'
266
+ )
267
+ );
268
+ }
269
+ } catch (transformError: any) {
270
+ console.error(
271
+ color(`✗ Transform error: ${transformError.message}`, 'red')
272
+ );
273
+ if (options.debug) {
274
+ console.error(transformError.stack);
275
+ }
276
+ process.exit(1);
277
+ }
278
+ }
279
+
280
+ // Prepare options for jtcsv
281
+ const jtcsvOptions = {
282
+ delimiter: options.delimiter,
283
+ includeHeaders: options.includeHeaders,
284
+ renameMap: options.renameMap,
285
+ template: options.template,
286
+ maxRecords: options.maxRecords,
287
+ preventCsvInjection: options.preventCsvInjection,
288
+ rfc4180Compliant: options.rfc4180Compliant,
289
+ schema: options.schema, // Add schema option
290
+ flatten: options.flatten,
291
+ flattenSeparator: options.flattenSeparator,
292
+ flattenMaxDepth: options.flattenMaxDepth,
293
+ arrayHandling: options.arrayHandling
294
+ };
295
+
296
+ // Apply transform function if provided
297
+ let transformedData = jsonData;
298
+ if (options.transform) {
299
+ transformedData = await applyTransform(jsonData, options.transform);
300
+ }
301
+
302
+ // Convert to CSV
303
+ const csvData = jtcsv.jsonToCsv(transformedData, jtcsvOptions);
304
+
305
+ // Write output file
306
+ await fs.promises.writeFile(outputFile, csvData, 'utf8');
307
+
308
+ const elapsed = Date.now() - startTime;
309
+ if (!options.silent) {
310
+ console.log(
311
+ color(
312
+ `✓ Converted ${transformedData.length.toLocaleString()} records in ${elapsed}ms`,
313
+ 'green'
314
+ )
315
+ );
316
+ console.log(
317
+ color(
318
+ ` Output: ${outputFile} (${csvData.length.toLocaleString()} bytes)`,
319
+ 'dim'
320
+ )
321
+ );
322
+ }
323
+
324
+ return {
325
+ records: transformedData.length,
326
+ bytes: csvData.length,
327
+ time: elapsed
328
+ };
329
+ } catch (error: any) {
330
+ console.error(color(`✗ Error: ${error.message}`, 'red'));
331
+ if (options.debug) {
332
+ console.error(error.stack);
333
+ }
334
+ process.exit(1);
335
+ }
336
+ }
337
+
338
+ async function convertCsvToJson(inputFile, outputFile, options: any): Promise<ConversionResult> {
339
+ const startTime = Date.now();
340
+
341
+ try {
342
+ if (!options.silent) {
343
+ console.log(color('Reading CSV file...', 'dim'));
344
+ }
345
+
346
+ // Prepare options for jtcsv
347
+ const jtcsvOptions = {
348
+ delimiter: options.delimiter,
349
+ autoDetect: options.autoDetect,
350
+ candidates: options.candidates,
351
+ hasHeaders: options.hasHeaders,
352
+ renameMap: options.renameMap,
353
+ trim: options.trim,
354
+ parseNumbers: options.parseNumbers,
355
+ parseBooleans: options.parseBooleans,
356
+ maxRows: options.maxRows,
357
+ useFastPath: options.useFastPath,
358
+ fastPathMode: options.fastPathMode,
359
+ schema: options.schema // Add schema option if supported
360
+ };
361
+
362
+ // Read and convert CSV
363
+ const jsonData = await jtcsv.readCsvAsJson(inputFile, jtcsvOptions);
364
+
365
+ // Apply transform if specified
366
+ let transformedData = jsonData;
367
+ if (options.transform) {
368
+ if (!options.silent) {
369
+ console.log(
370
+ color(`Applying transform from: ${options.transform}`, 'dim')
371
+ );
372
+ }
373
+ try {
374
+ transformedData = transformLoader.applyTransform(
375
+ jsonData,
376
+ options.transform
377
+ );
378
+ if (!options.silent) {
379
+ console.log(
380
+ color(
381
+ `✓ Transform applied to ${transformedData.length} rows`,
382
+ 'green'
383
+ )
384
+ );
385
+ }
386
+ } catch (transformError: any) {
387
+ console.error(
388
+ color(`✗ Transform error: ${transformError.message}`, 'red')
389
+ );
390
+ if (options.debug) {
391
+ console.error(transformError.stack);
392
+ }
393
+ process.exit(1);
394
+ }
395
+ }
396
+
397
+ // Format JSON
398
+ const jsonOutput = options.pretty
399
+ ? JSON.stringify(transformedData, null, 2)
400
+ : JSON.stringify(transformedData);
401
+
402
+ // Write output file
403
+ await fs.promises.writeFile(outputFile, jsonOutput, 'utf8');
404
+
405
+ const elapsed = Date.now() - startTime;
406
+ if (!options.silent) {
407
+ console.log(
408
+ color(
409
+ `✓ Converted ${transformedData.length.toLocaleString()} rows in ${elapsed}ms`,
410
+ 'green'
411
+ )
412
+ );
413
+ console.log(
414
+ color(
415
+ ` Output: ${outputFile} (${jsonOutput.length.toLocaleString()} bytes)`,
416
+ 'dim'
417
+ )
418
+ );
419
+ }
420
+
421
+ return {
422
+ rows: transformedData.length,
423
+ bytes: jsonOutput.length,
424
+ time: elapsed
425
+ };
426
+ } catch (error: any) {
427
+ console.error(color(`✗ Error: ${error.message}`, 'red'));
428
+ if (options.debug) {
429
+ console.error(error.stack);
430
+ }
431
+ process.exit(1);
432
+ }
433
+ }
434
+
435
+ async function saveAsCsv(inputFile, outputFile, options: any): Promise<ConversionResult> {
436
+ const startTime = Date.now();
437
+
438
+ try {
439
+ // Read input file
440
+ const inputData = await fs.promises.readFile(inputFile, 'utf8');
441
+
442
+ if (!options.silent) {
443
+ console.log(color('Saving CSV file...', 'dim'));
444
+ }
445
+
446
+ // Apply transform if specified
447
+ let transformedData = inputData;
448
+ if (options.transform) {
449
+ if (!options.silent) {
450
+ console.log(
451
+ color(`Applying transform from: ${options.transform}`, 'dim')
452
+ );
453
+ }
454
+ try {
455
+ // Для CSV нужно сначала распарсить, применить трансформацию, затем снова сериализовать
456
+ const parsedData = jtcsv.csvToJson(inputData, {
457
+ delimiter: options.delimiter,
458
+ autoDetect: options.autoDetect,
459
+ hasHeaders: options.hasHeaders,
460
+ trim: options.trim,
461
+ parseNumbers: options.parseNumbers,
462
+ parseBooleans: options.parseBooleans
463
+ });
464
+
465
+ const transformedJson = transformLoader.applyTransform(
466
+ parsedData,
467
+ options.transform
468
+ );
469
+
470
+ // Конвертировать обратно в CSV
471
+ transformedData = jtcsv.jsonToCsv(transformedJson, {
472
+ delimiter: options.delimiter,
473
+ includeHeaders: options.includeHeaders
474
+ });
475
+
476
+ if (!options.silent) {
477
+ console.log(color('✓ Transform applied', 'green'));
478
+ }
479
+ } catch (transformError: any) {
480
+ console.error(
481
+ color(`✗ Transform error: ${transformError.message}`, 'red')
482
+ );
483
+ if (options.debug) {
484
+ console.error(transformError.stack);
485
+ }
486
+ process.exit(1);
487
+ }
488
+ }
489
+
490
+ // Write output file
491
+ await fs.promises.writeFile(outputFile, transformedData, 'utf8');
492
+
493
+ const elapsed = Date.now() - startTime;
494
+ if (!options.silent) {
495
+ console.log(color(`✓ Saved CSV in ${elapsed}ms`, 'green'));
496
+ console.log(color(` Output: ${outputFile}`, 'dim'));
497
+ }
498
+
499
+ return { time: elapsed };
500
+ } catch (error: any) {
501
+ console.error(color(`✗ Error: ${error.message}`, 'red'));
502
+ if (options.debug) {
503
+ console.error(error.stack);
504
+ }
505
+ process.exit(1);
506
+ }
507
+ }
508
+
509
+ async function saveAsJson(inputFile, outputFile, options: any): Promise<ConversionResult> {
510
+ const startTime = Date.now();
511
+
512
+ try {
513
+ // Read input file
514
+ const inputData = await fs.promises.readFile(inputFile, 'utf8');
515
+ const jsonData = JSON.parse(inputData);
516
+
517
+ if (!options.silent) {
518
+ console.log(
519
+ color(
520
+ `Saving ${Array.isArray(jsonData) ? jsonData.length.toLocaleString() + ' records' : 'object'}...`,
521
+ 'dim'
522
+ )
523
+ );
524
+ }
525
+
526
+ // Apply transform if specified
527
+ let transformedData = jsonData;
528
+ if (options.transform) {
529
+ if (!options.silent) {
530
+ console.log(
531
+ color(`Applying transform from: ${options.transform}`, 'dim')
532
+ );
533
+ }
534
+ try {
535
+ transformedData = transformLoader.applyTransform(
536
+ jsonData,
537
+ options.transform
538
+ );
539
+ if (!options.silent) {
540
+ console.log(color('✓ Transform applied', 'green'));
541
+ }
542
+ } catch (transformError: any) {
543
+ console.error(
544
+ color(`✗ Transform error: ${transformError.message}`, 'red')
545
+ );
546
+ if (options.debug) {
547
+ console.error(transformError.stack);
548
+ }
549
+ process.exit(1);
550
+ }
551
+ }
552
+
553
+ // Prepare options for jtcsv
554
+ const jtcsvOptions = {
555
+ prettyPrint: options.pretty
556
+ };
557
+
558
+ // Save as JSON
559
+ await jtcsv.saveAsJson(transformedData, outputFile, jtcsvOptions);
560
+
561
+ const elapsed = Date.now() - startTime;
562
+ if (!options.silent) {
563
+ console.log(color(`✓ Saved JSON in ${elapsed}ms`, 'green'));
564
+ console.log(color(` Output: ${outputFile}`, 'dim'));
565
+ }
566
+
567
+ return { time: elapsed };
568
+ } catch (error: any) {
569
+ console.error(color(`✗ Error: ${error.message}`, 'red'));
570
+ if (options.debug) {
571
+ console.error(error.stack);
572
+ }
573
+ process.exit(1);
574
+ }
575
+ }
576
+
577
+ // ============================================================================
578
+ // NDJSON CONVERSION FUNCTIONS
579
+ // ============================================================================
580
+
581
+ async function convertNdjsonToCsv(inputFile, outputFile, options: any): Promise<ConversionResult> {
582
+ const startTime = Date.now();
583
+
584
+ try {
585
+ if (!options.silent) {
586
+ console.log(color('Converting NDJSON to CSV...', 'dim'));
587
+ }
588
+
589
+ // Read NDJSON file
590
+ const inputData = await fs.promises.readFile(inputFile, 'utf8');
591
+ const jsonData = jtcsv.ndjsonToJson(inputData);
592
+
593
+ if (!options.silent) {
594
+ console.log(
595
+ color(`Parsed ${jsonData.length.toLocaleString()} records from NDJSON`, 'dim')
596
+ );
597
+ }
598
+
599
+ // Prepare options for jtcsv
600
+ const jtcsvOptions = {
601
+ delimiter: options.delimiter,
602
+ includeHeaders: options.includeHeaders,
603
+ renameMap: options.renameMap,
604
+ template: options.template,
605
+ preventCsvInjection: options.preventCsvInjection,
606
+ rfc4180Compliant: options.rfc4180Compliant
607
+ };
608
+
609
+ // Convert to CSV
610
+ const csvData = jtcsv.jsonToCsv(jsonData, jtcsvOptions);
611
+
612
+ // Write output file
613
+ await fs.promises.writeFile(outputFile, csvData, 'utf8');
614
+
615
+ const elapsed = Date.now() - startTime;
616
+ if (!options.silent) {
617
+ console.log(
618
+ color(
619
+ `✓ Converted ${jsonData.length.toLocaleString()} records in ${elapsed}ms`,
620
+ 'green'
621
+ )
622
+ );
623
+ console.log(
624
+ color(
625
+ ` Output: ${outputFile} (${csvData.length.toLocaleString()} bytes)`,
626
+ 'dim'
627
+ )
628
+ );
629
+ }
630
+
631
+ return {
632
+ records: jsonData.length,
633
+ bytes: csvData.length,
634
+ time: elapsed
635
+ };
636
+ } catch (error: any) {
637
+ console.error(color(`✗ Error: ${error.message}`, 'red'));
638
+ if (options.debug) {
639
+ console.error(error.stack);
640
+ }
641
+ process.exit(1);
642
+ }
643
+ }
644
+
645
+ async function convertCsvToNdjson(inputFile, outputFile, options: any): Promise<ConversionResult> {
646
+ const startTime = Date.now();
647
+
648
+ try {
649
+ if (!options.silent) {
650
+ console.log(color('Converting CSV to NDJSON...', 'dim'));
651
+ }
652
+
653
+ // Prepare options for jtcsv
654
+ const jtcsvOptions = {
655
+ delimiter: options.delimiter,
656
+ autoDetect: options.autoDetect,
657
+ candidates: options.candidates,
658
+ hasHeaders: options.hasHeaders,
659
+ renameMap: options.renameMap,
660
+ trim: options.trim,
661
+ parseNumbers: options.parseNumbers,
662
+ parseBooleans: options.parseBooleans
663
+ };
664
+
665
+ // Read and convert CSV
666
+ const jsonData = await jtcsv.readCsvAsJson(inputFile, jtcsvOptions);
667
+
668
+ // Convert to NDJSON
669
+ const ndjsonData = jtcsv.jsonToNdjson(jsonData);
670
+
671
+ // Write output file
672
+ await fs.promises.writeFile(outputFile, ndjsonData, 'utf8');
673
+
674
+ const elapsed = Date.now() - startTime;
675
+ if (!options.silent) {
676
+ console.log(
677
+ color(
678
+ `✓ Converted ${jsonData.length.toLocaleString()} rows in ${elapsed}ms`,
679
+ 'green'
680
+ )
681
+ );
682
+ console.log(
683
+ color(
684
+ ` Output: ${outputFile} (${ndjsonData.length.toLocaleString()} bytes)`,
685
+ 'dim'
686
+ )
687
+ );
688
+ }
689
+
690
+ return {
691
+ rows: jsonData.length,
692
+ bytes: ndjsonData.length,
693
+ time: elapsed
694
+ };
695
+ } catch (error: any) {
696
+ console.error(color(`✗ Error: ${error.message}`, 'red'));
697
+ if (options.debug) {
698
+ console.error(error.stack);
699
+ }
700
+ process.exit(1);
701
+ }
702
+ }
703
+
704
+ async function convertNdjsonToJson(inputFile, outputFile, options: any): Promise<ConversionResult> {
705
+ const startTime = Date.now();
706
+
707
+ try {
708
+ if (!options.silent) {
709
+ console.log(color('Converting NDJSON to JSON array...', 'dim'));
710
+ }
711
+
712
+ // Read NDJSON file
713
+ const inputData = await fs.promises.readFile(inputFile, 'utf8');
714
+ const jsonData = jtcsv.ndjsonToJson(inputData);
715
+
716
+ // Format JSON
717
+ const jsonOutput = options.pretty
718
+ ? JSON.stringify(jsonData, null, 2)
719
+ : JSON.stringify(jsonData);
720
+
721
+ // Write output file
722
+ await fs.promises.writeFile(outputFile, jsonOutput, 'utf8');
723
+
724
+ const elapsed = Date.now() - startTime;
725
+ if (!options.silent) {
726
+ console.log(
727
+ color(
728
+ `✓ Converted ${jsonData.length.toLocaleString()} records in ${elapsed}ms`,
729
+ 'green'
730
+ )
731
+ );
732
+ console.log(
733
+ color(
734
+ ` Output: ${outputFile} (${jsonOutput.length.toLocaleString()} bytes)`,
735
+ 'dim'
736
+ )
737
+ );
738
+ }
739
+
740
+ return {
741
+ records: jsonData.length,
742
+ bytes: jsonOutput.length,
743
+ time: elapsed
744
+ };
745
+ } catch (error: any) {
746
+ console.error(color(`✗ Error: ${error.message}`, 'red'));
747
+ if (options.debug) {
748
+ console.error(error.stack);
749
+ }
750
+ process.exit(1);
751
+ }
752
+ }
753
+
754
+ async function convertJsonToNdjson(inputFile, outputFile, options: any): Promise<ConversionResult> {
755
+ const startTime = Date.now();
756
+
757
+ try {
758
+ if (!options.silent) {
759
+ console.log(color('Converting JSON array to NDJSON...', 'dim'));
760
+ }
761
+
762
+ // Read JSON file
763
+ const inputData = await fs.promises.readFile(inputFile, 'utf8');
764
+ const jsonData = JSON.parse(inputData);
765
+
766
+ if (!Array.isArray(jsonData)) {
767
+ throw new Error('JSON data must be an array of objects');
768
+ }
769
+
770
+ // Convert to NDJSON
771
+ const ndjsonData = jtcsv.jsonToNdjson(jsonData);
772
+
773
+ // Write output file
774
+ await fs.promises.writeFile(outputFile, ndjsonData, 'utf8');
775
+
776
+ const elapsed = Date.now() - startTime;
777
+ if (!options.silent) {
778
+ console.log(
779
+ color(
780
+ `✓ Converted ${jsonData.length.toLocaleString()} records in ${elapsed}ms`,
781
+ 'green'
782
+ )
783
+ );
784
+ console.log(
785
+ color(
786
+ ` Output: ${outputFile} (${ndjsonData.length.toLocaleString()} bytes)`,
787
+ 'dim'
788
+ )
789
+ );
790
+ }
791
+
792
+ return {
793
+ records: jsonData.length,
794
+ bytes: ndjsonData.length,
795
+ time: elapsed
796
+ };
797
+ } catch (error: any) {
798
+ console.error(color(`✗ Error: ${error.message}`, 'red'));
799
+ if (options.debug) {
800
+ console.error(error.stack);
801
+ }
802
+ process.exit(1);
803
+ }
804
+ }
805
+
806
+ // ============================================================================
807
+ // UNWRAP/FLATTEN FUNCTION
808
+ // ============================================================================
809
+
810
+ async function unwrapJson(inputFile, outputFile, options: any): Promise<ConversionResult> {
811
+ const startTime = Date.now();
812
+
813
+ try {
814
+ if (!options.silent) {
815
+ console.log(color('Unwrapping/flattening nested JSON...', 'dim'));
816
+ }
817
+
818
+ // Read JSON file
819
+ const inputData = await fs.promises.readFile(inputFile, 'utf8');
820
+ const jsonData = JSON.parse(inputData);
821
+
822
+ const maxDepth = options.maxDepth || 10;
823
+ const separator = options.flattenPrefix || '_';
824
+
825
+ // Flatten function
826
+ const flattenObject = (obj: any, prefix = '', depth = 0): any => {
827
+ if (depth >= maxDepth) {
828
+ return { [prefix.slice(0, -1)]: JSON.stringify(obj) };
829
+ }
830
+
831
+ const result = {};
832
+
833
+ for (const [key, value] of Object.entries(obj)) {
834
+ const newKey = prefix + key;
835
+
836
+ if (value && typeof value === 'object' && !Array.isArray(value)) {
837
+ Object.assign(result, flattenObject(value, newKey + separator, depth + 1));
838
+ } else if (Array.isArray(value)) {
839
+ // Flatten arrays
840
+ if (options.unwrapArrays) {
841
+ (result as any)[newKey] = value.join(', ');
842
+ } else {
843
+ (result as any)[newKey] = JSON.stringify(value);
844
+ }
845
+ } else {
846
+ (result as any)[newKey] = value;
847
+ }
848
+ }
849
+
850
+ return result;
851
+ }
852
+
853
+ let unwrappedData;
854
+ if (Array.isArray(jsonData)) {
855
+ unwrappedData = jsonData.map(item => flattenObject(item));
856
+ if (!options.silent) {
857
+ console.log(
858
+ color(`Processing ${jsonData.length.toLocaleString()} records...`, 'dim')
859
+ );
860
+ }
861
+ } else {
862
+ unwrappedData = flattenObject(jsonData);
863
+ }
864
+
865
+ // Format JSON
866
+ const jsonOutput = options.pretty
867
+ ? JSON.stringify(unwrappedData, null, 2)
868
+ : JSON.stringify(unwrappedData);
869
+
870
+ // Write output file
871
+ await fs.promises.writeFile(outputFile, jsonOutput, 'utf8');
872
+
873
+ const elapsed = Date.now() - startTime;
874
+ const recordCount = Array.isArray(unwrappedData) ? unwrappedData.length : 1;
875
+
876
+ if (!options.silent) {
877
+ console.log(
878
+ color(
879
+ `✓ Unwrapped ${recordCount.toLocaleString()} record(s) in ${elapsed}ms`,
880
+ 'green'
881
+ )
882
+ );
883
+ console.log(
884
+ color(
885
+ ` Output: ${outputFile} (${jsonOutput.length.toLocaleString()} bytes)`,
886
+ 'dim'
887
+ )
888
+ );
889
+ }
890
+
891
+ return {
892
+ records: recordCount,
893
+ bytes: jsonOutput.length,
894
+ time: elapsed
895
+ };
896
+ } catch (error: any) {
897
+ console.error(color(`✗ Error: ${error.message}`, 'red'));
898
+ if (options.debug) {
899
+ console.error(error.stack);
900
+ }
901
+ process.exit(1);
902
+ }
903
+ }
904
+
905
+ async function preprocessJson(inputFile, outputFile, options: any): Promise<ConversionResult> {
906
+ const startTime = Date.now();
907
+
908
+ try {
909
+ // Read input file
910
+ const inputData = await fs.promises.readFile(inputFile, 'utf8');
911
+ const jsonData = JSON.parse(inputData);
912
+
913
+ if (!Array.isArray(jsonData)) {
914
+ throw new Error(
915
+ 'JSON data must be an array of objects for preprocessing'
916
+ );
917
+ }
918
+
919
+ if (!options.silent) {
920
+ console.log(
921
+ color(
922
+ `Preprocessing ${jsonData.length.toLocaleString()} records...`,
923
+ 'dim'
924
+ )
925
+ );
926
+ }
927
+
928
+ // Preprocess data
929
+ const processedData = jtcsv.preprocessData(jsonData);
930
+
931
+ // Apply transform if specified
932
+ let transformedData = processedData;
933
+ if (options.transform) {
934
+ if (!options.silent) {
935
+ console.log(
936
+ color(`Applying transform from: ${options.transform}`, 'dim')
937
+ );
938
+ }
939
+ try {
940
+ transformedData = transformLoader.applyTransform(
941
+ processedData,
942
+ options.transform
943
+ );
944
+ if (!options.silent) {
945
+ console.log(
946
+ color(
947
+ `✓ Transform applied to ${transformedData.length} records`,
948
+ 'green'
949
+ )
950
+ );
951
+ }
952
+ } catch (transformError: any) {
953
+ console.error(
954
+ color(`✗ Transform error: ${transformError.message}`, 'red')
955
+ );
956
+ if (options.debug) {
957
+ console.error(transformError.stack);
958
+ }
959
+ process.exit(1);
960
+ }
961
+ }
962
+
963
+ // Apply deep unwrap if needed
964
+ if (options.unwrapArrays || options.stringifyObjects) {
965
+ const maxDepth = options.maxDepth || 5;
966
+ transformedData.forEach((item) => {
967
+ for (const key in item) {
968
+ if (item[key] && typeof item[key] === 'object') {
969
+ item[key] = jtcsv.deepUnwrap(item[key]); // depth removed for compatibility
970
+ }
971
+ }
972
+ });
973
+ }
974
+
975
+ // Format JSON
976
+ const jsonOutput = options.pretty
977
+ ? JSON.stringify(transformedData, null, 2)
978
+ : JSON.stringify(transformedData);
979
+
980
+ // Write output file
981
+ await fs.promises.writeFile(outputFile, jsonOutput, 'utf8');
982
+
983
+ const elapsed = Date.now() - startTime;
984
+ if (!options.silent) {
985
+ console.log(
986
+ color(
987
+ `✓ Preprocessed ${transformedData.length.toLocaleString()} records in ${elapsed}ms`,
988
+ 'green'
989
+ )
990
+ );
991
+ console.log(
992
+ color(
993
+ ` Output: ${outputFile} (${jsonOutput.length.toLocaleString()} bytes)`,
994
+ 'dim'
995
+ )
996
+ );
997
+ }
998
+
999
+ return {
1000
+ records: transformedData.length,
1001
+ bytes: jsonOutput.length,
1002
+ time: elapsed
1003
+ };
1004
+ } catch (error: any) {
1005
+ console.error(color(`✗ Error: ${error.message}`, 'red'));
1006
+ if (options.debug) {
1007
+ console.error(error.stack);
1008
+ }
1009
+ process.exit(1);
1010
+ }
1011
+ }
1012
+
1013
+ // ============================================================================
1014
+ // STREAMING FUNCTIONS
1015
+ // ============================================================================
1016
+
1017
+ async function streamJsonToCsv(inputFile, outputFile, options: any): Promise<void> {
1018
+ const startTime = Date.now();
1019
+ let recordCount = 0;
1020
+
1021
+ try {
1022
+ if (!options.silent) {
1023
+ console.log(color('Streaming JSON to CSV...', 'dim'));
1024
+ }
1025
+
1026
+ // Create streams
1027
+ const readStream = fs.createReadStream(inputFile, 'utf8');
1028
+ const writeStream = fs.createWriteStream(outputFile, 'utf8');
1029
+
1030
+ // Add UTF-8 BOM if requested
1031
+ if (options.addBOM) {
1032
+ writeStream.write('\uFEFF');
1033
+ }
1034
+
1035
+ // Parse JSON stream
1036
+ let buffer = '';
1037
+ const isFirstChunk = true;
1038
+ let headersWritten = false;
1039
+
1040
+ readStream.on('data', (chunk) => {
1041
+ buffer += chunk;
1042
+
1043
+ // Try to parse complete JSON objects
1044
+ const lines = buffer.split('\n');
1045
+ buffer = lines.pop() || '';
1046
+
1047
+ for (const line of lines) {
1048
+ if (line.trim()) {
1049
+ try {
1050
+ const obj = JSON.parse(line);
1051
+ // Apply renameMap if provided
1052
+ let finalObj = obj;
1053
+ if (options.renameMap) {
1054
+ const renameMap = options.renameMap as Record<string, string>;
1055
+ finalObj = {};
1056
+ for (const [oldKey, newKey] of Object.entries(renameMap)) {
1057
+ if (oldKey in obj) {
1058
+ (finalObj as any)[newKey] = (obj as any)[oldKey];
1059
+ }
1060
+ }
1061
+ // Copy remaining fields
1062
+ for (const [key, value] of Object.entries(obj)) {
1063
+ if (!(key in renameMap)) {
1064
+ (finalObj as any)[key] = value;
1065
+ }
1066
+ }
1067
+ }
1068
+ recordCount++;
1069
+
1070
+ // Write headers on first object
1071
+ if (!headersWritten && options.includeHeaders !== false) {
1072
+ const headers = Object.keys(finalObj);
1073
+ writeStream.write(headers.join(options.delimiter || ';') + '\n');
1074
+ headersWritten = true;
1075
+ }
1076
+
1077
+ // Write CSV row
1078
+ const row =
1079
+ Object.values(finalObj)
1080
+ .map((value) => {
1081
+ const str = String(value);
1082
+ if (
1083
+ str.includes(options.delimiter || ';') ||
1084
+ str.includes('"') ||
1085
+ str.includes('\n')
1086
+ ) {
1087
+ return `"${str.replace(/"/g, '""')}"`;
1088
+ }
1089
+ return str;
1090
+ })
1091
+ .join(options.delimiter || ';') + '\n';
1092
+
1093
+ writeStream.write(row);
1094
+
1095
+ // Show progress
1096
+ if (options.verbose && recordCount % 10000 === 0) {
1097
+ process.stdout.write(
1098
+ color(
1099
+ ` Processed ${recordCount.toLocaleString()} records\r`,
1100
+ 'dim'
1101
+ )
1102
+ );
1103
+ }
1104
+ } catch (error: any) {
1105
+ // Skip invalid JSON lines
1106
+ if (options.debug) {
1107
+ console.warn(
1108
+ color(
1109
+ ` Warning: Skipping invalid JSON line: ${error.message}`,
1110
+ 'yellow'
1111
+ )
1112
+ );
1113
+ }
1114
+ }
1115
+ }
1116
+ }
1117
+ });
1118
+
1119
+ readStream.on('end', async () => {
1120
+ // Process remaining buffer
1121
+ if (buffer.trim()) {
1122
+ try {
1123
+ const obj = JSON.parse(buffer);
1124
+
1125
+ // Apply renameMap if provided
1126
+ let finalObj = obj;
1127
+ if (options.renameMap) {
1128
+ const renameMap = options.renameMap as Record<string, string>;
1129
+ finalObj = {};
1130
+ for (const [oldKey, newKey] of Object.entries(renameMap)) {
1131
+ if (oldKey in obj) {
1132
+ (finalObj as any)[newKey] = (obj as any)[oldKey];
1133
+ }
1134
+ }
1135
+ // Copy remaining fields
1136
+ for (const [key, value] of Object.entries(obj)) {
1137
+ if (!(key in renameMap)) {
1138
+ (finalObj as any)[key] = value;
1139
+ }
1140
+ }
1141
+ }
1142
+
1143
+ recordCount++;
1144
+
1145
+ if (!headersWritten && options.includeHeaders !== false) {
1146
+ const headers = Object.keys(finalObj);
1147
+ writeStream.write(headers.join(options.delimiter || ';') + '\n');
1148
+ }
1149
+
1150
+ const row =
1151
+ Object.values(finalObj)
1152
+ .map((value) => {
1153
+ const str = String(value);
1154
+ if (
1155
+ str.includes(options.delimiter || ';') ||
1156
+ str.includes('"') ||
1157
+ str.includes('\n')
1158
+ ) {
1159
+ return `"${str.replace(/"/g, '""')}"`;
1160
+ }
1161
+ return str;
1162
+ })
1163
+ .join(options.delimiter || ';') + '\n';
1164
+
1165
+ writeStream.write(row);
1166
+ } catch (error: any) {
1167
+ // Skip invalid JSON
1168
+ }
1169
+ }
1170
+
1171
+ writeStream.end();
1172
+
1173
+ // Wait for write stream to finish
1174
+ await new Promise<void>((resolve) =>
1175
+ writeStream.on('finish', () => resolve())
1176
+ );
1177
+
1178
+ const elapsed = Date.now() - startTime;
1179
+ if (!options.silent) {
1180
+ console.log(
1181
+ color(
1182
+ `\n✓ Streamed ${recordCount.toLocaleString()} records in ${elapsed}ms`,
1183
+ 'green'
1184
+ )
1185
+ );
1186
+ console.log(color(` Output: ${outputFile}`, 'dim'));
1187
+ }
1188
+ });
1189
+
1190
+ readStream.on('error', (error) => {
1191
+ console.error(color(`✗ Stream error: ${error.message}`, 'red'));
1192
+ process.exit(1);
1193
+ });
1194
+
1195
+ writeStream.on('error', (error) => {
1196
+ console.error(color(`✗ Write error: ${error.message}`, 'red'));
1197
+ process.exit(1);
1198
+ });
1199
+ } catch (error: any) {
1200
+ console.error(color(`✗ Error: ${error.message}`, 'red'));
1201
+ if (options.debug) {
1202
+ console.error(error.stack);
1203
+ }
1204
+ process.exit(1);
1205
+ }
1206
+ }
1207
+
1208
+ async function streamCsvToJson(inputFile, outputFile, options: any): Promise<void> {
1209
+ const startTime = Date.now();
1210
+ let rowCount = 0;
1211
+
1212
+ try {
1213
+ if (!options.silent) {
1214
+ console.log(color('Streaming CSV to JSON...', 'dim'));
1215
+ }
1216
+
1217
+ // Create streams
1218
+ const readStream = fs.createReadStream(inputFile, 'utf8');
1219
+ const writeStream = fs.createWriteStream(outputFile, 'utf8');
1220
+
1221
+ // Write JSON array opening bracket
1222
+ writeStream.write('[\n');
1223
+
1224
+ let buffer = '';
1225
+ let isFirstRow = true;
1226
+ let headers = [];
1227
+
1228
+ readStream.on('data', (chunk) => {
1229
+ buffer += chunk;
1230
+
1231
+ // Process complete lines
1232
+ const lines = buffer.split('\n');
1233
+ buffer = lines.pop() || '';
1234
+
1235
+ for (let i = 0; i < lines.length; i++) {
1236
+ const line = lines[i].trim();
1237
+ if (!line) {
1238
+ continue;
1239
+ }
1240
+
1241
+ rowCount++;
1242
+
1243
+ // Parse CSV line
1244
+ const fields = parseCsvLineSimple(line, options.delimiter || ';');
1245
+
1246
+ // First row might be headers
1247
+ if (rowCount === 1 && options.hasHeaders !== false) {
1248
+ headers = fields;
1249
+ continue;
1250
+ }
1251
+
1252
+ // Create JSON object
1253
+ const obj = {};
1254
+ const fieldCount = Math.min(fields.length, headers.length);
1255
+
1256
+ for (let j = 0; j < fieldCount; j++) {
1257
+ const header = headers[j] || `column${j + 1}`;
1258
+ // Apply renameMap if provided
1259
+ let finalHeader = header;
1260
+ if (options.renameMap && options.renameMap[header]) {
1261
+ finalHeader = options.renameMap[header];
1262
+ }
1263
+
1264
+ let value = fields[j];
1265
+
1266
+ // Parse numbers if enabled
1267
+ if (options.parseNumbers) {
1268
+ // Fast numeric detection
1269
+ const trimmed = value.trim();
1270
+ const firstChar = trimmed.charAt(0);
1271
+ if ((firstChar >= '0' && firstChar <= '9') || firstChar === '-' || firstChar === '.') {
1272
+ const num = parseFloat(trimmed);
1273
+ if (!isNaN(num) && isFinite(num)) {
1274
+ if (String(num) === trimmed || (trimmed.includes('.') && !isNaN(Number(trimmed)))) {
1275
+ value = num;
1276
+ }
1277
+ }
1278
+ }
1279
+ }
1280
+
1281
+ // Parse booleans if enabled
1282
+ if (options.parseBooleans) {
1283
+ const lowerValue = value.toLowerCase();
1284
+ if (lowerValue === 'true') {
1285
+ value = true;
1286
+ }
1287
+ if (lowerValue === 'false') {
1288
+ value = false;
1289
+ }
1290
+ }
1291
+
1292
+ obj[finalHeader] = value;
1293
+ }
1294
+
1295
+ // Write JSON object
1296
+ const jsonStr = JSON.stringify(obj);
1297
+ if (!isFirstRow) {
1298
+ writeStream.write(',\n');
1299
+ }
1300
+ writeStream.write(' ' + jsonStr);
1301
+ isFirstRow = false;
1302
+
1303
+ // Show progress
1304
+ if (options.verbose && rowCount % 10000 === 0) {
1305
+ process.stdout.write(
1306
+ color(` Processed ${rowCount.toLocaleString()} rows\r`, 'dim')
1307
+ );
1308
+ }
1309
+ }
1310
+ });
1311
+
1312
+ readStream.on('end', async () => {
1313
+ // Process remaining buffer
1314
+ if (buffer.trim()) {
1315
+ const fields = parseCsvLineSimple(
1316
+ buffer.trim(),
1317
+ options.delimiter || ';'
1318
+ );
1319
+
1320
+ if (fields.length > 0) {
1321
+ rowCount++;
1322
+
1323
+ // Skip if it's headers
1324
+ if (!(rowCount === 1 && options.hasHeaders !== false)) {
1325
+ const obj = {};
1326
+ const fieldCount = Math.min(fields.length, headers.length);
1327
+
1328
+ for (let j = 0; j < fieldCount; j++) {
1329
+ const header = headers[j] || `column${j + 1}`;
1330
+ // Apply renameMap if provided
1331
+ let finalHeader = header;
1332
+ if (options.renameMap && options.renameMap[header]) {
1333
+ finalHeader = options.renameMap[header];
1334
+ }
1335
+ obj[finalHeader] = fields[j];
1336
+ }
1337
+
1338
+ const jsonStr = JSON.stringify(obj);
1339
+ if (!isFirstRow) {
1340
+ writeStream.write(',\n');
1341
+ }
1342
+ writeStream.write(' ' + jsonStr);
1343
+ }
1344
+ }
1345
+ }
1346
+
1347
+ // Write JSON array closing bracket
1348
+ writeStream.write('\n]');
1349
+ writeStream.end();
1350
+
1351
+ // Wait for write stream to finish
1352
+ await new Promise<void>((resolve) =>
1353
+ writeStream.on('finish', () => resolve())
1354
+ );
1355
+
1356
+ const elapsed = Date.now() - startTime;
1357
+ if (!options.silent) {
1358
+ console.log(
1359
+ color(
1360
+ `\n✓ Streamed ${(rowCount - (options.hasHeaders !== false ? 1 : 0)).toLocaleString()} rows in ${elapsed}ms`,
1361
+ 'green'
1362
+ )
1363
+ );
1364
+ console.log(color(` Output: ${outputFile}`, 'dim'));
1365
+ }
1366
+ });
1367
+
1368
+ readStream.on('error', (error) => {
1369
+ console.error(color(`✗ Stream error: ${error.message}`, 'red'));
1370
+ process.exit(1);
1371
+ });
1372
+
1373
+ writeStream.on('error', (error) => {
1374
+ console.error(color(`✗ Write error: ${error.message}`, 'red'));
1375
+ process.exit(1);
1376
+ });
1377
+ } catch (error: any) {
1378
+ console.error(color(`✗ Error: ${error.message}`, 'red'));
1379
+ if (options.debug) {
1380
+ console.error(error.stack);
1381
+ }
1382
+ process.exit(1);
1383
+ }
1384
+ }
1385
+
1386
+ // Simple CSV line parser for streaming
1387
+ function parseCsvLineSimple(line, delimiter: any): any {
1388
+ const fields = [];
1389
+ let currentField = '';
1390
+ let inQuotes = false;
1391
+
1392
+ for (let i = 0; i < line.length; i++) {
1393
+ const char = line[i];
1394
+
1395
+ if (char === '"') {
1396
+ if (inQuotes && i + 1 < line.length && line[i + 1] === '"') {
1397
+ // Escaped quote
1398
+ currentField += '"';
1399
+ i++;
1400
+ } else {
1401
+ // Toggle quotes
1402
+ inQuotes = !inQuotes;
1403
+ }
1404
+ } else if (char === delimiter && !inQuotes) {
1405
+ fields.push(currentField);
1406
+ currentField = '';
1407
+ } else {
1408
+ currentField += char;
1409
+ }
1410
+ }
1411
+
1412
+ fields.push(currentField);
1413
+ return fields;
1414
+ }
1415
+
1416
+ // ============================================================================
1417
+ // BATCH PROCESSING FUNCTIONS
1418
+ // ============================================================================
1419
+
1420
+ async function batchJsonToCsv(inputPattern, outputDir, options: any): Promise<BatchSummary> {
1421
+ const startTime = Date.now();
1422
+
1423
+ try {
1424
+ let glob;
1425
+ try {
1426
+ glob = require('glob');
1427
+ } catch (error: any) {
1428
+ console.error(
1429
+ color(
1430
+ '✗ Error: The "glob" module is required for batch processing',
1431
+ 'red'
1432
+ )
1433
+ );
1434
+ console.error(color(' Install it with: npm install glob', 'cyan'));
1435
+ console.error(color(' Or update jtcsv: npm update jtcsv', 'cyan'));
1436
+ process.exit(1);
1437
+ }
1438
+ const files = glob.sync(inputPattern, {
1439
+ absolute: true,
1440
+ nodir: true
1441
+ });
1442
+
1443
+ if (files.length === 0) {
1444
+ console.error(
1445
+ color(`✗ No files found matching pattern: ${inputPattern}`, 'red')
1446
+ );
1447
+ process.exit(1);
1448
+ }
1449
+
1450
+ if (!options.silent) {
1451
+ console.log(color(`Found ${files.length} files to process...`, 'dim'));
1452
+ }
1453
+
1454
+ // Create output directory if it doesn't exist
1455
+ await fs.promises.mkdir(outputDir, { recursive: true });
1456
+
1457
+ const results: BatchFileResult[] = [];
1458
+ const parallelLimit = options.parallel || 4;
1459
+
1460
+ // Process files in parallel batches
1461
+ for (let i = 0; i < files.length; i += parallelLimit) {
1462
+ const batch = files.slice(i, i + parallelLimit);
1463
+ const promises = batch.map(async (file) => {
1464
+ const fileName = path.basename(file, '.json');
1465
+ const outputFile = path.join(outputDir, `${fileName}.csv`);
1466
+
1467
+ if (!options.silent && options.verbose) {
1468
+ console.log(color(` Processing: ${file}`, 'dim'));
1469
+ }
1470
+
1471
+ try {
1472
+ const result = await convertJsonToCsv(file, outputFile, {
1473
+ ...options,
1474
+ silent: true // Suppress individual file output
1475
+ });
1476
+
1477
+ if (!options.silent) {
1478
+ console.log(
1479
+ color(
1480
+ ` ✓ ${fileName}.json → ${fileName}.csv (${result.records} records)`,
1481
+ 'green'
1482
+ )
1483
+ );
1484
+ }
1485
+
1486
+ return { file, success: true, ...result };
1487
+ } catch (error: any) {
1488
+ if (!options.silent) {
1489
+ console.log(color(` ✗ ${fileName}.json: ${error.message}`, 'red'));
1490
+ }
1491
+ return { file, success: false, error: error.message };
1492
+ }
1493
+ });
1494
+
1495
+ const batchResults = await Promise.all(promises);
1496
+ results.push(...batchResults);
1497
+
1498
+ if (!options.silent) {
1499
+ const processed = i + batch.length;
1500
+ const percent = Math.round((processed / files.length) * 100);
1501
+ console.log(
1502
+ color(
1503
+ ` Progress: ${processed}/${files.length} (${percent}%)`,
1504
+ 'dim'
1505
+ )
1506
+ );
1507
+ }
1508
+ }
1509
+
1510
+ const elapsed = Date.now() - startTime;
1511
+ const successful = results.filter((r) => r.success).length;
1512
+ const totalRecords = results
1513
+ .filter((r) => r.success)
1514
+ .reduce((sum, r) => sum + (r.records || 0), 0);
1515
+
1516
+ if (!options.silent) {
1517
+ console.log(
1518
+ color(`\n✓ Batch processing completed in ${elapsed}ms`, 'green')
1519
+ );
1520
+ console.log(
1521
+ color(` Successful: ${successful}/${files.length} files`, 'dim')
1522
+ );
1523
+ console.log(
1524
+ color(` Total records: ${totalRecords.toLocaleString()}`, 'dim')
1525
+ );
1526
+ console.log(color(` Output directory: ${outputDir}`, 'dim'));
1527
+ }
1528
+
1529
+ return {
1530
+ totalFiles: files.length,
1531
+ successful,
1532
+ totalRecords,
1533
+ time: elapsed,
1534
+ results
1535
+ };
1536
+ } catch (error: any) {
1537
+ console.error(color(`✗ Batch processing error: ${error.message}`, 'red'));
1538
+ if (options.debug) {
1539
+ console.error(error.stack);
1540
+ }
1541
+ process.exit(1);
1542
+ }
1543
+ }
1544
+
1545
+ async function batchCsvToJson(inputPattern, outputDir, options: any): Promise<BatchSummary> {
1546
+ const startTime = Date.now();
1547
+
1548
+ try {
1549
+ let glob;
1550
+ try {
1551
+ glob = require('glob');
1552
+ } catch (error: any) {
1553
+ console.error(
1554
+ color(
1555
+ '✗ Error: The "glob" module is required for batch processing',
1556
+ 'red'
1557
+ )
1558
+ );
1559
+ console.error(color(' Install it with: npm install glob', 'cyan'));
1560
+ console.error(color(' Or update jtcsv: npm update jtcsv', 'cyan'));
1561
+ process.exit(1);
1562
+ }
1563
+ const files = glob.sync(inputPattern, {
1564
+ absolute: true,
1565
+ nodir: true
1566
+ });
1567
+
1568
+ if (files.length === 0) {
1569
+ console.error(
1570
+ color(`✗ No files found matching pattern: ${inputPattern}`, 'red')
1571
+ );
1572
+ process.exit(1);
1573
+ }
1574
+
1575
+ if (!options.silent) {
1576
+ console.log(color(`Found ${files.length} files to process...`, 'dim'));
1577
+ }
1578
+
1579
+ // Create output directory if it doesn't exist
1580
+ await fs.promises.mkdir(outputDir, { recursive: true });
1581
+
1582
+ const results: BatchFileResult[] = [];
1583
+ const parallelLimit = options.parallel || 4;
1584
+
1585
+ // Process files in parallel batches
1586
+ for (let i = 0; i < files.length; i += parallelLimit) {
1587
+ const batch = files.slice(i, i + parallelLimit);
1588
+ const promises = batch.map(async (file) => {
1589
+ const fileName = path.basename(file, '.csv');
1590
+ const outputFile = path.join(outputDir, `${fileName}.json`);
1591
+
1592
+ if (!options.silent && options.verbose) {
1593
+ console.log(color(` Processing: ${file}`, 'dim'));
1594
+ }
1595
+
1596
+ try {
1597
+ const result = await convertCsvToJson(file, outputFile, {
1598
+ ...options,
1599
+ silent: true // Suppress individual file output
1600
+ });
1601
+
1602
+ if (!options.silent) {
1603
+ console.log(
1604
+ color(
1605
+ ` ✓ ${fileName}.csv → ${fileName}.json (${result.rows} rows)`,
1606
+ 'green'
1607
+ )
1608
+ );
1609
+ }
1610
+
1611
+ return { file, success: true, ...result };
1612
+ } catch (error: any) {
1613
+ if (!options.silent) {
1614
+ console.log(color(` ✗ ${fileName}.csv: ${error.message}`, 'red'));
1615
+ }
1616
+ return { file, success: false, error: error.message };
1617
+ }
1618
+ });
1619
+
1620
+ const batchResults = await Promise.all(promises);
1621
+ results.push(...batchResults);
1622
+
1623
+ if (!options.silent) {
1624
+ const processed = i + batch.length;
1625
+ const percent = Math.round((processed / files.length) * 100);
1626
+ console.log(
1627
+ color(
1628
+ ` Progress: ${processed}/${files.length} (${percent}%)`,
1629
+ 'dim'
1630
+ )
1631
+ );
1632
+ }
1633
+ }
1634
+
1635
+ const elapsed = Date.now() - startTime;
1636
+ const successful = results.filter((r) => r.success).length;
1637
+ const totalRows = results
1638
+ .filter((r) => r.success)
1639
+ .reduce((sum, r) => sum + (r.rows || 0), 0);
1640
+
1641
+ if (!options.silent) {
1642
+ console.log(
1643
+ color(`\n✓ Batch processing completed in ${elapsed}ms`, 'green')
1644
+ );
1645
+ console.log(
1646
+ color(` Successful: ${successful}/${files.length} files`, 'dim')
1647
+ );
1648
+ console.log(color(` Total rows: ${totalRows.toLocaleString()}`, 'dim'));
1649
+ console.log(color(` Output directory: ${outputDir}`, 'dim'));
1650
+ }
1651
+
1652
+ return {
1653
+ totalFiles: files.length,
1654
+ successful,
1655
+ totalRows,
1656
+ time: elapsed,
1657
+ results
1658
+ };
1659
+ } catch (error: any) {
1660
+ console.error(color(`✗ Batch processing error: ${error.message}`, 'red'));
1661
+ if (options.debug) {
1662
+ console.error(error.stack);
1663
+ }
1664
+ process.exit(1);
1665
+ }
1666
+ }
1667
+
1668
+ async function batchProcessMixed(inputPattern, outputDir, options: any): Promise<BatchSummary> {
1669
+ const startTime = Date.now();
1670
+
1671
+ try {
1672
+ let glob;
1673
+ try {
1674
+ glob = require('glob');
1675
+ } catch (error: any) {
1676
+ console.error(
1677
+ color(
1678
+ '✗ Error: The "glob" module is required for batch processing',
1679
+ 'red'
1680
+ )
1681
+ );
1682
+ console.error(color(' Install it with: npm install glob', 'cyan'));
1683
+ console.error(color(' Or update jtcsv: npm update jtcsv', 'cyan'));
1684
+ process.exit(1);
1685
+ }
1686
+
1687
+ // Находим все файлы
1688
+ const files = glob.sync(inputPattern, {
1689
+ absolute: true,
1690
+ nodir: true
1691
+ });
1692
+
1693
+ if (files.length === 0) {
1694
+ console.error(
1695
+ color(`✗ No files found matching pattern: ${inputPattern}`, 'red')
1696
+ );
1697
+ process.exit(1);
1698
+ }
1699
+
1700
+ if (!options.silent) {
1701
+ console.log(color(`Found ${files.length} files to process...`, 'dim'));
1702
+ }
1703
+
1704
+ // Создаем выходную директорию
1705
+ await fs.promises.mkdir(outputDir, { recursive: true });
1706
+
1707
+ const results: BatchFileResult[] = [];
1708
+ const parallelLimit = options.parallel || 4;
1709
+
1710
+ // Группируем файлы по типу
1711
+ const jsonFiles = files.filter((file) =>
1712
+ file.toLowerCase().endsWith('.json')
1713
+ );
1714
+ const csvFiles = files.filter((file) =>
1715
+ file.toLowerCase().endsWith('.csv')
1716
+ );
1717
+ const otherFiles = files.filter(
1718
+ (file) =>
1719
+ !file.toLowerCase().endsWith('.json') &&
1720
+ !file.toLowerCase().endsWith('.csv')
1721
+ );
1722
+
1723
+ if (otherFiles.length > 0 && !options.silent) {
1724
+ console.log(
1725
+ color(
1726
+ ` Warning: Skipping ${otherFiles.length} non-JSON/CSV files`,
1727
+ 'yellow'
1728
+ )
1729
+ );
1730
+ }
1731
+
1732
+ // Обрабатываем JSON файлы
1733
+ for (let i = 0; i < jsonFiles.length; i += parallelLimit) {
1734
+ const batch = jsonFiles.slice(i, i + parallelLimit);
1735
+ const promises = batch.map(async (file) => {
1736
+ const fileName = path.basename(file, '.json');
1737
+ const outputFile = path.join(outputDir, `${fileName}.csv`);
1738
+
1739
+ if (!options.silent && options.verbose) {
1740
+ console.log(color(` Processing JSON: ${file}`, 'dim'));
1741
+ }
1742
+
1743
+ try {
1744
+ const result = await convertJsonToCsv(file, outputFile, {
1745
+ ...options,
1746
+ silent: true
1747
+ });
1748
+
1749
+ if (!options.silent) {
1750
+ console.log(
1751
+ color(
1752
+ ` ✓ ${fileName}.json → ${fileName}.csv (${result.records} records)`,
1753
+ 'green'
1754
+ )
1755
+ );
1756
+ }
1757
+
1758
+ return { file, type: 'json', success: true, ...result };
1759
+ } catch (error: any) {
1760
+ if (!options.silent) {
1761
+ console.log(color(` ✗ ${fileName}.json: ${error.message}`, 'red'));
1762
+ }
1763
+ return { file, type: 'json', success: false, error: error.message };
1764
+ }
1765
+ });
1766
+
1767
+ const batchResults = await Promise.all(promises);
1768
+ results.push(...batchResults);
1769
+ }
1770
+
1771
+ // Обрабатываем CSV файлы
1772
+ for (let i = 0; i < csvFiles.length; i += parallelLimit) {
1773
+ const batch = csvFiles.slice(i, i + parallelLimit);
1774
+ const promises = batch.map(async (file) => {
1775
+ const fileName = path.basename(file, '.csv');
1776
+ const outputFile = path.join(outputDir, `${fileName}.json`);
1777
+
1778
+ if (!options.silent && options.verbose) {
1779
+ console.log(color(` Processing CSV: ${file}`, 'dim'));
1780
+ }
1781
+
1782
+ try {
1783
+ const result = await convertCsvToJson(file, outputFile, {
1784
+ ...options,
1785
+ silent: true
1786
+ });
1787
+
1788
+ if (!options.silent) {
1789
+ console.log(
1790
+ color(
1791
+ ` ✓ ${fileName}.csv → ${fileName}.json (${result.rows} rows)`,
1792
+ 'green'
1793
+ )
1794
+ );
1795
+ }
1796
+
1797
+ return { file, type: 'csv', success: true, ...result };
1798
+ } catch (error: any) {
1799
+ if (!options.silent) {
1800
+ console.log(color(` ✗ ${fileName}.csv: ${error.message}`, 'red'));
1801
+ }
1802
+ return { file, type: 'csv', success: false, error: error.message };
1803
+ }
1804
+ });
1805
+
1806
+ const batchResults = await Promise.all(promises);
1807
+ results.push(...batchResults);
1808
+ }
1809
+
1810
+ const elapsed = Date.now() - startTime;
1811
+ const successful = results.filter((r) => r.success).length;
1812
+ const totalRecords = results
1813
+ .filter((r) => r.success)
1814
+ .reduce((sum, r) => sum + (r.records || r.rows || 0), 0);
1815
+
1816
+ if (!options.silent) {
1817
+ console.log(
1818
+ color(`\n✓ Mixed batch processing completed in ${elapsed}ms`, 'green')
1819
+ );
1820
+ console.log(
1821
+ color(` Successful: ${successful}/${files.length} files`, 'dim')
1822
+ );
1823
+ console.log(
1824
+ color(
1825
+ ` JSON files: ${jsonFiles.length}, CSV files: ${csvFiles.length}`,
1826
+ 'dim'
1827
+ )
1828
+ );
1829
+ console.log(
1830
+ color(` Total records: ${totalRecords.toLocaleString()}`, 'dim')
1831
+ );
1832
+ console.log(color(` Output directory: ${outputDir}`, 'dim'));
1833
+ }
1834
+
1835
+ return {
1836
+ totalFiles: files.length,
1837
+ jsonFiles: jsonFiles.length,
1838
+ csvFiles: csvFiles.length,
1839
+ otherFiles: otherFiles.length,
1840
+ successful,
1841
+ totalRecords,
1842
+ time: elapsed,
1843
+ results
1844
+ };
1845
+ } catch (error: any) {
1846
+ console.error(color(`✗ Batch processing error: ${error.message}`, 'red'));
1847
+ if (options.debug) {
1848
+ console.error(error.stack);
1849
+ }
1850
+ process.exit(1);
1851
+ }
1852
+ }
1853
+
1854
+ // ============================================================================
1855
+ // OPTIONS PARSING
1856
+ // ============================================================================
1857
+
1858
+ function parseOptions(args: any): any {
1859
+ const options = {
1860
+ delimiter: ';',
1861
+ autoDetect: true,
1862
+ candidates: [';', ',', '\t', '|'],
1863
+ hasHeaders: true,
1864
+ includeHeaders: true,
1865
+ renameMap: undefined,
1866
+ template: undefined,
1867
+ trim: true,
1868
+ parseNumbers: false,
1869
+ parseBooleans: false,
1870
+ useFastPath: true,
1871
+ fastPathMode: 'objects',
1872
+ preventCsvInjection: true,
1873
+ rfc4180Compliant: true,
1874
+ maxRecords: undefined,
1875
+ maxRows: undefined,
1876
+ maxDepth: 5,
1877
+ pretty: false,
1878
+ silent: false,
1879
+ verbose: false,
1880
+ debug: false,
1881
+ dryRun: false,
1882
+ addBOM: false,
1883
+ unwrapArrays: false,
1884
+ stringifyObjects: false,
1885
+ recursive: false,
1886
+ pattern: '**/*',
1887
+ outputDir: './output',
1888
+ overwrite: false,
1889
+ parallel: 4,
1890
+ chunkSize: 65536,
1891
+ bufferSize: 1000,
1892
+ schema: undefined,
1893
+ transform: undefined,
1894
+ flattenPrefix: '_',
1895
+ flatten: false,
1896
+ flattenSeparator: '.',
1897
+ flattenMaxDepth: 3,
1898
+ arrayHandling: 'stringify',
1899
+ port: 3000,
1900
+ host: 'localhost'
1901
+ };
1902
+
1903
+ const files = [];
1904
+
1905
+ for (let i = 0; i < args.length; i++) {
1906
+ const arg = args[i];
1907
+
1908
+ if (arg.startsWith('--')) {
1909
+ const [key, value] = arg.slice(2).split('=');
1910
+
1911
+ switch (key) {
1912
+ case 'delimiter':
1913
+ options.delimiter = value || ',';
1914
+ options.autoDetect = false;
1915
+ break;
1916
+ case 'auto-detect':
1917
+ options.autoDetect = value !== 'false';
1918
+ break;
1919
+ case 'candidates':
1920
+ options.candidates = value ? value.split(',') : [';', ',', '\t', '|'];
1921
+ break;
1922
+ case 'no-headers':
1923
+ options.includeHeaders = false;
1924
+ options.hasHeaders = false;
1925
+ break;
1926
+ case 'parse-numbers':
1927
+ options.parseNumbers = true;
1928
+ break;
1929
+ case 'parse-booleans':
1930
+ options.parseBooleans = true;
1931
+ break;
1932
+ case 'no-trim':
1933
+ options.trim = false;
1934
+ break;
1935
+ case 'no-fast-path':
1936
+ options.useFastPath = false;
1937
+ break;
1938
+ case 'fast-path':
1939
+ options.useFastPath = value !== 'false';
1940
+ break;
1941
+ case 'fast-path-mode':
1942
+ options.fastPathMode = value || 'objects';
1943
+ if (
1944
+ options.fastPathMode !== 'objects' &&
1945
+ options.fastPathMode !== 'compact'
1946
+ ) {
1947
+ throw new Error('Invalid --fast-path-mode value (objects|compact)');
1948
+ }
1949
+ break;
1950
+ case 'rename':
1951
+ try {
1952
+ const jsonStr = value || '{}';
1953
+ const cleanStr = jsonStr
1954
+ .replace(/^'|'$/g, '')
1955
+ .replace(/^"|"$/g, '');
1956
+ options.renameMap = JSON.parse(cleanStr);
1957
+ } catch (e) {
1958
+ throw new Error(`Invalid JSON in --rename option: ${e.message}`);
1959
+ }
1960
+ break;
1961
+ case 'template':
1962
+ try {
1963
+ const jsonStr = value || '{}';
1964
+ const cleanStr = jsonStr
1965
+ .replace(/^'|'$/g, '')
1966
+ .replace(/^"|"$/g, '');
1967
+ options.template = JSON.parse(cleanStr);
1968
+ } catch (e) {
1969
+ throw new Error(`Invalid JSON in --template option: ${e.message}`);
1970
+ }
1971
+ break;
1972
+ case 'no-injection-protection':
1973
+ options.preventCsvInjection = false;
1974
+ break;
1975
+ case 'no-rfc4180':
1976
+ options.rfc4180Compliant = false;
1977
+ break;
1978
+ case 'max-records':
1979
+ options.maxRecords = parseInt(value, 10);
1980
+ break;
1981
+ case 'max-rows':
1982
+ options.maxRows = parseInt(value, 10);
1983
+ break;
1984
+ case 'max-depth':
1985
+ options.maxDepth = parseInt(value, 10) || 5;
1986
+ break;
1987
+ case 'pretty':
1988
+ options.pretty = true;
1989
+ break;
1990
+ case 'flatten':
1991
+ options.flatten = true;
1992
+ break;
1993
+ case 'flatten-separator':
1994
+ options.flattenSeparator = value || '.';
1995
+ break;
1996
+ case 'flatten-max-depth':
1997
+ options.flattenMaxDepth = parseInt(value, 10) || 3;
1998
+ break;
1999
+ case 'array-handling':
2000
+ options.arrayHandling = value || 'stringify';
2001
+ if (!['stringify', 'join', 'expand'].includes(options.arrayHandling)) {
2002
+ throw new Error('Invalid --array-handling value (stringify|join|expand)');
2003
+ }
2004
+ break;
2005
+ case 'silent':
2006
+ options.silent = true;
2007
+ break;
2008
+ case 'verbose':
2009
+ options.verbose = true;
2010
+ break;
2011
+ case 'debug':
2012
+ options.debug = true;
2013
+ break;
2014
+ case 'dry-run':
2015
+ options.dryRun = true;
2016
+ break;
2017
+ case 'add-bom':
2018
+ options.addBOM = true;
2019
+ break;
2020
+ case 'unwrap-arrays':
2021
+ options.unwrapArrays = true;
2022
+ break;
2023
+ case 'stringify-objects':
2024
+ options.stringifyObjects = true;
2025
+ break;
2026
+ case 'recursive':
2027
+ options.recursive = true;
2028
+ break;
2029
+ case 'pattern':
2030
+ options.pattern = value || '**/*';
2031
+ break;
2032
+ case 'output-dir':
2033
+ options.outputDir = value || './output';
2034
+ break;
2035
+ case 'overwrite':
2036
+ options.overwrite = true;
2037
+ break;
2038
+ case 'parallel':
2039
+ options.parallel = parseInt(value, 10) || 4;
2040
+ break;
2041
+ case 'chunk-size':
2042
+ options.chunkSize = parseInt(value, 10) || 65536;
2043
+ break;
2044
+ case 'buffer-size':
2045
+ options.bufferSize = parseInt(value, 10) || 1000;
2046
+ break;
2047
+ case 'schema':
2048
+ try {
2049
+ const jsonStr = value || '{}';
2050
+ const cleanStr = jsonStr
2051
+ .replace(/^'|'$/g, '')
2052
+ .replace(/^"|"$/g, '');
2053
+ options.schema = JSON.parse(cleanStr);
2054
+ } catch (e) {
2055
+ throw new Error(`Invalid JSON in --schema option: ${e.message}`);
2056
+ }
2057
+ break;
2058
+ case 'transform':
2059
+ options.transform = value;
2060
+ break;
2061
+ case 'port':
2062
+ options.port = parseInt(value, 10) || 3000;
2063
+ break;
2064
+ case 'host':
2065
+ options.host = value || 'localhost';
2066
+ break;
2067
+ }
2068
+ } else if (!arg.startsWith('-')) {
2069
+ files.push(arg);
2070
+ }
2071
+ }
2072
+
2073
+ return { options, files };
2074
+ }
2075
+
2076
+ // ============================================================================
2077
+ // TUI LAUNCHER
2078
+ // ============================================================================
2079
+
2080
+ async function launchTUI(): Promise<void> {
2081
+ try {
2082
+ console.log(color('Launching Terminal User Interface...', 'cyan'));
2083
+ console.log(color('Press Ctrl+Q to exit', 'dim'));
2084
+
2085
+ const JtcsvTUI = require('@jtcsv/tui');
2086
+ const tui = new JtcsvTUI();
2087
+ tui.start();
2088
+ } catch (error: any) {
2089
+ if (error.code === 'MODULE_NOT_FOUND') {
2090
+ console.error(color('Error: @jtcsv/tui is not installed', 'red'));
2091
+ console.log(color('Install it with:', 'dim'));
2092
+ console.log(color(' npm install @jtcsv/tui', 'cyan'));
2093
+ console.log(color('\nOr use the CLI interface instead:', 'dim'));
2094
+ console.log(color(' jtcsv help', 'cyan'));
2095
+ } else {
2096
+ console.error(color(`Error: ${error.message}`, 'red'));
2097
+ }
2098
+ process.exit(1);
2099
+ }
2100
+ }
2101
+
2102
+ async function launchWebUI(options: any = {}): Promise<void> {
2103
+ try {
2104
+ const webServer = require('../src/web-server');
2105
+ webServer.startServer({
2106
+ port: options.port || 3000,
2107
+ host: options.host || 'localhost'
2108
+ });
2109
+ } catch (error: any) {
2110
+ console.error(color(`Error: ${error.message}`, 'red'));
2111
+ if (options.debug) {
2112
+ console.error(error.stack);
2113
+ }
2114
+ process.exit(1);
2115
+ }
2116
+ }
2117
+
2118
+ async function startBasicTUI(): Promise<void> {
2119
+
2120
+ const rl = readline.createInterface({
2121
+ input: process.stdin,
2122
+ output: process.stdout
2123
+ });
2124
+
2125
+ console.clear();
2126
+ console.log(color('╔══════════════════════════════════════╗', 'cyan'));
2127
+ console.log(color('║ JTCSV Terminal Interface ║', 'cyan'));
2128
+ console.log(color('╚══════════════════════════════════════╝', 'cyan'));
2129
+ console.log();
2130
+ console.log(color('Select operation:', 'bright'));
2131
+ console.log(' 1. JSON → CSV');
2132
+ console.log(' 2. CSV → JSON');
2133
+ console.log(' 3. Preprocess JSON');
2134
+ console.log(' 4. Batch Processing');
2135
+ console.log(' 5. Exit');
2136
+ console.log();
2137
+
2138
+ rl.question(color('Enter choice (1-5): ', 'cyan'), async (choice) => {
2139
+ switch (choice) {
2140
+ case '1':
2141
+ await runJsonToCsvTUI(rl);
2142
+ break;
2143
+ case '2':
2144
+ await runCsvToJsonTUI(rl);
2145
+ break;
2146
+ case '3':
2147
+ console.log(color('Preprocess feature coming soon...', 'yellow'));
2148
+ rl.close();
2149
+ break;
2150
+ case '4':
2151
+ console.log(color('Batch processing coming soon...', 'yellow'));
2152
+ rl.close();
2153
+ break;
2154
+ case '5':
2155
+ console.log(color('Goodbye!', 'green'));
2156
+ rl.close();
2157
+ process.exit(0);
2158
+ break;
2159
+ default:
2160
+ console.log(color('Invalid choice', 'red'));
2161
+ rl.close();
2162
+ process.exit(1);
2163
+ }
2164
+ });
2165
+ }
2166
+
2167
+ async function runJsonToCsvTUI(rl: any): Promise<void> {
2168
+ console.clear();
2169
+ console.log(color('JSON → CSV Conversion', 'cyan'));
2170
+ console.log();
2171
+
2172
+ rl.question('Input JSON file: ', (inputFile) => {
2173
+ rl.question('Output CSV file: ', async (outputFile) => {
2174
+ rl.question('Delimiter (default: ;): ', async (delimiter) => {
2175
+ try {
2176
+ console.log(color('\nConverting...', 'dim'));
2177
+
2178
+ const result = await convertJsonToCsv(inputFile, outputFile, {
2179
+ delimiter: delimiter || ';',
2180
+ silent: false
2181
+ });
2182
+
2183
+ console.log(color('\n✓ Conversion complete!', 'green'));
2184
+ rl.question('\nPress Enter to continue...', () => {
2185
+ rl.close();
2186
+ startBasicTUI();
2187
+ });
2188
+ } catch (error: any) {
2189
+ console.error(color(`✗ Error: ${error.message}`, 'red'));
2190
+ rl.close();
2191
+ process.exit(1);
2192
+ }
2193
+ });
2194
+ });
2195
+ });
2196
+ }
2197
+
2198
+ async function runCsvToJsonTUI(rl: any): Promise<void> {
2199
+ console.clear();
2200
+ console.log(color('CSV → JSON Conversion', 'cyan'));
2201
+ console.log();
2202
+
2203
+ rl.question('Input CSV file: ', (inputFile) => {
2204
+ rl.question('Output JSON file: ', async (outputFile) => {
2205
+ rl.question('Delimiter (default: ;): ', async (delimiter) => {
2206
+ rl.question('Pretty print? (y/n): ', async (pretty) => {
2207
+ try {
2208
+ console.log(color('\nConverting...', 'dim'));
2209
+
2210
+ const result = await convertCsvToJson(inputFile, outputFile, {
2211
+ delimiter: delimiter || ';',
2212
+ pretty: pretty.toLowerCase() === 'y',
2213
+ silent: false
2214
+ });
2215
+
2216
+ console.log(color('\n✓ Conversion complete!', 'green'));
2217
+ rl.question('\nPress Enter to continue...', () => {
2218
+ rl.close();
2219
+ startBasicTUI();
2220
+ });
2221
+ } catch (error: any) {
2222
+ console.error(color(`✗ Error: ${error.message}`, 'red'));
2223
+ rl.close();
2224
+ process.exit(1);
2225
+ }
2226
+ });
2227
+ });
2228
+ });
2229
+ });
2230
+ }
2231
+
2232
+ // ============================================================================
2233
+ // MAIN FUNCTION
2234
+ // ============================================================================
2235
+
2236
+ async function main(): Promise<void> {
2237
+ const args = process.argv.slice(2);
2238
+
2239
+ if (args.length === 0) {
2240
+ showHelp();
2241
+ return;
2242
+ }
2243
+
2244
+ const command = args[0].toLowerCase();
2245
+ const { options, files } = parseOptions(args.slice(1));
2246
+
2247
+ // Handle dry run
2248
+ if (options.dryRun) {
2249
+ console.log(color('DRY RUN - No files will be modified', 'yellow'));
2250
+ console.log(`Command: ${command}`);
2251
+ console.log(`Files: ${files.join(', ')}`);
2252
+ console.log('Options:', options);
2253
+ return;
2254
+ }
2255
+
2256
+ // Suppress output if silent mode
2257
+ if (options.silent) {
2258
+ console.log = () => {};
2259
+ console.info = () => {};
2260
+ }
2261
+
2262
+ switch (command) {
2263
+ // Main conversion commands
2264
+ case 'json-to-csv':
2265
+ case 'json2csv':
2266
+ if (files.length < 2) {
2267
+ console.error(color('Error: Input and output files required', 'red'));
2268
+ console.log(
2269
+ color('Usage: jtcsv json-to-csv input.json output.csv', 'cyan')
2270
+ );
2271
+ process.exit(1);
2272
+ }
2273
+ await convertJsonToCsv(files[0], files[1], options);
2274
+ break;
2275
+
2276
+ case 'csv-to-json':
2277
+ case 'csv2json':
2278
+ if (files.length < 2) {
2279
+ console.error(color('Error: Input and output files required', 'red'));
2280
+ console.log(
2281
+ color('Usage: jtcsv csv-to-json input.csv output.json', 'cyan')
2282
+ );
2283
+ process.exit(1);
2284
+ }
2285
+ await convertCsvToJson(files[0], files[1], options);
2286
+ break;
2287
+
2288
+ case 'save-json':
2289
+ if (files.length < 2) {
2290
+ console.error(color('Error: Input and output files required', 'red'));
2291
+ console.log(
2292
+ color('Usage: jtcsv save-json input.json output.json', 'cyan')
2293
+ );
2294
+ process.exit(1);
2295
+ }
2296
+ await saveAsJson(files[0], files[1], options);
2297
+ break;
2298
+
2299
+ case 'save-csv':
2300
+ if (files.length < 2) {
2301
+ console.error(color('Error: Input and output files required', 'red'));
2302
+ console.log(
2303
+ color('Usage: jtcsv save-csv input.csv output.csv', 'cyan')
2304
+ );
2305
+ process.exit(1);
2306
+ }
2307
+ await saveAsCsv(files[0], files[1], options);
2308
+ break;
2309
+
2310
+ case 'preprocess':
2311
+ if (files.length < 2) {
2312
+ console.error(color('Error: Input and output files required', 'red'));
2313
+ console.log(
2314
+ color('Usage: jtcsv preprocess input.json output.json', 'cyan')
2315
+ );
2316
+ process.exit(1);
2317
+ }
2318
+ await preprocessJson(files[0], files[1], options);
2319
+ break;
2320
+
2321
+ // NDJSON commands
2322
+ case 'ndjson-to-csv':
2323
+ if (files.length < 2) {
2324
+ console.error(color('Error: Input and output files required', 'red'));
2325
+ console.log(
2326
+ color('Usage: jtcsv ndjson-to-csv input.ndjson output.csv', 'cyan')
2327
+ );
2328
+ process.exit(1);
2329
+ }
2330
+ await convertNdjsonToCsv(files[0], files[1], options);
2331
+ break;
2332
+
2333
+ case 'csv-to-ndjson':
2334
+ if (files.length < 2) {
2335
+ console.error(color('Error: Input and output files required', 'red'));
2336
+ console.log(
2337
+ color('Usage: jtcsv csv-to-ndjson input.csv output.ndjson', 'cyan')
2338
+ );
2339
+ process.exit(1);
2340
+ }
2341
+ await convertCsvToNdjson(files[0], files[1], options);
2342
+ break;
2343
+
2344
+ case 'ndjson-to-json':
2345
+ if (files.length < 2) {
2346
+ console.error(color('Error: Input and output files required', 'red'));
2347
+ console.log(
2348
+ color('Usage: jtcsv ndjson-to-json input.ndjson output.json', 'cyan')
2349
+ );
2350
+ process.exit(1);
2351
+ }
2352
+ await convertNdjsonToJson(files[0], files[1], options);
2353
+ break;
2354
+
2355
+ case 'json-to-ndjson':
2356
+ if (files.length < 2) {
2357
+ console.error(color('Error: Input and output files required', 'red'));
2358
+ console.log(
2359
+ color('Usage: jtcsv json-to-ndjson input.json output.ndjson', 'cyan')
2360
+ );
2361
+ process.exit(1);
2362
+ }
2363
+ await convertJsonToNdjson(files[0], files[1], options);
2364
+ break;
2365
+
2366
+ // Unwrap/Flatten command
2367
+ case 'unwrap':
2368
+ case 'flatten':
2369
+ if (files.length < 2) {
2370
+ console.error(color('Error: Input and output files required', 'red'));
2371
+ console.log(
2372
+ color('Usage: jtcsv unwrap input.json output.json', 'cyan')
2373
+ );
2374
+ process.exit(1);
2375
+ }
2376
+ await unwrapJson(files[0], files[1], options);
2377
+ break;
2378
+
2379
+ // Streaming commands
2380
+ case 'stream':
2381
+ if (args.length < 2) {
2382
+ console.error(
2383
+ color('Error: Streaming mode requires subcommand', 'red')
2384
+ );
2385
+ console.log(
2386
+ color(
2387
+ 'Usage: jtcsv stream [json-to-csv|csv-to-json|file-to-csv|file-to-json]',
2388
+ 'cyan'
2389
+ )
2390
+ );
2391
+ process.exit(1);
2392
+ }
2393
+
2394
+ const streamCommand = args[1].toLowerCase();
2395
+ // Для stream команд нужно парсить опции начиная с 3-го аргумента (после stream и подкоманды)
2396
+ const streamArgs = args.slice(2);
2397
+ const { options: streamOptions, files: streamFiles } =
2398
+ parseOptions(streamArgs);
2399
+
2400
+ if (streamCommand === 'json-to-csv' && streamFiles.length >= 2) {
2401
+ await streamJsonToCsv(streamFiles[0], streamFiles[1], streamOptions);
2402
+ } else if (streamCommand === 'csv-to-json' && streamFiles.length >= 2) {
2403
+ await streamCsvToJson(streamFiles[0], streamFiles[1], streamOptions);
2404
+ } else if (streamCommand === 'file-to-csv' && streamFiles.length >= 2) {
2405
+ // Use jtcsv streaming API if available
2406
+ try {
2407
+ const readStream = fs.createReadStream(streamFiles[0], 'utf8');
2408
+ const writeStream = fs.createWriteStream(streamFiles[1], 'utf8');
2409
+
2410
+ if (streamOptions.addBOM) {
2411
+ writeStream.write('\uFEFF');
2412
+ }
2413
+
2414
+ const transformStream = jtcsv.createJsonToCsvStream(streamOptions);
2415
+ await pipeline(readStream, transformStream, writeStream);
2416
+
2417
+ console.log(color('✓ File streamed successfully', 'green'));
2418
+ } catch (error: any) {
2419
+ console.error(color(`✗ Streaming error: ${error.message}`, 'red'));
2420
+ process.exit(1);
2421
+ }
2422
+ } else if (streamCommand === 'file-to-json' && streamFiles.length >= 2) {
2423
+ // Use jtcsv streaming API if available
2424
+ try {
2425
+ const readStream = fs.createReadStream(streamFiles[0], 'utf8');
2426
+ const writeStream = fs.createWriteStream(streamFiles[1], 'utf8');
2427
+
2428
+ const transformStream = jtcsv.createCsvToJsonStream(streamOptions);
2429
+ await pipeline(readStream, transformStream, writeStream);
2430
+
2431
+ console.log(color('✓ File streamed successfully', 'green'));
2432
+ } catch (error: any) {
2433
+ console.error(color(`✗ Streaming error: ${error.message}`, 'red'));
2434
+ process.exit(1);
2435
+ }
2436
+ } else {
2437
+ console.error(
2438
+ color('Error: Invalid streaming command or missing files', 'red')
2439
+ );
2440
+ process.exit(1);
2441
+ }
2442
+ break;
2443
+
2444
+ // Batch processing commands
2445
+ case 'batch':
2446
+ if (args.length < 2) {
2447
+ console.error(color('Error: Batch mode requires subcommand', 'red'));
2448
+ console.log(
2449
+ color('Usage: jtcsv batch [json-to-csv|csv-to-json|process]', 'cyan')
2450
+ );
2451
+ process.exit(1);
2452
+ }
2453
+
2454
+ const batchCommand = args[1].toLowerCase();
2455
+ // Для batch команд нужно парсить опции начиная с 3-го аргумента (после batch и подкоманды)
2456
+ const batchArgs = args.slice(2);
2457
+ const { options: batchOptions, files: batchFiles } =
2458
+ parseOptions(batchArgs);
2459
+
2460
+ if (batchCommand === 'json-to-csv' && batchFiles.length >= 2) {
2461
+ await batchJsonToCsv(batchFiles[0], batchFiles[1], batchOptions);
2462
+ } else if (batchCommand === 'csv-to-json' && batchFiles.length >= 2) {
2463
+ await batchCsvToJson(batchFiles[0], batchFiles[1], batchOptions);
2464
+ } else if (batchCommand === 'process' && files.length >= 2) {
2465
+ await batchProcessMixed(files[0], files[1], options);
2466
+ } else {
2467
+ console.error(
2468
+ color('Error: Invalid batch command or missing files', 'red')
2469
+ );
2470
+ process.exit(1);
2471
+ }
2472
+ break;
2473
+
2474
+ // TUI command
2475
+ case 'tui':
2476
+ await launchTUI();
2477
+ break;
2478
+
2479
+ // Web UI command
2480
+ case 'web':
2481
+ await launchWebUI(options);
2482
+ break;
2483
+
2484
+ // Help and version
2485
+ case 'help':
2486
+ case '--help':
2487
+ case '-h':
2488
+ showHelp();
2489
+ break;
2490
+
2491
+ case 'version':
2492
+ case '-v':
2493
+ case '--version':
2494
+ showVersion();
2495
+ break;
2496
+
2497
+ default:
2498
+ console.error(color(`Error: Unknown command '${command}'`, 'red'));
2499
+ console.log(color('Use jtcsv help for available commands', 'cyan'));
2500
+ process.exit(1);
2501
+ }
2502
+ }
2503
+
2504
+ // ============================================================================
2505
+ // ERROR HANDLING
2506
+ // ============================================================================
2507
+
2508
+ process.on('uncaughtException', (error: any) => {
2509
+ console.error(color(`\n✗ Uncaught error: ${error.message}`, 'red'));
2510
+ if (process.env.DEBUG) {
2511
+ console.error(error.stack);
2512
+ }
2513
+ process.exit(1);
2514
+ });
2515
+
2516
+ process.on('unhandledRejection', (error: any) => {
2517
+ console.error(
2518
+ color(`\n✗ Unhandled promise rejection: ${error.message}`, 'red')
2519
+ );
2520
+ if (process.env.DEBUG) {
2521
+ console.error(error.stack);
2522
+ }
2523
+ process.exit(1);
2524
+ });
2525
+
2526
+ // Run main function
2527
+ if (require.main === module) {
2528
+ main().catch((error: any) => {
2529
+ console.error(color(`\n✗ Fatal error: ${error.message}`, 'red'));
2530
+ process.exit(1);
2531
+ });
2532
+ }
2533
+
2534
+ module.exports = { main };