diff --git a/build/actions/app.js b/build/actions/app.js index eabe210a..bdf37a7b 100644 --- a/build/actions/app.js +++ b/build/actions/app.js @@ -1,10 +1,8 @@ (function() { - var Promise, _, commandOptions, form, resin, vcs, visuals; + var _, commandOptions, helpers, resin, vcs, visuals; _ = require('lodash'); - Promise = require('bluebird'); - resin = require('resin-sdk'); visuals = require('resin-cli-visuals'); @@ -13,7 +11,7 @@ vcs = require('resin-vcs'); - form = require('resin-cli-form'); + helpers = require('../utils/helpers'); exports.create = { signature: 'app create <name>', @@ -33,13 +31,9 @@ if (hasApplication) { throw new Error('You already have an application with that name!'); } - }).then(function() { - return form.ask({ - message: 'Device Type', - type: 'list', - choices: ['Raspberry Pi', 'Raspberry Pi 2', 'BeagleBone Black'] - }); - }).then(_.partial(resin.models.application.create, params.name)).then(function(application) { + }).then(helpers.selectDeviceType).then(function(deviceType) { + return resin.models.application.create(params.name, deviceType); + }).then(function(application) { return console.info("Application created: " + application.app_name + " (" + application.device_type + ", id " + application.id + ")"); }).nodeify(done); } @@ -86,16 +80,7 @@ options: [commandOptions.yes], permission: 'user', action: function(params, options, done) { - return Promise["try"](function() { - if (options.yes) { - return true; - } - return form.ask({ - message: 'Are you sure you want to delete the application?', - type: 'confirm', - "default": false - }); - }).then(function(confirmed) { + return helpers.confirm(option.yes, 'Are you sure you want to delete the application?').then(function(confirmed) { if (!confirmed) { return; } @@ -119,15 +104,8 @@ } }).then(function() { var message; - if (options.yes) { - return true; - } message = "Are you sure you want to associate " + currentDirectory + " with " + params.name + "?"; - return form.ask({ - message: message, - type: 'confirm', - "default": false - }); + return helpers.confirm(options.yes, message); }).then(function(confirmed) { if (!confirmed) { return; diff --git a/build/utils/helpers.js b/build/utils/helpers.js new file mode 100644 index 00000000..f0553665 --- /dev/null +++ b/build/utils/helpers.js @@ -0,0 +1,29 @@ +(function() { + var Promise, form; + + Promise = require('bluebird'); + + form = require('resin-cli-form'); + + exports.selectDeviceType = function() { + return form.ask({ + message: 'Device Type', + type: 'list', + choices: ['Raspberry Pi', 'Raspberry Pi 2', 'BeagleBone Black'] + }); + }; + + exports.confirm = function(yesOption, message) { + return Promise["try"](function() { + if (yesOption) { + return true; + } + return form.ask({ + message: message, + type: 'confirm', + "default": false + }); + }); + }; + +}).call(this); diff --git a/lib/actions/app.coffee b/lib/actions/app.coffee index f11f943a..0b0956bb 100644 --- a/lib/actions/app.coffee +++ b/lib/actions/app.coffee @@ -1,10 +1,9 @@ _ = require('lodash') -Promise = require('bluebird') resin = require('resin-sdk') visuals = require('resin-cli-visuals') commandOptions = require('./command-options') vcs = require('resin-vcs') -form = require('resin-cli-form') +helpers = require('../utils/helpers') exports.create = signature: 'app create <name>' @@ -42,19 +41,9 @@ exports.create = if hasApplication throw new Error('You already have an application with that name!') - .then -> - form.ask - message: 'Device Type' - type: 'list' - choices: [ - - # Lock to specific devices until we support - # the rest with device specs. - 'Raspberry Pi' - 'Raspberry Pi 2' - 'BeagleBone Black' - ] - .then(_.partial(resin.models.application.create, params.name)).then (application) -> + .then(helpers.selectDeviceType).then (deviceType) -> + return resin.models.application.create(params.name, deviceType) + .then (application) -> console.info("Application created: #{application.app_name} (#{application.device_type}, id #{application.id})") .nodeify(done) @@ -136,14 +125,7 @@ exports.remove = options: [ commandOptions.yes ] permission: 'user' action: (params, options, done) -> - Promise.try -> - return true if options.yes - - form.ask - message: 'Are you sure you want to delete the application?' - type: 'confirm' - default: false - .then (confirmed) -> + helpers.confirm(option.yes, 'Are you sure you want to delete the application?').then (confirmed) -> return if not confirmed resin.models.application.remove(params.name) .nodeify(done) @@ -173,13 +155,8 @@ exports.associate = throw new Error("Invalid application: #{params.name}") .then -> - return true if options.yes - message = "Are you sure you want to associate #{currentDirectory} with #{params.name}?" - form.ask - message: message - type: 'confirm' - default: false + helpers.confirm(options.yes, message) .then (confirmed) -> return if not confirmed diff --git a/lib/utils/helpers.coffee b/lib/utils/helpers.coffee new file mode 100644 index 00000000..b79c5d98 --- /dev/null +++ b/lib/utils/helpers.coffee @@ -0,0 +1,23 @@ +Promise = require('bluebird') +form = require('resin-cli-form') + +exports.selectDeviceType = -> + return form.ask + message: 'Device Type' + type: 'list' + choices: [ + + # Lock to specific devices until we support + # the rest with device specs. + 'Raspberry Pi' + 'Raspberry Pi 2' + 'BeagleBone Black' + ] + +exports.confirm = (yesOption, message) -> + Promise.try -> + return true if yesOption + return form.ask + message: message + type: 'confirm' + default: false