-
Notifications
You must be signed in to change notification settings - Fork 145
/
print-commands.js
105 lines (95 loc) · 2.69 KB
/
print-commands.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const print = require('../utils/print')
const {
pipe,
isNil,
map,
sortBy,
prop,
propEq,
reject,
replace
} = require('ramda')
const { dotPath } = require('ramdasauce')
const { isBlank } = require('../utils/string-utils')
/**
* Does this have a loadState error?
*/
const hasLoadStateError = propEq('loadState', 'error')
/**
* Is this a hidden command?
*/
const isHidden = propEq('hidden', true)
/**
* Gets the list of plugins.
*
* @param {RunContext} context
* @return {[string, string]}
*/
function getListOfPlugins (context) {
return pipe(
dotPath('runtime.plugins'),
reject(hasLoadStateError),
reject(isHidden),
reject(plugin => context.runtime.defaultPlugin && context.runtime.defaultPlugin.name === plugin.name),
sortBy(prop('name')),
map(plugin => [
plugin.name,
replace('$BRAND', context.runtime.brand, plugin.description || '-')
])
)(context)
}
/**
* Gets the list of commands for the given plugin.
*
* @param {RunContext} context The context
* @param {Plugin} plugin The plugins holding the commands
* @return {[string, string]}
*/
function getListOfCommands (context, plugin) {
return pipe(
prop('commands'),
reject(hasLoadStateError),
reject(isHidden),
map(command => [
command.name,
replace('$BRAND', context.runtime.brand, command.description || '-')
])
)(plugin)
}
/**
* Prints the list of commands.
*
* @param {RunContext} context The context that was used
*/
function printCommands (context) {
// jet if we've got both a plugin & a command
if (context.plugin && context.command) return
const noPlugin = isNil(context.plugin)
const noCommand = isNil(context.command)
const searchedforPlugin = !isBlank(context.parameters.pluginName)
const searchedforCommand = !isBlank(context.parameters.rawCommand)
// was there user error involved?
let errorMessage = null
if (searchedforPlugin && noPlugin) {
errorMessage = `${context.runtime.brand} '${context.parameters.pluginName}' is not a command.`
} else if (searchedforCommand && noCommand) {
errorMessage = `${context.runtime.brand} ${context.parameters.pluginName} '${context.parameters.rawCommand}' is not a command.`
}
let data = []
if (noPlugin) {
data = getListOfPlugins(context)
if (context.runtime.defaultPlugin) {
const defaultCommands = getListOfCommands(context, context.runtime.defaultPlugin)
data = data.concat(defaultCommands)
}
} else if (noCommand) {
data = getListOfCommands(context, context.plugin)
}
print.newline() // a spacer
print.table(data) // the data
if (errorMessage) {
print.newline()
print.error(errorMessage)
}
}
module.exports = printCommands