balena-cli
Version:
The official balena Command Line Interface
175 lines • 6.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@oclif/core");
const indent = require("indent-string");
const lazy_1 = require("./utils/lazy");
function getHelpSubject(args) {
for (const arg of args) {
if (arg === '--') {
return;
}
if (arg === 'help' || arg === '--help') {
continue;
}
if (arg.startsWith('-')) {
return;
}
return arg;
}
}
class BalenaHelp extends core_1.Help {
constructor() {
super(...arguments);
this.formatCommandsTitle = null;
this.manuallySortedPrimaryCommands = [
'login',
'push',
'fleet',
'device',
'preload',
'build',
'deploy',
'join',
'leave',
];
}
async showCommandHelp(command) {
this.formatCommandsTitle = 'SUB COMMANDS';
await super.showCommandHelp(command);
}
async showHelp(argv) {
const ux = (0, lazy_1.getCliUx)();
const subject = getHelpSubject(argv);
if (!subject) {
const verbose = argv.includes('-v') || argv.includes('--verbose');
console.log(this.getCustomRootHelp(verbose));
return;
}
const command = this.config.findCommand(subject);
if (command) {
await this.showCommandHelp(command);
return;
}
const topicCommands = await Promise.all(this.config.commands
.filter((c) => {
return c.id.startsWith(`${subject}:`);
})
.map((topic) => topic.load()));
if (topicCommands.length > 0) {
console.log(`${ux.colorize('yellow', subject)} commands include:`);
console.log(this.formatCommands(topicCommands));
console.log(`\nRun ${ux.colorize('bold', ux.colorize('cyan', 'balena help -v'))} for a list of all available commands,`);
console.log(` or ${ux.colorize('bold', ux.colorize('cyan', 'balena help <command>'))} for detailed help on a specific command.`);
return;
}
console.log(`command ${ux.colorize('bold', ux.colorize('cyan', subject))} not found`);
}
getCustomRootHelp(showAllCommands) {
const ux = (0, lazy_1.getCliUx)();
let commands = this.config.commands;
commands = commands.filter((c) => this.opts.all || !c.hidden);
const primaryCommands = this.manuallySortedPrimaryCommands
.map((pc) => {
return commands.find((c) => c.id === pc.replace(' ', ':'));
})
.filter((c) => !!c);
let cmdLength = 0;
for (const cmd of primaryCommands) {
cmdLength = Math.max(cmdLength, cmd.id.length);
}
let additionalCmdSection;
if (showAllCommands) {
const additionalCommands = commands.filter((c) => !this.manuallySortedPrimaryCommands.includes(c.id.replace(':', ' ')));
for (const cmd of additionalCommands) {
cmdLength = Math.max(cmdLength, cmd.id.length);
}
if (typeof primaryCommands[0].id === 'string' &&
typeof additionalCommands[0].id === 'string') {
primaryCommands[0].id = primaryCommands[0].id.padEnd(cmdLength);
additionalCommands[0].id = additionalCommands[0].id.padEnd(cmdLength);
}
additionalCmdSection = [
ux.colorize('bold', '\nADDITIONAL COMMANDS'),
this.formatCommands(additionalCommands),
];
}
else {
const cmd = ux.colorize('bold', ux.colorize('cyan', 'balena help --verbose'));
additionalCmdSection = [
`\n${ux.colorize('bold', '...MORE')} run ${cmd} to list additional commands.`,
];
}
const globalOps = [
['--help', 'display command help'],
['--debug', 'enable debug output'],
[
'--unsupported',
`\
prevent exit with an error as per Deprecation Policy
See: https://git.io/JRHUW#deprecation-policy`,
],
];
globalOps[0][0] = globalOps[0][0].padEnd(cmdLength);
const { deprecationPolicyNote, reachingOut } = require('./utils/messages');
return [
ux.colorize('bold', 'USAGE'),
'$ balena [COMMAND] [OPTIONS]',
ux.colorize('bold', '\nPRIMARY COMMANDS'),
this.formatCommands(primaryCommands),
...additionalCmdSection,
ux.colorize('bold', '\nGLOBAL OPTIONS'),
this.formatGlobalOpts(globalOps),
ux.colorize('bold', '\nDeprecation Policy Reminder'),
deprecationPolicyNote,
reachingOut,
].join('\n');
}
formatGlobalOpts(opts) {
const ux = (0, lazy_1.getCliUx)();
const outLines = [];
let flagWidth = 0;
for (const opt of opts) {
flagWidth = Math.max(flagWidth, opt[0].length);
}
for (const opt of opts) {
const descriptionLines = opt[1].split('\n');
outLines.push(` ${opt[0].padEnd(flagWidth + 2)}${ux.colorize('dim', descriptionLines[0])}`);
outLines.push(...descriptionLines
.slice(1)
.map((line) => ` ${' '.repeat(flagWidth + 2)}${ux.colorize('dim', line)}`));
}
return outLines.join('\n');
}
formatCommands(commands) {
if (commands.length === 0) {
return '';
}
const body = this.renderList(commands.map((c) => [
c.id.replaceAll(':', ' '),
this.formatDescription(c.description),
]), {
spacer: '\n',
stripAnsi: this.opts.stripAnsi,
indentation: 2,
multiline: false,
});
if (this.formatCommandsTitle) {
this.log(this.formatCommandsTitle);
this.formatCommandsTitle = null;
}
return indent(body, 2);
}
formatDescription(desc = '') {
const ux = (0, lazy_1.getCliUx)();
desc = desc.split('\n')[0];
if (desc.endsWith('.')) {
desc = desc.substring(0, desc.length - 1);
}
if (desc.length > 1 && desc[1] === desc[1].toLowerCase()) {
desc = `${desc[0].toLowerCase()}${desc.substring(1)}`;
}
return ux.colorize('gray', desc);
}
}
exports.default = BalenaHelp;
//# sourceMappingURL=help.js.map