2014-11-21 17:23:29 +00:00
|
|
|
async = require('async')
|
2014-12-02 15:08:22 +00:00
|
|
|
fs = require('fs')
|
2014-11-26 17:27:57 +00:00
|
|
|
widgets = require('../widgets/widgets')
|
2014-12-05 16:00:15 +00:00
|
|
|
resin = require('../../resin')
|
2014-12-02 21:40:13 +00:00
|
|
|
ProgressBar = require('progress')
|
2014-11-21 17:23:29 +00:00
|
|
|
|
|
|
|
exports.remove = (name, confirmAttribute, deleteFunction, outerCallback) ->
|
|
|
|
async.waterfall([
|
|
|
|
|
|
|
|
(callback) ->
|
|
|
|
if confirmAttribute
|
|
|
|
return callback(null, true)
|
|
|
|
|
2014-11-26 17:27:57 +00:00
|
|
|
widgets.confirmRemoval(name, callback)
|
2014-11-21 17:23:29 +00:00
|
|
|
|
|
|
|
(confirmed, callback) ->
|
|
|
|
return callback() if not confirmed
|
|
|
|
deleteFunction(callback)
|
|
|
|
|
|
|
|
], outerCallback)
|
2014-12-02 15:08:22 +00:00
|
|
|
|
|
|
|
exports.downloadFile = (url, dest, callback) ->
|
|
|
|
bar = null
|
2014-12-02 21:40:13 +00:00
|
|
|
received = 0
|
|
|
|
|
2014-12-05 16:00:15 +00:00
|
|
|
resin.server.request
|
2014-12-02 15:08:22 +00:00
|
|
|
method: 'GET'
|
|
|
|
url: url
|
|
|
|
pipe: fs.createWriteStream(dest)
|
|
|
|
, (error) ->
|
|
|
|
return callback(error)
|
|
|
|
, (state) ->
|
2014-12-02 21:40:13 +00:00
|
|
|
|
2014-12-03 13:09:36 +00:00
|
|
|
# TODO: Allow quieting this progress bar
|
2014-12-02 21:40:13 +00:00
|
|
|
bar ?= new ProgressBar 'Downloading device OS [:bar] :percent :etas',
|
|
|
|
complete: '='
|
|
|
|
incomplete: ' '
|
|
|
|
width: 40
|
|
|
|
total: state.total
|
|
|
|
|
|
|
|
return if bar.complete or not state?
|
|
|
|
|
|
|
|
bar.tick(state.received - received)
|
|
|
|
received = state.received
|