Skip to content

Commit

Permalink
feat: support dynamic show help commands (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpselvis authored Nov 21, 2019
1 parent 1b8bcc0 commit 3ec5fab
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 20 deletions.
4 changes: 2 additions & 2 deletions packages/feflow-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
"@types/abbrev": "^1.1.0",
"@types/bunyan": "^1.8.6",
"@types/chai": "^4.2.0",
"@types/command-line-usage": "^5.0.1",
"@types/cross-spawn": "^6.0.0",
"@types/easy-table": "0.0.32",
"@types/figlet": "^1.2.0",
"@types/inquirer": "^6.0.3",
"@types/js-yaml": "^3.12.1",
"@types/meow": "^5.0.0",
"@types/minimist": "^1.2.0",
"@types/mocha": "^5.2.7",
"@types/node": "^12.0.10",
Expand All @@ -52,6 +52,7 @@
"abbrev": "^1.1.1",
"bunyan": "^1.8.12",
"chalk": "^2.4.2",
"command-line-usage": "^6.1.0",
"commander": "^2.20.0",
"cross-spawn": "^6.0.5",
"easy-table": "^1.1.1",
Expand All @@ -60,7 +61,6 @@
"inquire": "^0.4.8",
"inquirer": "^6.5.0",
"js-yaml": "^3.13.1",
"meow": "^5.0.0",
"minimist": "^1.2.0",
"osenv": "^0.1.5",
"package-json": "^6.5.0",
Expand Down
1 change: 1 addition & 0 deletions packages/feflow-cli/src/core/commander/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default class Commander {

register(name: string, desc: string, fn: Function) {
this.store[name.toLowerCase()] = fn;
this.store[name.toLowerCase()].desc = desc;
this.alias = abbrev(Object.keys(this.store));
}
};
69 changes: 51 additions & 18 deletions packages/feflow-cli/src/core/native/help.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,56 @@
import meow from 'meow';
import commandLineUsage from 'command-line-usage';

const getCommands = (store: any) => {
const arr = [];
for (const name in store ) {
const desc = store[name].desc;
arr.push({
colA: name,
colB: desc
})
}
return arr;
};

const showHelp = (commands: Array<Object>) => {

const sections = [
{
header: 'Usage',
content: '$ feflow [options] [command]'
},
{
header: 'Commands',
content: {
data: commands,
options: {
maxWidth: 60
}
}
},
{
header: 'Options',
optionList: [
{
name: 'version',
description: 'Print version and exit successfully.'
},
{
name: 'help',
description: 'Print this help and exit successfully.'
}
]
}
];
const usage = commandLineUsage(sections);

return usage;
}

module.exports = (ctx: any) => {
ctx.commander.register('help', 'Help messages', () => {
const cli = meow(`
Usage: feflow [options] [command]
Commands:
init Choose a boilerplate to initialize project.
install <plugin> Install a plugin or a yeoman generator.
uninstall <plugin> Uninstall a plugin or a yeoman generator.
lint <folder> Lint files or a folder.
dev Local development.
build Build and package.
Options:
--version, -[v] Print version and exit successfully.
--help, -[h] Print this help and exit successfully.
--list, -[l] Print all all plugins installed.
Report bugs to https://github.com/Tencent/feflow.
`);

return cli.showHelp(0);
const commands = getCommands(ctx.commander.store);
const usage = showHelp(commands);
console.log(usage);
});
};

0 comments on commit 3ec5fab

Please sign in to comment.