forked from mafintosh/csv-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbin.js
executable file
·51 lines (43 loc) · 1.37 KB
/
bin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env node
var minimist = require('minimist')
var ldjson = require('ldjson-stream')
var fs = require('fs')
var csv = require('./')
var argv = minimist(process.argv, {
alias: {
h: 'headers',
v: 'version',
o: 'output',
s: 'separator'
},
default: {
s: ','
},
boolean: ['version', 'help']
})
var headers = argv.headers && argv.headers.toString().split(argv.separator)
var filename = argv._[2]
if (argv.version) {
console.log(require('./package').version)
process.exit(0)
}
if (argv.help || (process.stdin.isTTY && !filename)) {
console.error(
'Usage: csv-parser [filename?] [options]\n\n'+
' --headers,-h Explicitly specify csv headers as a comma separated list\n'+
' --output,-o Set output file. Defaults to stdout\n'+
' --separator,-s Set the separator character ("," by default)\n'+
' --version,-v Print out the installed version\n'+
' --help Show this help\n'
)
process.exit(1)
}
var input
var output = (argv.output && argv.output !== '-') ? fs.createWriteStream(argv.output) : process.stdout
if (filename === '-' || !filename) input = process.stdin
else if (fs.existsSync(filename)) input = fs.createReadStream(filename)
else {
console.error('File: %s does not exist', filename)
process.exit(2)
}
input.pipe(csv({headers:headers, separator:argv.separator})).pipe(ldjson.serialize()).pipe(output)