2016-01-11 19:58:35 +00:00
/ *
Copyright 2016 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 .
* /
2015-08-20 19:54:42 +00:00
( function ( ) {
2015-10-21 17:28:51 +00:00
var Promise , _ , chalk , form , resin , validation , visuals ;
2015-08-20 19:54:42 +00:00
_ = require ( 'lodash' ) ;
Promise = require ( 'bluebird' ) ;
form = require ( 'resin-cli-form' ) ;
visuals = require ( 'resin-cli-visuals' ) ;
resin = require ( 'resin-sdk' ) ;
2015-10-06 22:51:17 +00:00
chalk = require ( 'chalk' ) ;
2015-10-21 17:28:51 +00:00
validation = require ( './validation' ) ;
2015-08-20 19:54:42 +00:00
exports . selectDeviceType = function ( ) {
return resin . models . device . getSupportedDeviceTypes ( ) . then ( function ( deviceTypes ) {
return form . ask ( {
message : 'Device Type' ,
type : 'list' ,
choices : deviceTypes
} ) ;
} ) ;
} ;
exports . confirm = function ( yesOption , message ) {
return Promise [ "try" ] ( function ( ) {
if ( yesOption ) {
return true ;
}
return form . ask ( {
message : message ,
type : 'confirm' ,
"default" : false
} ) ;
} ) . then ( function ( confirmed ) {
if ( ! confirmed ) {
throw new Error ( 'Aborted' ) ;
}
} ) ;
} ;
2015-11-11 19:00:02 +00:00
exports . selectApplication = function ( filter ) {
2015-09-29 15:10:33 +00:00
return resin . models . application . hasAny ( ) . then ( function ( hasAnyApplications ) {
if ( ! hasAnyApplications ) {
throw new Error ( 'You don\'t have any applications' ) ;
}
2015-11-11 19:00:02 +00:00
return resin . models . application . getAll ( ) ;
} ) . filter ( filter || _ . constant ( true ) ) . then ( function ( applications ) {
return form . ask ( {
message : 'Select an application' ,
type : 'list' ,
2015-11-23 13:23:08 +00:00
choices : _ . map ( applications , function ( application ) {
return {
name : application . app _name + " (" + application . device _type + ")" ,
value : application . app _name
} ;
} )
2015-09-29 15:10:33 +00:00
} ) ;
} ) ;
} ;
exports . selectOrCreateApplication = function ( ) {
2015-08-20 19:54:42 +00:00
return resin . models . application . hasAny ( ) . then ( function ( hasAnyApplications ) {
if ( ! hasAnyApplications ) {
return ;
}
return resin . models . application . getAll ( ) . then ( function ( applications ) {
2015-11-23 13:23:08 +00:00
applications = _ . map ( applications , function ( application ) {
return {
name : application . app _name + " (" + application . device _type + ")" ,
value : application . app _name
} ;
} ) ;
2015-08-20 19:54:42 +00:00
applications . unshift ( {
name : 'Create a new application' ,
value : null
} ) ;
return form . ask ( {
message : 'Select an application' ,
type : 'list' ,
choices : applications
} ) ;
} ) ;
} ) . then ( function ( application ) {
if ( application != null ) {
return application ;
}
return form . ask ( {
message : 'Choose a Name for your new application' ,
2015-10-19 14:56:02 +00:00
type : 'input' ,
2015-10-21 13:37:25 +00:00
validate : validation . validateApplicationName
2015-08-20 19:54:42 +00:00
} ) ;
} ) ;
} ;
exports . awaitDevice = function ( uuid ) {
return resin . models . device . getName ( uuid ) . then ( function ( deviceName ) {
2015-10-07 15:38:59 +00:00
var poll , spinner ;
spinner = new visuals . Spinner ( "Waiting for " + deviceName + " to come online" ) ;
poll = function ( ) {
return resin . models . device . isOnline ( uuid ) . then ( function ( isOnline ) {
if ( isOnline ) {
spinner . stop ( ) ;
console . info ( "Device became online: " + deviceName ) ;
} else {
spinner . start ( ) ;
return Promise . delay ( 3000 ) . then ( poll ) ;
}
} ) ;
} ;
2015-08-20 19:54:42 +00:00
console . info ( "Waiting for " + deviceName + " to connect to resin..." ) ;
return poll ( ) [ "return" ] ( uuid ) ;
} ) ;
} ;
2015-10-06 22:51:17 +00:00
exports . printErrorMessage = function ( message ) {
2016-01-12 12:31:40 +00:00
console . error ( chalk . red ( message ) ) ;
return console . error ( chalk . red ( '\nIf you need help, don\'t hesitate in contacting us at:\n\n GitHub: https://github.com/resin-io/resin-cli/issues/new\n Gitter: https://gitter.im/resin-io/chat\n' ) ) ;
2015-10-06 22:51:17 +00:00
} ;
2015-08-20 19:54:42 +00:00
} ) . call ( this ) ;