cli-meow-help
Version:
Generate automatically formatted help text for `meow` CLI helper
137 lines (115 loc) • 3.06 kB
TypeScript
import chalk from 'chalk';
import chalkTemplate from 'chalk-template';
import Table from 'cli-table3';
var createTable = () => {
return new Table({
chars: {
top: '',
'top-mid': '',
'top-left': '',
'top-right': '',
bottom: '',
'bottom-mid': '',
'bottom-left': '',
'bottom-right': '',
left: '',
'left-mid': '',
mid: '',
'mid-mid': '',
right: '',
'right-mid': '',
middle: ''
},
style: {
'padding-left': 0,
'padding-right': 2
},
wordWrap: true
});
};
let defaultValue = ``;
const defaultText = `Default:`;
var getDefaultValue = (defaults, options) => {
if (defaults && options.default) {
defaultValue = `${defaultText} ${chalk.yellow(`${options.default}`)}`;
} else if (defaults && options.type === `boolean`) {
defaultValue = `${defaultText} ${chalk.yellow(`false`)}`;
}
return defaultValue;
};
/**
* Cli `meow` Helper.
*
* Generate automatically formatted help text for `meow` CLI helper
*
* @autor Awais <https://twitter.com/MrAhmadAwais/>
*/
const dim = chalk.dim;
const greenInverse = chalk.bold.inverse.green;
const cyanInverse = chalk.bold.inverse.cyan;
const yellowInverse = chalk.bold.inverse.yellow;
var index = ({
name = `(CLI name undefined)`,
desc,
commands = {},
flags = {},
examples = [],
defaults = true,
header,
footer
}) => {
let help = '';
const spacer = `\n\n`;
if (header) {
help += `${header}${spacer}`;
}
if (desc) {
help += `${desc}${spacer}`;
}
// Usage.
help += `${greenInverse(` USAGE `)} ${spacer}`;
help += chalkTemplate`{gray $} {green ${name}} {cyan <command>} {yellow [option]}`;
let isPlural;
if (examples.length) {
isPlural = examples.length > 1 ? `S` : ``;
help += `${spacer}${chalkTemplate`{gray EXAMPLE${isPlural} }`}`;
examples.map(ex => {
const exFlags = ex.flags ? `--${ex.flags.join(` --`)}` : ``;
help += chalkTemplate`\n{gray $} {green ${name}} {cyan ${ex.command}} {yellow ${exFlags}}`;
});
}
// Commands.
help += `${spacer}${cyanInverse(` COMMANDS `)} ${spacer}`;
const tableCommands = createTable();
const commandKeys = Object.keys(commands);
for (const command of commandKeys) {
let options = commands[command];
const defaultValue = getDefaultValue(defaults, options);
tableCommands.push([
chalkTemplate`{cyan ${command}}`,
`${options.desc} ${dim(defaultValue)}`
]);
}
help += tableCommands.toString();
// Flags.
help += `${spacer}${yellowInverse(` OPTIONS `)} ${spacer}`;
const tableFlags = createTable();
const flagKeys = Object.keys(flags);
for (const flag of flagKeys) {
let options = flags[flag];
let alias = options.alias ? `-${options.alias}, ` : ``;
let shortFlag = options.shortFlag ? `-${options.shortFlag}, ` : ``;
const defaultValue = getDefaultValue(defaults, options);
tableFlags.push([
chalkTemplate`{yellow ${alias}${shortFlag}--${flag}}`,
`${options.desc} ${dim(defaultValue)}`
]);
}
help += tableFlags.toString();
help += `\n`;
if (footer) {
help += `\n${footer}\n`;
}
return help;
};
export { index as default };