-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
79 lines (67 loc) · 1.86 KB
/
index.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
var minimist = require('minimist')
var Stdout = require('pull-stdio').stdout
var Stringify = require('pull-json-doubleline').stringify
var pull = require('pull-stream/pull')
var path = require('path')
var methods = {
get: 'async',
append: 'async',
stream: 'source'
}
function isEmpty (opts) {
for(var k in opts) return false
return true
}
//flumedb, array, opts
module.exports = function (db, command, opts, name) {
if(!command) {
opts = minimist(process.argv.slice(2))
var _ = opts._
command = _.length ? _.shift().split('.') : []
delete opts._
if(isEmpty(opts)) opts = _.shift()
}
//let database load...
if(command.length) {
var method = command.pop()
var obj = db
for(var i in command)
obj = obj[command[i]]
var type = (obj === db ? methods : obj.methods)[method]
if(type === 'source') {
pull(
obj[method](opts),
Stringify(),
Stdout()
)
}
else if(type === 'async')
obj[method](opts, function (err, data) {
if(err) throw err
console.log(JSON.stringify(data, null, 2))
db.close(function () {})
})
else if(type === 'sync') {
console.log(JSON.stringify(obj[method](opts)), null, 2)
db.close(function () {})
} else
throw new Error('expected type=source, async, or sync but got:'+type)
}
else {
name = name || ('node ' + path.relative(process.cwd(), process.argv[1]))
console.log('usage: '+(name)+' COMMAND {options}')
console.log('where COMMAND is one of:')
var lines = []
for(var key in db) {
var type = methods[key]
if(type) console.log(key, '# '+type)
if(db[key].methods) {
for(var _key in db[key]) {
var type = db[key].methods[_key]
if(type) console.log(key+'.'+_key, '# '+type)
}
}
}
db.close(function () {})
}
}