tiny-bin
Version:
A library for building tiny and beautiful command line apps.
43 lines (42 loc) • 1.68 kB
JavaScript
/* IMPORT */
import colors from 'tiny-colors';
import Collection from './collection.js';
import { groupBy, identity, pushBack } from '../utils.js';
/* MAIN */
class Commands extends Collection {
/* API */
print(mode) {
const commands = this.getAll();
if (!commands.length)
return;
const commandsVisible = commands.filter(command => !command.hidden);
if (!commandsVisible.length)
return;
const withoutOther = (section) => section.toLowerCase() !== 'other' ? section : '';
const commandsBySection = pushBack(groupBy(commandsVisible, command => withoutOther(command.section.toLowerCase())), '');
commandsBySection.forEach((commands, section) => {
if (!commands.length)
return;
const title = section ? `${section.toUpperCase()} COMMANDS` : (commandsBySection.size > 1 ? 'OTHER COMMANDS' : 'COMMANDS');
const table = commands.map(command => {
const withDeprecated = command.deprecated ? colors.dim : identity;
return [
[
colors.magenta(command.name),
...command.arguments.getAll().map(arg => colors.yellow(arg.name))
].join(' '),
command.description
].map(withDeprecated);
});
this.stdout.group(title, () => {
this.stdout.table(table, mode);
});
});
}
run(name, options, argv) {
const command = this.getByIdOrFail(name);
return command.run(options, argv);
}
}
/* EXPORT */
export default Commands;