Implement utils/helpers to abstract common app patterns

- Add helpers.confirm() to abstract the process of asking for
confirmation.
- Add helpers.selectDeviceType() to abstract the form needed to ask for
device types.

The functions on this module are reused by app actions.
This commit is contained in:
Juan Cruz Viotti 2015-08-13 14:00:23 -04:00
parent 25c6246e9f
commit 4647aa70c0
4 changed files with 65 additions and 58 deletions

View File

@ -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;

29
build/utils/helpers.js Normal file
View File

@ -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);

View File

@ -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

23
lib/utils/helpers.coffee Normal file
View File

@ -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