This repository has been archived by the owner on Dec 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
205 lines (183 loc) · 5.25 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
'use strict'
var pump = require('pump')
var fs = require('fs')
var join = require('path').join
var relative = require('path').relative
var basename = require('path').basename
var EventEmitter = require('events').EventEmitter
var chokidar = require('chokidar')
var series = require('run-series')
var match = require('anymatch')
var through = require('through2')
var isDuplicate = require('hyperdrive-duplicate')
var noop = function () {}
module.exports = function (archive, target, opts, done) {
if (typeof opts === 'function') {
done = opts
opts = {}
}
opts = opts || {}
var watch = opts.watch || opts.live
var overwrite = opts.overwrite !== false
var dryRun = opts.dryRun === true
var compareFileContent = opts.compareFileContent === true
function emitError (err) {
if (err) status.emit('error', err)
}
done = done || emitError
var basePath = (typeof opts.basePath === 'string') ? opts.basePath : ''
var watcher
var isWatching = false
if (watch && archive.live) {
watcher = chokidar.watch([target], {
persistent: true,
ignored: opts.ignore
})
watcher.once('ready', function () {
watcher.on('add', function (file, stat) {
status.emit('file watch event', {path: file, mode: 'created'})
consume(file, stat)
})
watcher.on('change', function (file, stat) {
status.emit('file watch event', {path: file, mode: 'updated'})
consume(file, stat)
})
watcher.on('unlink', noop) // TODO
})
}
var status = new EventEmitter()
status.close = function () { watcher && watcher.close() }
status.fileCount = 0
status.totalSize = 0
status.bytesImported = 0
function consume (file, stat, cb) {
cb = cb || emitError
if (opts.ignore && match(opts.ignore, file)) return cb()
if (stat) {
onstat(stat)
} else {
fs.stat(file, function (err, stat) {
if (err) return cb(err)
onstat(stat)
})
}
function onstat (stat) {
if (stat.isDirectory()) {
consumeDir(file, stat, cb)
} else {
consumeFile(file, stat, cb)
}
}
}
function consumeFile (file, stat, cb) {
cb = cb || emitError
var hyperPath = file === target
? joinHyperPath(basePath, basename(file))
: joinHyperPath(basePath, relative(target, file))
archive.stat(hyperPath, function (err, st) {
if (overwrite) return add(st)
if (err && !st) return add()
status.emit('file skipped', { path: file })
cb()
})
function add (entry) {
// update the stats according to whether this is the initial import
if (!isWatching) {
// initial import, just add
status.fileCount++
status.totalSize += stat.size
} else {
if (entry) {
// watch update to existing file, remove old and add new
status.totalSize -= entry.size
status.totalSize += stat.size
} else {
// watch addition, just add
status.fileCount++
status.totalSize += stat.size
}
}
if (!entry) {
next('created')
} else if (entry.size !== stat.size || entry.mtime !== stat.mtime.getTime()) {
if (compareFileContent) {
isDuplicate(archive, file, hyperPath, function (err, duplicate) {
if (!err && duplicate) return skip()
next('updated')
})
} else {
next('updated')
}
} else {
skip()
}
function skip () {
status.bytesImported += stat.size
status.emit('file skipped', { path: file })
cb()
}
}
function next (mode) {
if (dryRun) {
return pumpDone()
}
var rs = fs.createReadStream(file)
var ws = archive.createWriteStream(hyperPath, {indexing: opts.indexing})
var increment = through(function (chunk, enc, cb) {
status.bytesImported += chunk.length
cb(null, chunk)
})
pump(rs, increment, ws, pumpDone)
function pumpDone (err) {
if (err) return cb(err)
status.emit('file imported', {
path: file,
mode: mode
})
cb()
}
}
}
function consumeDir (file, stat, cb) {
cb = cb || emitError
var hyperPath = joinHyperPath(basePath, relative(target, file))
function next () {
fs.readdir(file, function (err, _files) {
if (err) return cb(err)
series(_files.map(function (_file) {
return function (cb2) {
consume(join(file, _file), null, cb2)
}
}), cb)
})
}
if (dryRun) {
next()
} else {
archive.stat(hyperPath, function (err, st) {
if (!err && st) next()
else archive.mkdir(hyperPath, next)
})
}
}
consume(target, null, function (err) {
isWatching = true
done(err)
})
return status
}
function normalizeEntryPath (path) {
if (typeof path === 'string' && path.charAt(0) === '/') {
return path.slice(1)
}
return path
}
function joinHyperPath (base, path) {
path = join(base, path)
if (path === '.') {
// '.' is returned when base is '' and path is '': aka, the root directory
// in hyperdrive, root should be '' or '/', so we replace it with this special case
return ''
}
return normalizeEntryPath(path)
}