Skip to content

Commit

Permalink
starting json output
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelly Selden committed Aug 22, 2015
1 parent 09cba45 commit 8a9f0fb
Show file tree
Hide file tree
Showing 33 changed files with 2,407 additions and 356 deletions.
104 changes: 67 additions & 37 deletions lib/commands/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ var chalk = require('chalk');
var Command = require('../models/command');
var Promise = require('../ext/promise');
var Blueprint = require('../models/blueprint');
var flatten = require('lodash/array/flatten');
var merge = require('lodash/object/merge');
var find = require('lodash/collection/find');
var pluck = require('lodash/collection/pluck');
var reject = require('lodash/collection/reject');
var EOL = require('os').EOL;

Expand Down Expand Up @@ -88,42 +85,54 @@ module.exports = Command.extend({
},

printDetailedHelp: function(options) {
if (options.rawArgs) {
this.printSingleBlueprint(options.rawArgs);
} else {
this.printAllBlueprints(options);
}
this.ui.writeLine(this.getAllBlueprints(options));
},

printSingleBlueprint: function(options) {
var blueprintName = options[0];
var lookupPaths = this.project.blueprintLookupPaths();
var blueprintList = Blueprint.list({ paths: lookupPaths });
var blueprints = flatten(pluck(blueprintList, 'blueprints'));
var blueprint = find(blueprints, function(model) {
return model.name === blueprintName;
});
if (blueprint) {
this.printBlueprintInfo(blueprint, true);
} else {
this.ui.writeLine(chalk.yellow('The \'' + blueprintName +
'\' blueprint does not exist in this project.'));
}
addAdditionalJsonForHelp: function(json, options) {
json.availableBlueprints = this.getAllBlueprints(options);
},

printAllBlueprints: function(options) {
getAllBlueprints: function(options) {
var lookupPaths = this.project.blueprintLookupPaths();
var blueprintList = Blueprint.list({ paths: lookupPaths });

this.ui.writeLine('');
this.ui.writeLine(' Available blueprints:');
var output = '';

var singleBlueprintName;
if (options.rawArgs) {
singleBlueprintName = options.rawArgs[0];
}

if (!singleBlueprintName && !options.json) {
output += EOL + ' Available blueprints:' + EOL;
}

var collectionsJson = [];

blueprintList.forEach(function(collection) {
this.printPackageBlueprints(collection, options);
var result = this.getPackageBlueprints(collection, options, singleBlueprintName);
if (options.json) {
var collectionJson = {};
collectionJson[collection.source] = result;
collectionsJson.push(collectionJson);
} else {
output += result;
}
}, this);

if (singleBlueprintName && !output && !options.json) {
output = chalk.yellow('The \'' + singleBlueprintName +
'\' blueprint does not exist in this project.') + EOL;
}

if (options.json) {
return collectionsJson;
} else {
return output;
}
},

printPackageBlueprints: function(collection, options) {
getPackageBlueprints: function(collection, options, singleBlueprintName) {

var verbose = options.verbose;
var blueprints = collection.blueprints;
Expand All @@ -132,22 +141,43 @@ module.exports = Command.extend({
blueprints = reject(blueprints, 'overridden');
}

if (blueprints.length === 0) {
return;
var output = '';

if (blueprints.length && !singleBlueprintName && !options.json) {
output += ' ' + collection.source + ':' + EOL;
}

this.ui.writeLine(' ' + collection.source + ':');
var blueprintsJson = [];

blueprints.forEach(function(blueprint) {
this.printBlueprintInfo(blueprint, verbose);
var singleMatch = singleBlueprintName === blueprint.name;
if (singleMatch) {
verbose = true;
}
if (!singleBlueprintName || singleMatch) {
// this may add default keys for printing
blueprint.availableOptions.forEach(this.normalizeOption);

if (options.json) {
blueprintsJson.push(blueprint.getJson(verbose));
} else {
output += this.getBlueprintInfoString(blueprint, verbose) + EOL;
}
}
}, this);

if (options.json) {
return blueprintsJson;
} else {
return output;
}
},

printBlueprintInfo: function(blueprint, verbose) {
getBlueprintInfoString: function(blueprint, verbose) {
var options;
var output = ' ';
if (blueprint.overridden) {
output += chalk.grey('(overridden) ');
output += chalk.grey('(overridden)') + ' ';
output += chalk.grey(blueprint.name);
} else {
output += blueprint.name;
Expand Down Expand Up @@ -175,15 +205,15 @@ module.exports = Command.extend({
}

if (opt.default !== undefined) {
output += chalk.cyan(' (Default: ' + opt.default + ')');
output += ' ' + chalk.cyan('(Default: ' + opt.default + ')');
}

if (opt.required) {
output += chalk.cyan(' (Required)');
output += ' ' + chalk.cyan('(Required)');
}

if (opt.aliases) {
output += chalk.grey(EOL + ' aliases: ' + opt.aliases.map(function(a) {
output += EOL + ' ' + chalk.grey('aliases: ' + opt.aliases.map(function(a) {
var key;
if (typeof a === 'string') {
return '-' + a + (opt.type === Boolean ? '' : ' <value>');
Expand All @@ -206,6 +236,6 @@ module.exports = Command.extend({

}

this.ui.writeLine(output);
return output;
}
});
106 changes: 82 additions & 24 deletions lib/commands/help.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

var path = require('path');
var Command = require('../models/command');
var lookupCommand = require('../cli/lookup-command');
var string = require('../utilities/string');
Expand All @@ -23,7 +24,8 @@ module.exports = Command.extend({
aliases: [undefined, 'h', '--help', '-h'],

availableOptions: [
{ name: 'verbose', type: Boolean, default: false, aliases: ['v'] }
{ name: 'verbose', type: Boolean, default: false, aliases: ['v'] },
{ name: 'json', type: Boolean, default: false }
],

anonymousOptions: [
Expand All @@ -33,41 +35,66 @@ module.exports = Command.extend({
run: function(commandOptions, rawArgs) {
var multipleCommands = ['g','generate'];
var command;
var json;
var rootCommand = new RootCommand({
ui: this.ui,
project: this.project,
commands: this.commands,
tasks: this.tasks
});
if (commandOptions.json) {
json = rootCommand.getJson(commandOptions);
json.commands = [];
json.addons = [];
}
if (rawArgs.length === 0) {
var rootCommand = new RootCommand({
ui: this.ui,
project: this.project,
commands: this.commands,
tasks: this.tasks
});
rootCommand.printBasicHelp(commandOptions);
// Display usage for all commands.
this.ui.writeLine('Available commands in ember-cli:');
this.ui.writeLine('');
if (!commandOptions.json) {
rootCommand.printBasicHelp(commandOptions);
// Display usage for all commands.
this.ui.writeLine('Available commands in ember-cli:');
this.ui.writeLine('');
}

Object.keys(this.commands).forEach(function(commandName) {
this._printBasicHelpForCommand(commandName, commandOptions);
}.bind(this));
if (commandOptions.json) {
this._addCommandHelpToJson(commandName, commandOptions, json);
} else {
this._printBasicHelpForCommand(commandName, commandOptions);
}
}, this);

if (this.project.eachAddonCommand) {
this.project.eachAddonCommand(function(addonName, commands){
this.project.eachAddonCommand(function(addonName, commands) {
this.commands = commands;
this.ui.writeLine('');
this.ui.writeLine('Available commands from ' + addonName + ':');
var addonJson;
if (commandOptions.json) {
addonJson = this.getJson(commandOptions);
addonJson.commands = [];
json.addons.push(addonJson);
} else {
this.ui.writeLine('');
this.ui.writeLine('Available commands from ' + addonName + ':');
}
Object.keys(this.commands).forEach(function(commandName) {
this._printBasicHelpForCommand(commandName, commandOptions);
}.bind(this));
if (commandOptions.json) {
this._addCommandHelpToJson(commandName, commandOptions, addonJson);
} else {
this._printBasicHelpForCommand(commandName, commandOptions);
}
}, this);
}.bind(this));
}

} else {
// If args were passed to the help command,
// attempt to look up the command for each of them.
this.ui.writeLine('Requested ember-cli commands:');
this.ui.writeLine('');
if (!commandOptions.json) {
this.ui.writeLine('Requested ember-cli commands:');
this.ui.writeLine('');
}

if (this.project.eachAddonCommand) {
this.project.eachAddonCommand(function(addonName, commands){
this.project.eachAddonCommand(function(addonName, commands) {
assign(this.commands, commands);
}.bind(this));
}
Expand All @@ -77,17 +104,36 @@ module.exports = Command.extend({
if (rawArgs.length > 0) {
commandOptions.rawArgs = rawArgs;
}
this._printDetailedHelpForCommand(command, commandOptions);
if (commandOptions.json) {
this._addCommandHelpToJson(command, commandOptions, json);
} else {
this._printDetailedHelpForCommand(command, commandOptions);
}

} else {

// Iterate through each arg beyond the initial 'help' command,
// and try to display usage instructions.
rawArgs.forEach(function(commandName) {
this._printDetailedHelpForCommand(commandName, commandOptions);
}.bind(this));
if (commandOptions.json) {
this._addCommandHelpToJson(commandName, commandOptions, json);
} else {
this._printDetailedHelpForCommand(commandName, commandOptions);
}
}, this);
}
}

if (commandOptions.json) {
this._printJsonHelp(json);
}
},

_addCommandHelpToJson: function(commandName, options, json) {
var command = this._lookupCommand(commandName);
if (!command.skipHelp) {
json.commands.push(command.getJson(options));
}
},

_printBasicHelpForCommand: function(commandName, options) {
Expand All @@ -110,6 +156,18 @@ module.exports = Command.extend({
}
},

_printJsonHelp: function(json) {
var outputJsonString = JSON.stringify(json, function(key, value) {
// build command has a recursive property
if (value === path) {
return 'path';
}
return value;
}, 2);

this.ui.writeLine(outputJsonString);
},

_lookupCommand: function(commandName) {
var Command = this.commands[string.classify(commandName)] ||
lookupCommand(this.commands, commandName);
Expand Down
Loading

0 comments on commit 8a9f0fb

Please sign in to comment.