balena-cli
Version:
The official balena Command Line Interface
167 lines • 6.08 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.manuallySortedPrimaryCommands = [
'login',
'push',
'fleet',
'device',
'preload',
'build',
'deploy',
'join',
'leave',
];
}
async showHelp(argv) {
const chalk = (0, lazy_1.getChalk)();
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(`${chalk.yellow(subject)} commands include:`);
console.log(this.formatCommands(topicCommands));
console.log(`\nRun ${chalk.cyan.bold('balena help -v')} for a list of all available commands,`);
console.log(` or ${chalk.cyan.bold('balena help <command>')} for detailed help on a specific command.`);
return;
}
console.log(`command ${chalk.cyan.bold(subject)} not found`);
}
getCustomRootHelp(showAllCommands) {
const { bold, cyan } = (0, lazy_1.getChalk)();
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 = [
bold('\nADDITIONAL COMMANDS'),
this.formatCommands(additionalCommands),
];
}
else {
const cmd = cyan.bold('balena help --verbose');
additionalCmdSection = [
`\n${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 [
bold('USAGE'),
'$ balena [COMMAND] [OPTIONS]',
bold('\nPRIMARY COMMANDS'),
this.formatCommands(primaryCommands),
...additionalCmdSection,
bold('\nGLOBAL OPTIONS'),
this.formatGlobalOpts(globalOps),
bold('\nDeprecation Policy Reminder'),
deprecationPolicyNote,
reachingOut,
].join('\n');
}
formatGlobalOpts(opts) {
const { dim } = (0, lazy_1.getChalk)();
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)}${dim(descriptionLines[0])}`);
outLines.push(...descriptionLines
.slice(1)
.map((line) => ` ${' '.repeat(flagWidth + 2)}${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,
});
return indent(body, 2);
}
formatDescription(desc = '') {
var _a;
const chalk = (0, lazy_1.getChalk)();
desc = desc.split('\n')[0];
if (desc[desc.length - 1] === '.') {
desc = desc.substring(0, desc.length - 1);
}
if (desc[1] === ((_a = desc[1]) === null || _a === void 0 ? void 0 : _a.toLowerCase())) {
desc = `${desc[0].toLowerCase()}${desc.substring(1)}`;
}
return chalk.grey(desc);
}
}
exports.default = BalenaHelp;
//# sourceMappingURL=help.js.map
;