Sort 'balena help' primary commands in manually specified order

Connects-to: #1140
Change-type: patch
Signed-off-by: Paulo Castro <paulo@balena.io>
This commit is contained in:
Paulo Castro 2019-04-29 22:31:49 +01:00
parent c49a1d3fbf
commit 8f8d6b5f08
2 changed files with 37 additions and 14 deletions

View File

@ -19,6 +19,7 @@ capitano = require('capitano')
columnify = require('columnify')
messages = require('../utils/messages')
{ getManualSortCompareFunction } = require('../utils/helpers')
{ exitWithExpectedError } = require('../utils/patterns')
{ getOclifHelpLinePairs } = require('./help_ts')
@ -42,12 +43,30 @@ indent = (text) ->
return ' ' + line
return text.join('\n')
print = (usageDescriptionPairs...) ->
data = _.fromPairs([].concat(usageDescriptionPairs...).sort())
console.log indent columnify data,
print = (usageDescriptionPairs) ->
console.log indent columnify _.fromPairs(usageDescriptionPairs),
showHeaders: false
minWidth: 35
manuallySortedPrimaryCommands = [
'help',
'login',
'push',
'logs',
'ssh',
'apps',
'app',
'devices',
'device',
'tunnel',
'preload',
'build',
'deploy',
'join',
'leave',
'local scan',
]
general = (params, options, done) ->
console.log('Usage: balena [COMMAND] [OPTIONS]\n')
console.log(messages.reachingOut)
@ -63,17 +82,21 @@ general = (params, options, done) ->
return 'primary'
return 'secondary'
print(parse(groupedCommands.primary))
print parse(groupedCommands.primary).sort(getManualSortCompareFunction(
manuallySortedPrimaryCommands,
([signature, description], manualItem) ->
signature == manualItem or signature.startsWith("#{manualItem} ")
))
if options.verbose
console.log('\nAdditional commands:\n')
print(parse(groupedCommands.secondary), getOclifHelpLinePairs())
print parse(groupedCommands.secondary).concat(getOclifHelpLinePairs()).sort()
else
console.log('\nRun `balena help --verbose` to list additional commands')
if not _.isEmpty(capitano.state.globalOptions)
console.log('\nGlobal Options:\n')
print(parse(capitano.state.globalOptions))
print parse(capitano.state.globalOptions).sort()
return done()
@ -93,7 +116,7 @@ command = (params, options, done) ->
if not _.isEmpty(command.options)
console.log('\nOptions:\n')
print(parse(command.options))
print parse(command.options).sort()
return done()

View File

@ -234,15 +234,15 @@ export function retry<T>(
* compare(a, b) function will compare a and b using the standard '<' and
* '>' Javascript operators.
* - If only a or only b are found in the manuallySortedArray, the returned
* compare(a, b) function will consider the found element as being
* "smaller than" the not-found element (i.e. found elements appeare before
* compare(a, b) function will treat the element that was found as being
* "smaller than" the not-found element (i.e. found elements appear before
* not-found elements in sorted order).
*
* The equalityFunc() argument is a function used to compare the array items
* against the manuallySortedArray. For example, if equalityFunc was (a, x) =>
* a.startsWith(x), where a is an item being sorted and x is an item in the
* manuallySortedArray, then the manuallySortedArray could contain prefix
* substrings to guide the sorting.
* The equalityFunc(a, x) argument is a function used to compare the items
* being sorted against the items in the manuallySortedArray. For example, if
* equalityFunc was (a, x) => a.startsWith(x), where a is an item being sorted
* and x is an item in the manuallySortedArray, then the manuallySortedArray
* could contain prefix substrings to guide the sorting.
*
* @param manuallySortedArray A pre-sorted array to guide the sorting
* @param equalityFunc An optional function used to compare the items being