forked from bionode/bionode-ncbi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.js
executable file
·78 lines (64 loc) · 1.68 KB
/
cli.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env node
var minimist = require('minimist')
var JSONStream = require('JSONStream')
var split = require('split2')
var ncbi = require('./')
var minimistOptions = {
alias: {
limit: 'l',
throughput: 't',
help: 'h'
}
}
var jsonPattern = /\{(.+?)\}/,
args = process.argv.slice(2).join(' '),
options = {},
match = args.match(jsonPattern)
if (match) {
var jsonLine = match[0].replace(/\'/g, '\"'),
options = JSON.parse(jsonLine),
args = args.replace(match[0], 'obj')
}
var argv = minimist(args.split(' '), minimistOptions)
if (argv.help || argv._.length === 0) {
console.log('Please check the documentation at http://doc.bionode.io')
process.exit()
}
var command = argv._[0]
var arg1 = argv._[1]
var lastArg = argv._[argv._.length - 1]
var wantsStdin = false
if (lastArg === '-') {
wantsStdin = true
argv._.pop()
}
if (command === 'link') {
var arg2 = argv._[2]
var arg3 = argv._[3]
} else {
var arg2 = argv._.slice(2).join(' ')
var arg3 = null
}
if (Object.keys(argv).length > 1) {
options.limit = argv.limit
options.throughput = argv.throughput
if (arg1 !== 'obj') {
options.db = arg1
options.term = arg2
}
}
var ncbiStream = Object.keys(options).length ? ncbi[command](options) : ncbi[command](arg1, arg2, arg3)
ncbiStream.pipe(JSONStream.stringify(false)).pipe(process.stdout)
if (wantsStdin) {
process.stdin.setEncoding('utf8')
process.stdin.pipe(split()).on('data', function (data) {
if (data.trim() === '') { return }
ncbiStream.write(data.trim())
})
process.stdin.on('end', function () {
ncbiStream.end()
})
}
process.stdout.on('error', function (err) {
if (err.code === 'EPIPE') { process.exit(0) }
})