2014-12-02 15:08:22 +00:00
|
|
|
_ = require('lodash')
|
|
|
|
async = require('async')
|
|
|
|
path = require('path')
|
|
|
|
mkdirp = require('mkdirp')
|
|
|
|
url = require('url')
|
|
|
|
resin = require('../resin')
|
|
|
|
connection = require('../connection/connection')
|
|
|
|
|
|
|
|
exports.download = (id) ->
|
2014-12-02 15:30:17 +00:00
|
|
|
params =
|
|
|
|
network: resin.cli.getArgument('network')
|
|
|
|
wifiSsid: resin.cli.getArgument('wifiSsid')
|
|
|
|
wifiKey: resin.cli.getArgument('wifiKey')
|
2014-12-02 15:08:22 +00:00
|
|
|
|
2014-12-02 15:30:17 +00:00
|
|
|
fileName = resin.os.generateCacheName(id, params)
|
2014-12-02 15:53:34 +00:00
|
|
|
outputFile = resin.cli.getArgument('output') or path.join(resin.config.osDirectory, fileName)
|
2014-12-02 15:08:22 +00:00
|
|
|
|
|
|
|
async.waterfall [
|
|
|
|
|
|
|
|
(callback) ->
|
|
|
|
|
|
|
|
# We need to ensure this directory exists
|
2014-12-02 15:53:34 +00:00
|
|
|
mkdirp path.dirname(outputFile), (error) ->
|
2014-12-02 15:08:22 +00:00
|
|
|
return callback(error)
|
|
|
|
|
|
|
|
(callback) ->
|
|
|
|
connection.parseConnectionParameters(params, callback)
|
|
|
|
|
|
|
|
(parameters, callback) ->
|
|
|
|
parameters.appId = id
|
|
|
|
|
|
|
|
query = url.format(query: parameters)
|
|
|
|
downloadUrl = url.resolve(resin.config.urls.download, query)
|
|
|
|
|
|
|
|
return callback(null, downloadUrl)
|
|
|
|
|
|
|
|
(downloadUrl, callback) ->
|
|
|
|
resin.ui.patterns.downloadFile(downloadUrl, outputFile, callback)
|
|
|
|
|
|
|
|
], (error) ->
|
|
|
|
resin.errors.handle(error) if error?
|
|
|
|
console.log "\n\nFinished downloading #{outputFile}"
|