Skip to content

Commit

Permalink
feat: help指令开发
Browse files Browse the repository at this point in the history
  • Loading branch information
xuasir committed Jan 28, 2021
1 parent 4bac795 commit 488a15a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
5 changes: 4 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
56 changes: 54 additions & 2 deletions packages/cli/src/builtInPlugins/commands/help.ts
Original file line number Diff line number Diff line change
@@ -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 <command> [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('')
}
2 changes: 1 addition & 1 deletion packages/cli/src/types/args.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export type Args = {
_: string[]
[key: string]: boolean | string
[key: string]: any
}

export type RawArgs = string[]
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==
Expand Down

0 comments on commit 488a15a

Please sign in to comment.