resin sync: always display device selection dialog when uuid is not passed as an argument

Closes #375
This commit is contained in:
Kostas Lekkas 2016-07-29 12:12:56 +03:00
parent e0a2217b94
commit 16a1741374
2 changed files with 10 additions and 11 deletions

View File

@ -146,10 +146,11 @@ module.exports =
if options.ignore?
options.ignore = options.ignore.split(',')
Promise.resolve(params.uuid ? loadConfig(options.source).uuid)
Promise.resolve(params.uuid)
.then (uuid) ->
if not uuid?
return patterns.inferOrSelectDevice()
savedUuid = loadConfig(options.source).uuid
return patterns.inferOrSelectDevice(savedUuid)
resin.models.device.has(uuid)
.then (hasDevice) ->

View File

@ -150,21 +150,19 @@ exports.awaitDevice = (uuid) ->
console.info("Waiting for #{deviceName} to connect to resin...")
poll().return(uuid)
exports.inferOrSelectDevice = (applicationName) ->
Promise.try ->
if applicationName?
return resin.models.device.getAllByApplication(applicationName)
return resin.models.device.getAll()
exports.inferOrSelectDevice = (preferredUuid) ->
resin.models.device.getAll()
.filter (device) ->
device.is_online
.then (devices) ->
if _.isEmpty(devices)
throw new Error('You don\'t have any devices')
.then (onlineDevices) ->
if _.isEmpty(onlineDevices)
throw new Error('You don\'t have any devices online')
return form.ask
message: 'Select a device'
type: 'list'
choices: _.map devices, (device) ->
default: if preferredUuid in _.map(onlineDevices, 'uuid') then preferredUuid else onlineDevices[0].uuid
choices: _.map onlineDevices, (device) ->
return {
name: "#{device.name or 'Untitled'} (#{device.uuid.slice(0, 7)})"
value: device.uuid