-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·178 lines (158 loc) · 4.52 KB
/
cli.js
File metadata and controls
executable file
·178 lines (158 loc) · 4.52 KB
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
#!/usr/bin/env node
var path = require('path')
var fs = require('fs')
var args = require('minimist')(process.argv.slice(2))
var neatlog = require('neat-log')
var Dat = require('dat-node')
var stringKey = require('dat-encoding').toStr
var xtend = require('xtend')
var glob = require('glob')
if (args.help) {
var help = `Usage: dat-share-all [options] [--dir=<dir>]
Share all dats that are located below a directory.
Options:
--import Import file changes in writable dats (default: false).
--watch Watch for file changes in writable dats and import (default: false).
--watchdir Watch for new dats being added and share them (default: false).
--dir=<dir> Set the dir from which to look for dats (default: current dir).
--port Set port (default: 3282)
--neat Simple list UI (WIP, not working correctly for many dats).
`
console.log(help)
process.exit(0)
}
var defaultOpts = {
showKey: true,
createIfMissing: false,
exit: false,
port: args.port || 3282,
utp: true,
quiet: false,
watch: !!args.watch,
import: !!args.import
}
var ui, neat
if (args.neat) {
ui = require('./ui-neat')
neat = neatlog(ui, {})
}
else {
ui = require('./ui-blessed')
neat = neatlog(ui, {})
}
neat.use(init)
neat.use(archive)
neat.use(wait)
if (args.watchdir) {
var chokidar = require('chokidar')
setInterval(function() {
neat.use(watchdir)
}, 5000)
}
neat.render()
function init(state, bus) {
state.dats = {}
state.errors = []
state.debug = []
state.exit = false
state.paths = []
state.basepath = path.resolve(args.dir || process.cwd())
var globOpts = {absolute: true, cwd: state.basepath, strict: false, silent: true}
try {
glob("**/.dat", globOpts, function(err, paths) {
if (err || !paths) return
paths.forEach(function(datpath) {
var parent = path.dirname(datpath)
if (state.paths.indexOf(datpath) === -1 && fs.lstatSync(datpath).isDirectory()) {
state.paths.push(datpath)
bus.emit('datpath', parent)
}
})
})
}
catch(e) {
// Todo: Error handling.
}
bus.on('datpath', function(datpath) {
var name = path.basename(datpath)
var opts = xtend(defaultOpts, {dir: datpath})
Dat(opts.dir, opts, function (err, dat) {
if (err && err.name === 'MissingError') {
state.errors.push({name: name, message: 'Not a dat.'})
}
else if (err) {
state.errors.push({name: name, message: err.message, err: err})
}
else {
dat.archive.on('update', function () {
bus.render()
})
var stats = dat.trackStats()
stats.on('update', function () {
bus.render()
})
// import?
var progress = false
var doImport = false
if (opts.import && dat.writable) {
doImport = true
progress = dat.importFiles(opts, function (err) {
})
}
var datState = {name: name, path: datpath, dat: dat, stats: stats, opts: opts, key: stringKey(dat.key), import: doImport, progress: progress}
state.dats[datState.key] = datState
bus.emit('dat', datState)
}
bus.render()
})
})
}
function watchdir(state, bus) {
var watcher = chokidar.watch(state.basepath + '/**/.dat')
watcher.on('addDir', function(datpath) {
if (state.paths.indexOf(datpath) === -1 && fs.lstatSync(datpath).isDirectory()) {
state.debug.push('Add path: ' + datpath)
state.paths.push(datpath)
bus.emit('datpath', path.dirname(datpath))
}
})
}
function wait (state, bus) {
// Ensure no exit and rerender every second.
setInterval(function () {
if (!state.exit) {
bus.render()
}
}, 1000)
}
function archive (state, bus) {
function setTitle(key) {
fs.readFile(path.join(state.dats[key].path, 'dat.json'), function (err, data) {
if (err) state.dats[key].title = ''
try {
var datJson = JSON.parse(data)
if (datJson.title) {
state.dats[key].title = datJson.title
}
}
catch (e) {}
})
}
bus.on('dat', function (datState) {
var dat = datState.dat
var key = datState.key
setTitle(key)
var network = dat.joinNetwork(datState.opts, function () {
})
state.dats[key].sources = state.dats[key].sources || {}
bus.render()
network.on('connection', function (conn, info) {
// todo: connection details.
})
dat.archive.on('update', function() {
state.dats[key].updated = true
setTitle(key)
bus.render()
})
})
}