Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions commands/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,24 @@ var speedometer = require('speedometer')
var ui = require('../lib/ui')

module.exports = function (dat, args) {
var log = logger(args)
var lines = [[], []]
var log = logger(lines, {debug: args.debug, quiet: args.quiet})
var messages = lines[0] = []
var progress = lines[1] = []

var downloadTxt = 'Downloading '
var finished = false

dat.stats.rateUp = speedometer()
dat.stats.rateDown = speedometer()

log.status('Starting Dat...\n', 0)
log.status('Connecting...', 1)
progress.push('Starting Dat...\n')
progress.push('Connecting...')

dat.on('error', onerror)

dat.open(function () {
log.message('Downloading in ' + dat.dir + '\n')
messages.push('Downloading in ' + dat.dir + '\n')
dat.download(function (err) {
if (err) onerror(err)
})
Expand All @@ -32,7 +35,7 @@ module.exports = function (dat, args) {
})

dat.once('key', function (key) {
log.message(ui.keyMsg(key))
messages.push(ui.keyMsg(key))
if (args.quiet) console.log(ui.keyMsg(key))
})

Expand All @@ -50,7 +53,7 @@ module.exports = function (dat, args) {
finished = false
dat.stats.rateDown = speedometer()
updateStats()
log.status('', -1) // remove download finished message
progress[2] = '' // remove download finished message
})
dat.on('file-downloaded', updateStats)

Expand All @@ -59,16 +62,16 @@ module.exports = function (dat, args) {
dat.stats.rateDown = 0
updateStats()
if (args.exit) {
log.status('', 1)
progress[1] = '' // clear swarm info before exiting
process.exit(0)
}
log.status('\nDownload Finished. You may exit the process with Ctrl-C.', -1)
progress[2] = '\nDownload Finished. You may exit the process with Ctrl-C.'
})

dat.on('swarm-update', printSwarm)

function printSwarm () {
log.status(ui.swarmMsg(dat), 1)
progress[1] = ui.swarmMsg(dat)
}

function updateStats () {
Expand All @@ -79,7 +82,7 @@ module.exports = function (dat, args) {
}
msg += ' ' + downloadTxt + chalk.bold(stats.filesTotal) + ' items'
msg += chalk.dim(' (' + prettyBytes(stats.bytesTotal) + ')')
log.status(msg + '\n', 0)
progress[0] = msg + '\n'
}
}

Expand Down
22 changes: 13 additions & 9 deletions commands/share.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,26 @@ var speedometer = require('speedometer')
var ui = require('../lib/ui')

module.exports = function (dat, args) {
var log = logger(args)
var lines = [[], []]
var log = logger(lines, {debug: args.debug, quiet: args.quiet})
var messages = lines[0] = []
var progress = lines[1] = []

var addText = 'Adding '
var finalized = false

dat.stats.rateUp = speedometer()

log.status('Starting Dat...\n', 0)
if (args.snapshot) log.status('Creating Link...', 1)
else log.status('Connecting...', 1)
progress.push('Starting Dat...\n')
if (args.snapshot) progress.push('Creating Link...')
else progress.push('Connecting...')

dat.on('error', onerror)

dat.open(function () {
dat.archive.open(function () {
if (dat.archive.key && !dat.archive.owner) return download(dat, args)
log.message('Sharing ' + dat.dir + '\n')
messages.push('Sharing ' + dat.dir + '\n')
dat.share(function (err) {
if (err) onerror(err)
})
Expand All @@ -43,11 +47,11 @@ module.exports = function (dat, args) {
var msg = 'Calculating Size: '
msg += stats.filesTotal + ' items '
msg += chalk.dim('(' + prettyBytes(stats.bytesTotal) + ')')
log.status(msg + '\n', 0)
progress[0] = msg + '\n'
})

dat.once('key', function (key) {
log.message(ui.keyMsg(key))
messages.push(ui.keyMsg(key))
if (args.quiet) console.log(ui.keyMsg(key))
})

Expand All @@ -72,7 +76,7 @@ module.exports = function (dat, args) {
dat.on('swarm-update', printSwarm)

function printSwarm () {
log.status(ui.swarmMsg(dat), 1)
progress[1] = ui.swarmMsg(dat)
}

function updateStats () {
Expand All @@ -85,7 +89,7 @@ module.exports = function (dat, args) {
if (finalized) msg += chalk.bold(files) + ' '
msg += 'items'
msg += chalk.dim(' (' + prettyBytes(bytesTotal) + ')')
log.status(msg + '\n', 0)
progress[0] = msg + '\n'
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"pretty-bytes": "^3.0.0",
"pump": "^1.0.1",
"speedometer": "^1.0.0",
"status-logger": "^2.0.1"
"status-logger": "^3.0.0"
},
"bugs": {
"url": "https://github.com/datproject/dat/issues"
Expand Down
8 changes: 5 additions & 3 deletions tests/share.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ test('share with temp arg', function (t) {
st.end()
})

test('sharing existing directory begins sync', function (t) {
test('sharing downlaoded directory begins download', function (t) {
// cmd: dat <link> . then dat .
var tmpdir = newTestFolder()
var link = null
Expand All @@ -187,21 +187,23 @@ test('sharing existing directory begins sync', function (t) {
var contains = output.indexOf('Downloaded') > -1
if (!contains || !share) return false
downloader.kill()
share.kill()
spawnShare()
return true
}, 'download one started')
downloader.end()
share.end()
}

function spawnShare () {
var st = spawn(t, dat + ' ' + tmpdir)
st.stdout.match(function (output) {
var contains = output.indexOf('Connected to') > -1
var contains = output.indexOf('Downloading') > -1
if (!contains) return false
st.kill()
cleanDat()
return true
})
}, 'share becomes download')
st.end()
}
})
Expand Down