diff --git a/build/actions/os.js b/build/actions/os.js index 7060cd94..274cc711 100644 --- a/build/actions/os.js +++ b/build/actions/os.js @@ -1,10 +1,8 @@ (function() { - var _, async, commandOptions, diskio, elevate, fs, mkdirp, npm, os, packageJSON, path, progressStream, resin, updateActions, visuals; + var _, async, commandOptions, elevate, mkdirp, npm, os, packageJSON, path, resin, updateActions, visuals; _ = require('lodash-contrib'); - fs = require('fs'); - os = require('os'); async = require('async'); @@ -17,10 +15,6 @@ visuals = require('resin-cli-visuals'); - progressStream = require('progress-stream'); - - diskio = require('diskio'); - commandOptions = require('./command-options'); npm = require('../npm'); @@ -102,6 +96,8 @@ options: [commandOptions.yes], permission: 'user', action: function(params, options, done) { + var bundle; + bundle = require('../devices/raspberry-pi'); return async.waterfall([ function(callback) { return npm.isUpdated(packageJSON.name, packageJSON.version, callback); @@ -130,27 +126,13 @@ message = "This will completely erase " + params.device + ". Are you sure you want to continue?"; return visuals.patterns.confirm(options.yes, message, callback); }, function(confirmed, callback) { - var bar, error, imageFileSize, imageFileStream, progress; + var bar; if (!confirmed) { return done(); } - imageFileSize = fs.statSync(params.image).size; - if (imageFileSize === 0) { - error = new Error("Invalid OS image: " + params.image + ". The image is 0 bytes."); - return callback(error); - } - progress = progressStream({ - length: imageFileSize, - time: 500 - }); - if (!options.quiet) { - bar = new visuals.widgets.Progress('Writing Device OS'); - progress.on('progress', function(status) { - return bar.update(status); - }); - } - imageFileStream = fs.createReadStream(params.image).pipe(progress); - return diskio.writeStream(params.device, imageFileStream, callback); + bar = new visuals.widgets.Progress('Writing Device OS'); + params.progress = bar.update; + return bundle.write(params, callback); } ], function(error) { var resinWritePath; diff --git a/build/devices/raspberry-pi.js b/build/devices/raspberry-pi.js new file mode 100644 index 00000000..0a4eed7a --- /dev/null +++ b/build/devices/raspberry-pi.js @@ -0,0 +1,30 @@ +(function() { + var diskio, fs, progressStream; + + fs = require('fs'); + + progressStream = require('progress-stream'); + + diskio = require('diskio'); + + exports.name = 'Raspberry Pi'; + + exports.write = function(options, callback) { + var error, imageFileSize, imageFileStream, progress; + imageFileSize = fs.statSync(options.image).size; + if (imageFileSize === 0) { + error = new Error("Invalid OS image: " + options.image + ". The image is 0 bytes."); + return callback(error); + } + progress = progressStream({ + length: imageFileSize, + time: 500 + }); + if (!options.quiet) { + progress.on('progress', options.progress); + } + imageFileStream = fs.createReadStream(options.image).pipe(progress); + return diskio.writeStream(options.device, imageFileStream, callback); + }; + +}).call(this); diff --git a/lib/actions/os.coffee b/lib/actions/os.coffee index e6a4761e..a27a6927 100644 --- a/lib/actions/os.coffee +++ b/lib/actions/os.coffee @@ -1,13 +1,10 @@ _ = require('lodash-contrib') -fs = require('fs') os = require('os') async = require('async') path = require('path') mkdirp = require('mkdirp') resin = require('resin-sdk') visuals = require('resin-cli-visuals') -progressStream = require('progress-stream') -diskio = require('diskio') commandOptions = require('./command-options') npm = require('../npm') packageJSON = require('../../package.json') @@ -127,6 +124,9 @@ exports.install = options: [ commandOptions.yes ] permission: 'user' action: (params, options, done) -> + + bundle = require('../devices/raspberry-pi') + async.waterfall [ (callback) -> @@ -166,25 +166,9 @@ exports.install = (confirmed, callback) -> return done() if not confirmed - imageFileSize = fs.statSync(params.image).size - - if imageFileSize is 0 - error = new Error("Invalid OS image: #{params.image}. The image is 0 bytes.") - return callback(error) - - progress = progressStream - length: imageFileSize - time: 500 - - if not options.quiet - bar = new visuals.widgets.Progress('Writing Device OS') - - progress.on 'progress', (status) -> - bar.update(status) - - imageFileStream = fs.createReadStream(params.image).pipe(progress) - - diskio.writeStream(params.device, imageFileStream, callback) + bar = new visuals.widgets.Progress('Writing Device OS') + params.progress = bar.update + bundle.write(params, callback) ], (error) -> return done() if not error? diff --git a/lib/devices/raspberry-pi.coffee b/lib/devices/raspberry-pi.coffee new file mode 100644 index 00000000..7778be7a --- /dev/null +++ b/lib/devices/raspberry-pi.coffee @@ -0,0 +1,22 @@ +fs = require('fs') +progressStream = require('progress-stream') +diskio = require('diskio') + +exports.name = 'Raspberry Pi' + +exports.write = (options, callback) -> + imageFileSize = fs.statSync(options.image).size + + if imageFileSize is 0 + error = new Error("Invalid OS image: #{options.image}. The image is 0 bytes.") + return callback(error) + + progress = progressStream + length: imageFileSize + time: 500 + + if not options.quiet + progress.on('progress', options.progress) + + imageFileStream = fs.createReadStream(options.image).pipe(progress) + diskio.writeStream(options.device, imageFileStream, callback)