2014-12-12 21:20:29 +00:00
|
|
|
_ = require('lodash')
|
|
|
|
_.str = require('underscore.string')
|
2015-01-08 12:04:37 +00:00
|
|
|
resin = require('resin-sdk')
|
2014-12-12 21:20:29 +00:00
|
|
|
capitano = require('capitano')
|
|
|
|
|
|
|
|
# TODO: Refactor this terrible mess
|
|
|
|
|
|
|
|
PADDING_INITIAL = ' '
|
|
|
|
PADDING_MIDDLE = '\t'
|
|
|
|
|
|
|
|
getFieldMaxLength = (array, field) ->
|
|
|
|
return _.max _.map array, (item) ->
|
|
|
|
return item[field].toString().length
|
|
|
|
|
|
|
|
buildHelpString = (firstColumn, secondColumn) ->
|
|
|
|
result = "#{PADDING_INITIAL}#{firstColumn}"
|
|
|
|
result += "#{PADDING_MIDDLE}#{secondColumn}"
|
|
|
|
return result
|
|
|
|
|
|
|
|
addOptionPrefix = (option) ->
|
|
|
|
return if option.length <= 0
|
|
|
|
if option.length is 1
|
|
|
|
return "-#{option}"
|
|
|
|
else
|
|
|
|
return "--#{option}"
|
|
|
|
|
|
|
|
addAlias = (alias) ->
|
|
|
|
return ", #{addOptionPrefix(alias)}"
|
|
|
|
|
|
|
|
buildOptionSignatureHelp = (option) ->
|
|
|
|
result = addOptionPrefix(option.signature.toString())
|
|
|
|
|
|
|
|
if _.isString(option.alias)
|
|
|
|
result += addAlias(option.alias)
|
|
|
|
else if _.isArray(option.alias)
|
|
|
|
for alias in option.alias
|
|
|
|
result += addAlias(alias)
|
|
|
|
|
|
|
|
if option.parameter?
|
|
|
|
result += " <#{option.parameter}>"
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
getCommandHelp = (command) ->
|
|
|
|
maxSignatureLength = getFieldMaxLength(capitano.state.commands, 'signature')
|
|
|
|
commandSignature = _.str.rpad(command.signature.toString(), maxSignatureLength, ' ')
|
|
|
|
return buildHelpString(commandSignature, command.description)
|
|
|
|
|
|
|
|
getOptionsParsedSignatures = (optionsHelp) ->
|
|
|
|
maxLength = _.max _.map optionsHelp, (signature) ->
|
|
|
|
return signature.length
|
|
|
|
|
|
|
|
return _.map optionsHelp, (signature) ->
|
|
|
|
return _.str.rpad(signature, maxLength, ' ')
|
|
|
|
|
|
|
|
getOptionHelp = (option, maxLength) ->
|
|
|
|
result = PADDING_INITIAL
|
|
|
|
result += _.str.rpad(option.signature, maxLength, ' ')
|
|
|
|
result += PADDING_MIDDLE
|
|
|
|
result += option.description
|
|
|
|
return result
|
|
|
|
|
|
|
|
exports.general = ->
|
2015-01-15 14:36:43 +00:00
|
|
|
console.log("Usage: #{process.argv[0]} [COMMAND] [OPTIONS]\n")
|
|
|
|
console.log('Commands:\n')
|
2014-12-12 21:20:29 +00:00
|
|
|
|
|
|
|
for command in capitano.state.commands
|
|
|
|
continue if command.isWildcard()
|
2015-01-15 14:36:43 +00:00
|
|
|
console.log(getCommandHelp(command))
|
2014-12-12 21:20:29 +00:00
|
|
|
|
2015-01-15 14:36:43 +00:00
|
|
|
console.log('\nGlobal Options:\n')
|
2014-12-12 21:20:29 +00:00
|
|
|
|
|
|
|
options = _.map capitano.state.globalOptions, (option) ->
|
|
|
|
option.signature = buildOptionSignatureHelp(option)
|
|
|
|
return option
|
|
|
|
|
|
|
|
optionSignatureMaxLength = _.max _.map options, (option) ->
|
|
|
|
return option.signature.length
|
|
|
|
|
|
|
|
for option in options
|
2015-01-15 14:36:43 +00:00
|
|
|
console.log(getOptionHelp(option, optionSignatureMaxLength))
|
2014-12-12 21:20:29 +00:00
|
|
|
|
2015-01-15 14:36:43 +00:00
|
|
|
console.log()
|
2014-12-12 21:20:29 +00:00
|
|
|
|
|
|
|
exports.command = (params) ->
|
|
|
|
command = capitano.state.getMatchCommand(params.command)
|
|
|
|
|
|
|
|
if not command? or command.isWildcard()
|
|
|
|
return capitano.defaults.actions.commandNotFound(params.command)
|
|
|
|
|
2015-01-15 14:36:43 +00:00
|
|
|
console.log("Usage: #{command.signature}")
|
2014-12-12 21:20:29 +00:00
|
|
|
|
|
|
|
if command.help?
|
2015-01-15 14:36:43 +00:00
|
|
|
console.log("\n#{command.help}")
|
2014-12-12 21:20:29 +00:00
|
|
|
else if command.description?
|
2015-01-15 14:36:43 +00:00
|
|
|
console.log("\n#{_.str.humanize(command.description)}")
|
2014-12-12 21:20:29 +00:00
|
|
|
|
|
|
|
if not _.isEmpty(command.options)
|
2015-01-15 14:36:43 +00:00
|
|
|
console.log('\nOptions:\n')
|
2014-12-12 21:20:29 +00:00
|
|
|
|
|
|
|
options = _.map command.options, (option) ->
|
|
|
|
option.signature = buildOptionSignatureHelp(option)
|
|
|
|
return option
|
|
|
|
|
|
|
|
optionSignatureMaxLength = _.max _.map options, (option) ->
|
|
|
|
return option.signature.toString().length
|
|
|
|
|
|
|
|
for option in options
|
2015-01-15 14:36:43 +00:00
|
|
|
console.log(getOptionHelp(option, optionSignatureMaxLength))
|
2014-12-12 21:20:29 +00:00
|
|
|
|
2015-01-15 14:36:43 +00:00
|
|
|
console.log()
|
2015-01-15 14:18:34 +00:00
|
|
|
|
2015-01-15 17:10:14 +00:00
|
|
|
exports.help =
|
|
|
|
signature: 'help [command...]'
|
|
|
|
description: 'show help'
|
|
|
|
action: (params) ->
|
|
|
|
if params.command?
|
|
|
|
exports.command(arguments...)
|
|
|
|
else
|
|
|
|
exports.general(arguments...)
|