balena-cli/lib/ui/patterns/patterns.coffee

45 lines
981 B
CoffeeScript
Raw Normal View History

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')
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
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) ->
# TODO: Allow quieting this progress bar
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