-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
executable file
·101 lines (89 loc) · 2.66 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env node
var hypercore = require('hypercore')
var swarm = require('hyperdrive-archive-swarm')
var level = require('level')
var minimist = require('minimist')
var mkdirp = require('mkdirp')
var path = require('path')
var fs = require('fs')
var home = process.env.HOME || process.env.USERPROFILE
var usage = fs.readFileSync(path.join(__dirname, 'usage.txt'), 'utf8')
var argv = minimist(process.argv.slice(2), {
boolean: [ 'help', 'live' ],
default: {live: true},
alias: { help: 'h' }
})
var cmd = argv._[0]
var dir = argv._[1] && path.join(home, '.hypername', argv._[1])
var key = argv._[2]
var value = argv._[3]
if (argv.help || !dir) {
console.log(usage)
process.exit(argv.help ? 0 : 1)
}
mkdirp.sync(dir)
var core = hypercore(level(dir))
core._db.get('_key', {valueEncoding: 'binary'}, function (_, oldKey) {
var feed = null
if (cmd === 'init') {
feed = core.createFeed(oldKey || key)
feed.open(function () {
core._db.put('_key', feed.key, function (err) {
if (err) throw err
console.log(feed.key.toString('hex'))
})
})
return
}
if (!oldKey) throw new Error('No key found. Run `hypername init` first')
feed = core.createFeed(oldKey)
if (cmd === 'sync') {
feed.open(function () {
var blocks = feed.blocks
swarm(feed)
feed.on('download-finished', function () {
console.log('Pulled ' + (feed.blocks - blocks) + ' change(s)')
blocks = feed.blocks
if (argv.live === false) process.exit(0)
})
})
} else if (cmd === 'list' || cmd === 'ls') {
parse(feed, {}, function (err, map) {
if (err) throw err
process.stdout.write('{\n')
Object.keys(map).forEach(function (key, i, keys) {
process.stdout.write(' "' + key + '": "' + map[key] + '"')
if (i !== (keys.length - 1)) process.stdout.write(',\n')
})
process.stdout.write('\n}\n')
})
} else if (cmd === 'get') {
parse(feed, {}, function (err, view) {
if (err) throw err
if (view[key]) console.log(view[key])
else process.exit(1)
})
} else if (cmd === 'set') {
if (!key) throw new Error('key is required')
feed.append(JSON.stringify({key: key, value: value || ''}), function (err) {
if (err) throw err
})
} else {
throw new Error('Usage: hypername <cmd> <topic> <options...>')
}
})
function parse (feed, opts, cb) {
var view = {}
feed.createReadStream(opts)
.on('data', function (data) {
data = JSON.parse(data)
if (data.value) view[data.key] = data.value
else delete view[data.key]
})
.on('end', function () {
cb(null, view)
})
.on('error', function (err) {
cb(err)
})
}