diff --git a/packages/cli/package.json b/packages/cli/package.json index 1fdb44e..ba567a5 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -27,8 +27,11 @@ "url": "https://github.com/xus-code/bundle-tools/issues" }, "dependencies": { - "@types/minimist": "^1.2.1", "@xus/cli-shared-utils": "^0.0.1", + "chalk": "^4.1.0", "minimist": "^1.2.5" + }, + "devDependencies": { + "@types/minimist": "^1.2.1" } } diff --git a/packages/cli/src/builtInPlugins/commands/help.ts b/packages/cli/src/builtInPlugins/commands/help.ts index a96904e..4e1ce83 100644 --- a/packages/cli/src/builtInPlugins/commands/help.ts +++ b/packages/cli/src/builtInPlugins/commands/help.ts @@ -1,8 +1,60 @@ +import { Commands, Command } from './../../types' import { IPluginAPI } from '../../PluginAPI' +import { info } from '@xus/cli-shared-utils' +import chalk from 'chalk' export default function (api: IPluginAPI): void { api.registerCommand('help', (args) => { - console.log(`args `, args) - console.log(`all command `, Object.keys(api.commands)) + const commandName = args._[0] + if (!commandName) { + // log all + logAll(api.commands) + } else { + // log point + logPointCommand(commandName, api.commands[commandName]) + } }) } + +function logAll(commads: Commands) { + info(` + usage: xus-cli [options] + Commands: + + `) + for (const name in commads) { + if (name !== 'help') { + const ops = commads[name].ops + info(` + ${chalk.blue(name)} + ${ops?.desc || ''} + `) + } + } + info(` + run ${chalk.green(`xus-cli help [command]`)} + for detail information of specific command + `) +} + +function logPointCommand(name: string, commad: Command) { + if (!commad) { + info(chalk.red(`\n command "${name}" does not exist.`)) + } else { + const { ops } = commad + if (ops?.usage) { + info(` + Uasge: ${ops.usage} + `) + } + if (ops?.options) { + info(` + Options: + `) + for (const [flags, description] of Object.entries(ops.options)) { + info(` ${chalk.blue(flags)}: ${description}`) + } + } + } + info('') +} diff --git a/packages/cli/src/types/args.ts b/packages/cli/src/types/args.ts index 9415444..6be8813 100644 --- a/packages/cli/src/types/args.ts +++ b/packages/cli/src/types/args.ts @@ -1,6 +1,6 @@ export type Args = { _: string[] - [key: string]: boolean | string + [key: string]: any } export type RawArgs = string[] diff --git a/yarn.lock b/yarn.lock index 3dfd6f9..e8957d4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1555,7 +1555,7 @@ chalk@^2.0.0, chalk@^2.3.1, chalk@^2.4.2: escape-string-regexp "^1.0.5" supports-color "^5.3.0" -chalk@^4.0.0: +chalk@^4.0.0, chalk@^4.1.0: version "4.1.0" resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.1.0.tgz#4e14870a618d9e2edd97dd8345fd9d9dc315646a" integrity sha512-qwx12AxXe2Q5xQ43Ac//I6v5aXTipYrSESdOgzrN+9XjgEpyjpKuvSGaN4qE93f7TQTlerQQ8S+EQ0EyDoVL1A==