-
Notifications
You must be signed in to change notification settings - Fork 5
/
cli.js
executable file
·180 lines (150 loc) · 4.14 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env node
var minimist = require('minimist')
var pretty = require('prettier-bytes')
var speed = require('speedometer')
var progress = require('progress-string')
var cliTruncate = require('cli-truncate')
var neatLog = require('neat-log')
var output = require('neat-log/output')
var debug = require('debug')('dat-next')
var view = require('./ui')
var Dat = require('./')
process.title = 'dat-next'
var argv = minimist(process.argv.slice(2), {
alias: {temp: 't', help: 'h', watch: 'w', sleep: 's'},
boolean: ['watch'],
default: {
'watch' : true
}
})
var src = argv._[0] || process.cwd()
var dest = argv._[1]
var logspeed = argv.logspeed || 400
var quiet = debug.enabled || !!process.env.DEBUG
if (!argv._.length || argv.help) return usage()
var neat = neatLog([view.main, view.progress], {
logspeed: logspeed,
quiet: quiet
})
neat.use(runDat)
neat.use(trackNetwork)
neat.use(trackProgress)
function runDat (state, bus) {
state.opts = argv
state.title = 'Starting Dat program...'
bus.emit('render')
Dat(src, dest, argv, function (err, dat) {
if (err) {
bus.clear()
console.error('ERROR:', err)
process.exit(1)
}
state.archive = dat.archive
state.network = dat.network
state.importer = dat.importer
state.stats = dat.stats
state.writable = dat.writable
if (dat.archive.content) {
bus.emit('archive:content')
} else {
dat.archive.once('content', function () {
bus.emit('archive:content')
})
}
dat.archive.metadata.on('append', function () {
bus.emit('render')
})
if (dat.writable) state.title = `dat://${dat.key.toString('hex')}`
else state.title = 'Dat!'
bus.emit('archive')
bus.emit('render')
})
}
function trackProgress (state, bus) {
bus.once('archive:content', function () {
if (state.archive.metadata.writable) trackImport()
else trackDownload()
})
function trackDownload () {
state.downloading = true
state.modified = false
state.archive.content.on('clear', function () {
state.modified = true
})
state.archive.content.on('download', function (index, data) {
state.modified = true
})
state.archive.on('sync', function () {
state.nsync = true
if (state.modified && !argv.live) {
state.downloadExit = true
bus.render()
process.exit()
}
bus.emit('render')
})
state.archive.on('update', function () {
state.nsync = false
bus.emit('render')
bus.emit('archive:update')
})
}
function trackImport () {
state.importing = true
var progress = state.importer
var counting = setInterval(function () {
// Update file count while we are going (for big dirs)
bus.emit('render')
}, logspeed)
progress.once('count', function (count) {
clearInterval(counting)
state.count = count
bus.emit('render')
})
progress.on('put', function (src, dst) {
if (src.stat.isDirectory()) return
state.fileImport = {
src: src,
dst: dst,
progress: 0,
type: 'put'
}
bus.emit('render')
})
progress.on('put-data', function (chunk, src, dst) {
state.fileImport.progress += chunk.length
bus.emit('render')
})
progress.on('put-end', function (src, dst) {
// state.fileImport = null
bus.emit('render')
})
progress.on('end', function (src, dst) {
// state.fileImport = null
bus.emit('render')
})
}
}
function trackNetwork (state, bus) {
bus.on('archive:content', function () {
var network = state.network
network.on('connection', function (peer) {
bus.emit('render')
peer.on('close', function () {
bus.emit('render')
})
})
var speed = state.stats.network
setInterval(function () {
state.uploadSpeed = speed.uploadSpeed
state.downloadSpeed = speed.downloadSpeed
bus.emit('render')
}, logspeed)
})
}
function usage () {
console.error('dat-next!')
console.error(' dat-next <dir> SHARE directory')
console.error(' dat-next <key> <dir> DOWNLOAD key to dir (directory required)')
process.exit(0)
}