dash: add summary and other improvements

This commit is contained in:
Austen Collins 2015-09-08 21:08:48 -07:00
parent 7bb73ae26f
commit 45f5c48e47
2 changed files with 90 additions and 21 deletions

View File

@ -6,7 +6,9 @@ var JawsError = require('../jaws-error'),
JawsCLI = require('../utils/cli'),
Promise = require('bluebird'),
fs = require('fs'),
os = require('os'),
path = require('path'),
chalk = require('chalk'),
utils = require('../utils/index'),
tagCmd = require('./tag');
@ -100,36 +102,97 @@ CMD.prototype._collectChoices = Promise.method(function() {
return utils.findAllJawsJsons(_this._projectRootPath)
.then(function(jsonPaths) {
// Prepare choices from json modules
var modules = [];
// Fetch and prepare json modules
for (var i = 0; i < jsonPaths.length; i++) {
// Add modules
var json = require(jsonPaths[i]);
// Add Spacer
if (json.lambda || json.endpoint) {
_this._choices.push({ spacer: true });
}
var module = {};
// Add Lambda
if (json.lambda) {
_this._choices.push({
key: 'L) ',
value: json.lambda.functionName,
});
// Parse lambda path
var paths = jsonPaths[i].split('/');
paths = paths[paths.length - 3] + '/' + paths[paths.length - 2];
paths = chalk.grey(' in ' + paths);
module.lambda = json.lambda.functionName + paths;
}
// Add Endpoint
if (json.endpoint) {
module.endpoint = json.endpoint.path + chalk.grey(' - ' + json.endpoint.method);
}
modules.push(module);
}
// Sort by endpoint path
modules.sort(function(a, b) {
return (a.endpoint < b.endpoint) ? -1 : (a.endpoint > b.endpoint) ? 1 : 0;
});
// Prepare Choices
for (var i = 0; i < modules.length; i++) {
if (modules[i].lambda || modules[i].endpoint) {
_this._choices.push({
spacer: true,
});
}
if (modules[i].lambda) {
_this._choices.push({
key: 'L) ',
value: modules[i].lambda,
type: 'lambda'
});
}
if (modules[i].endpoint) {
_this._choices.push({
key: 'E) ',
value: json.endpoint.path + ' - ' + json.endpoint.method.toUpperCase(),
value: modules[i].endpoint,
type: 'endpoint'
});
}
}
// Remove first spacer
_this._choices.splice(0, 1);
});
});
/**
* Show Summary
*/
CMD.prototype._prepareSummary = Promise.method(function() {
var _this = this;
var lambdaCount = 0;
var endpointCount = 0;
for (var i = 0; i < _this._choices.length; i++) {
if (_this._choices[i].type === 'lambda') lambdaCount++;
if (_this._choices[i].type === 'endpoint') endpointCount++;
}
_this._summary = 'DASHBOARD' + os.EOL
+ chalk.white.bold(' -------------------------------------------') + os.EOL
+ chalk.white(' Project Summary') + os.EOL
+ chalk.white.bold(' -------------------------------------------') + os.EOL
+ chalk.white(' Lambdas: ' + lambdaCount) + os.EOL
+ chalk.white(' Endpoints: ' + endpointCount) + os.EOL
+ chalk.white(' Target Stage: ' + _this._stage) + os.EOL
+ chalk.white(' Target Region: ' + _this._region) + os.EOL
+ chalk.white.bold(' -------------------------------------------') + os.EOL
+ chalk.white(' Select Resources To Deploy') + os.EOL
+ chalk.white.bold(' -------------------------------------------');
});
/**
* Render Dash
*/
@ -138,10 +201,11 @@ CMD.prototype._renderDash = Promise.method(function() {
var _this = this;
return JawsCLI.select(
'Dashboard for "' + _this._stage + ' - ' + _this._region + '"',
_this._summary,
_this._choices,
true,
'- - - - -');
'- - - - -',
'Deploy Selected');
});
/**
@ -155,7 +219,8 @@ CMD.prototype.run = Promise.method(function() {
.bind(_this)
.then(_this._promptRegion)
.then(_this._collectChoices)
.then(_this._renderDash)
.then(_this._prepareSummary)
.then(_this._renderDash);
});
module.exports.run = function(JAWS, stage, region) {

View File

@ -106,7 +106,7 @@ Select._render = function() {
line = line + choice.value;
// Add toggled style
if (choice.toggled) {
line = chalk.yellow.bold(line);
line = chalk.yellow(line);
}
// Add line break
line = line + os.EOL;
@ -119,7 +119,7 @@ Select._render = function() {
// Render Action
if (choice.action) {
line = choice.action + os.EOL;
line = choice.label + os.EOL;
}
// TODO: Add custom word wrap after measuring terminal width. Re-count lines.
@ -156,7 +156,7 @@ Select._close = function(cb) {
return Select._promise(selected);
};
module.exports.select = function(message, choices, multi, spacer) {
module.exports.select = function(message, choices, multi, spacer, doneLabel) {
// Set keypress listener, if not set
if (!Select.state) {
@ -199,7 +199,7 @@ module.exports.select = function(message, choices, multi, spacer) {
}
return Select._render();
} else if (key.name == 'return') {
// Check if "done" option
@ -232,17 +232,21 @@ module.exports.select = function(message, choices, multi, spacer) {
// Update CheckList
Select.state = {
choices: choices,
index: choices[0].spacer ? 2 : 1,
index: (choices[0] && choices[0].spacer) ? 2 : 1,
lines: 0,
multi: multi,
spacer: spacer,
doneLabel: doneLabel ? doneLabel : 'Done',
};
// Add Done and Cancel to choices
if (Select.state.multi) {
Select.state.choices.push(
{spacer: true},
{action: 'Done'});
{ spacer: true },
{
action: 'Done',
label: Select.state.doneLabel,
});
}
// Log Message