@erdii/csv2jsonstream
Advanced tools
Comparing version 0.0.3 to 0.1.0
@@ -8,14 +8,45 @@ #!/usr/bin/env node | ||
inputPath: process.argv[2], | ||
notFirstLine: false, | ||
inputStream: null, | ||
get hasInputPath() { | ||
return !!this.inputPath; | ||
}, | ||
// stats | ||
outLines: 0, | ||
get notFirstLine() { | ||
return this.outLines > 0; | ||
}, | ||
}; | ||
if (!path.isAbsolute(state.inputPath)) { | ||
state.inputPath = path.resolve(process.cwd(), state.inputPath); | ||
if (state.hasInputPath) { | ||
// normalize input path | ||
if (!path.isAbsolute(state.inputPath)) { | ||
state.inputPath = path.resolve(process.cwd(), state.inputPath); | ||
} | ||
state.inputStream = fs.createReadStream(state.inputPath, { | ||
encoding: "utf8" | ||
}); | ||
} else { | ||
process.stdin.setEncoding("utf8"); | ||
state.inputStream = process.stdin; | ||
} | ||
state.inputFileStream = fs.createReadStream(state.inputPath); | ||
// open output array | ||
console.log("["); | ||
Papa.parse(state.inputFileStream, { | ||
// output a "]" before exiting | ||
function exitHandler() { | ||
console.log("]"); | ||
console.error(`Processed Lines: ${state.outLines}`); | ||
process.exit(); | ||
} | ||
process.on("SIGINT", function() { | ||
console.error("Exiting because of SIGKILL..."); | ||
exitHandler(); | ||
}); | ||
// configure and start the csv parser | ||
Papa.parse(state.inputStream, { | ||
header: true, | ||
@@ -28,9 +59,5 @@ dynamicTyping: true, | ||
if (!state.notFirstLine) { | ||
state.notFirstLine = true; | ||
} | ||
state.outLines++; | ||
}, | ||
complete: () => { | ||
console.log("]"); | ||
} | ||
complete: exitHandler, | ||
}); |
{ | ||
"name": "@erdii/csv2jsonstream", | ||
"version": "0.0.3", | ||
"version": "0.1.0", | ||
"description": "the cheapest csv to json-array converter EVER!", | ||
@@ -5,0 +5,0 @@ "main": "csv2jsonstream.js", |
@@ -9,4 +9,9 @@ # csv2jsonstream | ||
### installation | ||
`npm i -g @erdii/csv2jsonstream` | ||
### usage | ||
`csv2jsonstream input.csv > output.json` | ||
* Read csv from file: `csv2jsonstream input.csv > output.json` | ||
* Read csv from stdin: `cat input.csv | csv2jsonstream > output.json` |
3750
52
17