2017-07-27 11:07:05 +00:00
// Generated by CoffeeScript 1.12.7
2017-03-08 23:30:24 +00:00
/ *
Copyright 2017 Resin . io
Licensed under the Apache License , Version 2.0 ( the 'License' ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an 'AS IS' BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
2017-03-22 09:46:06 +00:00
module . exports = {
signature : 'local flash <image>' ,
description : 'Flash an image to a drive' ,
help : 'Use this command to flash a resinOS image to a drive.\n\nExamples:\n\n $ resin local flash path/to/resinos.img\n $ resin local flash path/to/resinos.img --drive /dev/disk2\n $ resin local flash path/to/resinos.img --drive /dev/disk2 --yes' ,
options : [
{
signature : 'yes' ,
boolean : true ,
description : 'confirm non-interactively' ,
alias : 'y'
} , {
signature : 'drive' ,
parameter : 'drive' ,
description : 'drive' ,
alias : 'd'
}
] ,
root : true ,
action : function ( params , options , done ) {
2017-03-27 09:14:55 +00:00
var Promise , _ , chalk , driveListAsync , form , fs , imageWrite , os , umountAsync , visuals ;
2017-03-22 09:46:06 +00:00
_ = require ( 'lodash' ) ;
os = require ( 'os' ) ;
Promise = require ( 'bluebird' ) ;
2017-03-27 09:14:55 +00:00
umountAsync = Promise . promisify ( require ( 'umount' ) . umount ) ;
2017-03-22 09:46:06 +00:00
fs = Promise . promisifyAll ( require ( 'fs' ) ) ;
2017-03-27 09:14:55 +00:00
driveListAsync = Promise . promisify ( require ( 'drivelist' ) . list ) ;
2017-03-22 09:46:06 +00:00
chalk = require ( 'chalk' ) ;
visuals = require ( 'resin-cli-visuals' ) ;
form = require ( 'resin-cli-form' ) ;
imageWrite = require ( 'etcher-image-write' ) ;
return form . run ( [
2017-03-08 23:30:24 +00:00
{
2017-03-22 09:46:06 +00:00
message : 'Select drive' ,
type : 'drive' ,
name : 'drive'
2017-03-08 23:30:24 +00:00
} , {
2017-03-22 09:46:06 +00:00
message : 'This will erase the selected drive. Are you sure?' ,
type : 'confirm' ,
name : 'yes' ,
"default" : false
}
] , {
override : {
drive : options . drive ,
yes : options . yes || void 0
2017-03-08 23:30:24 +00:00
}
2017-03-22 09:46:06 +00:00
} ) . then ( function ( answers ) {
if ( answers . yes !== true ) {
console . log ( chalk . red . bold ( 'Aborted image flash' ) ) ;
process . exit ( 0 ) ;
}
2017-03-27 09:14:55 +00:00
return driveListAsync ( ) . then ( function ( drives ) {
2017-03-22 09:46:06 +00:00
var selectedDrive ;
selectedDrive = _ . find ( drives , {
device : answers . drive
} ) ;
if ( selectedDrive == null ) {
throw new Error ( "Drive not found: " + answers . drive ) ;
}
return selectedDrive ;
2017-03-08 23:30:24 +00:00
} ) ;
2017-03-22 09:46:06 +00:00
} ) . then ( function ( selectedDrive ) {
var progressBars ;
progressBars = {
write : new visuals . Progress ( 'Flashing' ) ,
check : new visuals . Progress ( 'Validating' )
} ;
2017-03-27 09:14:55 +00:00
return umountAsync ( selectedDrive . device ) . then ( function ( ) {
2017-03-22 09:46:06 +00:00
return Promise . props ( {
imageSize : fs . statAsync ( params . image ) . get ( 'size' ) ,
imageStream : Promise . resolve ( fs . createReadStream ( params . image ) ) ,
driveFileDescriptor : fs . openAsync ( selectedDrive . raw , 'rs+' )
} ) ;
} ) . then ( function ( results ) {
return imageWrite . write ( {
fd : results . driveFileDescriptor ,
device : selectedDrive . raw ,
size : selectedDrive . size
2017-03-08 23:30:24 +00:00
} , {
2017-03-22 09:46:06 +00:00
stream : results . imageStream ,
size : results . imageSize
} , {
check : true
2017-03-08 23:30:24 +00:00
} ) ;
2017-03-22 09:46:06 +00:00
} ) . then ( function ( writer ) {
return new Promise ( function ( resolve , reject ) {
writer . on ( 'progress' , function ( state ) {
return progressBars [ state . type ] . update ( state ) ;
2017-03-08 23:30:24 +00:00
} ) ;
2017-03-22 09:46:06 +00:00
writer . on ( 'error' , reject ) ;
return writer . on ( 'done' , resolve ) ;
2017-03-08 23:30:24 +00:00
} ) ;
2017-03-22 09:46:06 +00:00
} ) . then ( function ( ) {
2017-03-27 09:14:55 +00:00
var ejectAsync ;
2017-03-22 09:46:06 +00:00
if ( ( os . platform ( ) === 'win32' ) && ( selectedDrive . mountpoint != null ) ) {
2017-03-27 09:14:55 +00:00
ejectAsync = Promise . promisify ( require ( 'removedrive' ) . eject ) ;
return ejectAsync ( selectedDrive . mountpoint ) ;
2017-03-22 09:46:06 +00:00
}
2017-03-27 09:14:55 +00:00
return umountAsync ( selectedDrive . device ) ;
2017-03-22 09:46:06 +00:00
} ) ;
} ) . asCallback ( done ) ;
}
} ;