balena-cli/build/actions/help.js

135 lines
3.7 KiB
JavaScript
Raw Normal View History

// Generated by CoffeeScript 1.12.5
/*
Copyright 2016 Resin.io
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2017-03-22 09:46:06 +00:00
var _, capitano, columnify, command, general, indent, messages, parse, print;
2017-03-22 09:46:06 +00:00
_ = require('lodash');
2015-02-26 15:47:56 +00:00
2017-03-22 09:46:06 +00:00
_.str = require('underscore.string');
2015-02-26 15:47:56 +00:00
2017-03-22 09:46:06 +00:00
capitano = require('capitano');
2015-02-26 15:47:56 +00:00
2017-03-22 09:46:06 +00:00
columnify = require('columnify');
2015-02-26 15:47:56 +00:00
2017-03-22 09:46:06 +00:00
messages = require('../utils/messages');
2015-02-26 15:47:56 +00:00
2017-03-22 09:46:06 +00:00
parse = function(object) {
return _.object(_.map(object, function(item) {
var signature;
if (item.alias != null) {
signature = item.toString();
} else {
2017-03-22 09:46:06 +00:00
signature = item.signature.toString();
}
return [signature, item.description];
}));
};
indent = function(text) {
text = _.map(_.str.lines(text), function(line) {
return ' ' + line;
});
return text.join('\n');
};
print = function(data) {
return console.log(indent(columnify(data, {
showHeaders: false,
minWidth: 35
})));
};
general = function(params, options, done) {
var commands, groupedCommands;
console.log('Usage: resin [COMMAND] [OPTIONS]\n');
console.log(messages.gettingStarted + "\n");
console.log(messages.reachingOut);
console.log('\nPrimary commands:\n');
commands = _.reject(capitano.state.commands, function(command) {
2017-03-24 09:48:14 +00:00
return command.hidden || command.isWildcard();
2017-03-22 09:46:06 +00:00
});
groupedCommands = _.groupBy(commands, function(command) {
if (command.plugin) {
return 'plugins';
2017-03-24 09:48:14 +00:00
}
if (command.primary) {
2017-03-22 09:46:06 +00:00
return 'primary';
}
return 'secondary';
});
print(parse(groupedCommands.primary));
if (options.verbose) {
if (!_.isEmpty(groupedCommands.plugins)) {
console.log('\nInstalled plugins:\n');
print(parse(groupedCommands.plugins));
}
console.log('\nAdditional commands:\n');
print(parse(groupedCommands.secondary));
} else {
console.log('\nRun `resin help --verbose` to list additional commands');
}
if (!_.isEmpty(capitano.state.globalOptions)) {
console.log('\nGlobal Options:\n');
print(parse(capitano.state.globalOptions));
}
return done();
};
command = function(params, options, done) {
return capitano.state.getMatchCommand(params.command, function(error, command) {
if (error != null) {
return done(error);
}
2017-03-22 09:46:06 +00:00
if ((command == null) || command.isWildcard()) {
return done(new Error("Command not found: " + params.command));
}
console.log("Usage: " + command.signature);
if (command.help != null) {
console.log("\n" + command.help);
} else if (command.description != null) {
console.log("\n" + (_.str.humanize(command.description)));
}
if (!_.isEmpty(command.options)) {
console.log('\nOptions:\n');
print(parse(command.options));
2015-08-12 12:17:46 +00:00
}
return done();
2017-03-22 09:46:06 +00:00
});
};
exports.help = {
signature: 'help [command...]',
description: 'show help',
help: 'Get detailed help for an specific command.\n\nExamples:\n\n $ resin help apps\n $ resin help os download',
primary: true,
options: [
{
signature: 'verbose',
description: 'show additional commands',
boolean: true,
alias: 'v'
2015-02-26 15:47:56 +00:00
}
2017-03-22 09:46:06 +00:00
],
action: function(params, options, done) {
if (params.command != null) {
return command(params, options, done);
} else {
return general(params, options, done);
}
}
};