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 = ->
|
|
|
|
resin.models.device.getSupportedDeviceTypes().then (deviceTypes) ->
|
|
|
|
return form.ask
|
|
|
|
message: 'Device Type'
|
|
|
|
type: 'list'
|
|
|
|
choices: deviceTypes
|
|
|
|
|
|
|
|
exports.confirm = (yesOption, message) ->
|
|
|
|
Promise.try ->
|
|
|
|
return true if yesOption
|
|
|
|
return form.ask
|
|
|
|
message: message
|
|
|
|
type: 'confirm'
|
|
|
|
default: false
|
|
|
|
.then (confirmed) ->
|
|
|
|
if not confirmed
|
|
|
|
throw new Error('Aborted')
|
|
|
|
|
2015-11-11 19:00:02 +00:00
|
|
|
exports.selectApplication = (filter) ->
|
2015-09-29 15:10:33 +00:00
|
|
|
resin.models.application.hasAny().then (hasAnyApplications) ->
|
|
|
|
if not 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 or _.constant(true))
|
|
|
|
.then (applications) ->
|
|
|
|
return form.ask
|
|
|
|
message: 'Select an application'
|
|
|
|
type: 'list'
|
2015-11-23 13:23:08 +00:00
|
|
|
choices: _.map applications, (application) ->
|
|
|
|
return {
|
|
|
|
name: "#{application.app_name} (#{application.device_type})"
|
|
|
|
value: application.app_name
|
|
|
|
}
|
2015-09-29 15:10:33 +00:00
|
|
|
|
|
|
|
exports.selectOrCreateApplication = ->
|
2015-08-20 19:54:42 +00:00
|
|
|
resin.models.application.hasAny().then (hasAnyApplications) ->
|
|
|
|
return if not hasAnyApplications
|
|
|
|
resin.models.application.getAll().then (applications) ->
|
2015-11-23 13:23:08 +00:00
|
|
|
applications = _.map applications, (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 (application) ->
|
|
|
|
return application if application?
|
|
|
|
form.ask
|
|
|
|
message: 'Choose a Name for your new application'
|
|
|
|
type: 'input'
|
2015-10-21 13:37:25 +00:00
|
|
|
validate: validation.validateApplicationName
|
2015-08-20 19:54:42 +00:00
|
|
|
|
|
|
|
exports.awaitDevice = (uuid) ->
|
2015-10-07 15:38:59 +00:00
|
|
|
resin.models.device.getName(uuid).then (deviceName) ->
|
|
|
|
spinner = new visuals.Spinner("Waiting for #{deviceName} to come online")
|
2015-08-20 19:54:42 +00:00
|
|
|
|
2015-10-07 15:38:59 +00:00
|
|
|
poll = ->
|
|
|
|
resin.models.device.isOnline(uuid).then (isOnline) ->
|
|
|
|
if isOnline
|
|
|
|
spinner.stop()
|
|
|
|
console.info("Device became online: #{deviceName}")
|
|
|
|
return
|
|
|
|
else
|
2015-08-20 19:54:42 +00:00
|
|
|
|
2015-10-07 15:38:59 +00:00
|
|
|
# Spinner implementation is smart enough to
|
|
|
|
# not start again if it was already started
|
|
|
|
spinner.start()
|
2015-08-20 19:54:42 +00:00
|
|
|
|
2015-10-07 15:38:59 +00:00
|
|
|
return Promise.delay(3000).then(poll)
|
2015-08-20 19:54:42 +00:00
|
|
|
|
|
|
|
console.info("Waiting for #{deviceName} to connect to resin...")
|
|
|
|
poll().return(uuid)
|
2015-10-06 22:51:17 +00:00
|
|
|
|
|
|
|
exports.printErrorMessage = (message) ->
|
|
|
|
console.error(chalk.red(message))
|