Skip to content

Commit

Permalink
fix: 修正help输出格式
Browse files Browse the repository at this point in the history
  • Loading branch information
xuasir committed Jan 28, 2021
1 parent b209824 commit 45d9dec
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 30 deletions.
3 changes: 2 additions & 1 deletion packages/cli-plugin-command-help/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"url": "https://github.com/xus-code/bundle-tools/issues"
},
"dependencies": {
"@xus/cli": "^0.0.1"
"@xus/cli": "^0.0.1",
"chalk": "^4.1.0"
}
}
41 changes: 19 additions & 22 deletions packages/cli-plugin-command-help/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import type { Command, Commands, IPluginAPI, Args } from '@xus/cli'
import { info } from '@xus/cli'
import chalk from 'chalk'
import { getPadLength } from './pad'

export default function (api: IPluginAPI): void {
api.registerCommand('help', (args: Args) => {
console.log(`cmd `, args)
const commandName = args._[0]
if (!commandName) {
// log all
Expand All @@ -17,44 +16,42 @@ export default function (api: IPluginAPI): void {
}

function logAll(commads: Commands) {
info(`
usage: xus-cli <command> [options]
Commands:
`)
console.info(
`\n usage: ${chalk.green(`xus-cli <command> [options]`)}` + `\n Commands:`
)
const padLen = getPadLength(commads)
for (const name in commads) {
if (name !== 'help') {
const ops = commads[name].ops
info(`
${chalk.blue(name)}
${ops?.desc || ''}
`)
console.info(`\n ${chalk.blue(name.padEnd(padLen))}${ops?.desc || ''}`)
}
}
info(`
run ${chalk.green(`xus-cli help [command]`)}
for detail information of specific command
`)
console.info(
`\n run ${chalk.green(`xus-cli help [command]`)}` +
`\n for detail information of specific command`
)
}

function logPointCommand(name: string, commad: Command) {
if (!commad) {
info(chalk.red(`\n command "${name}" does not exist.`))
console.info(chalk.red(`\n command "${name}" does not exist.`))
} else {
const { ops } = commad
console.info(`\nCommand: ${name}`)
if (ops?.usage) {
info(`
Uasge: ${ops.usage}
`)
console.info(`\n Uasge: ${ops.usage}`)
}
if (ops?.options) {
info(`
console.info(`
Options:
`)
const padLen = getPadLength(ops.options)
for (const [flags, description] of Object.entries(ops.options)) {
info(` ${chalk.blue(flags)}: ${description}`)
console.info(
`\n ${chalk.blue(flags.padEnd(padLen))}: ${description}`
)
}
}
}
info('')
console.info('')
}
7 changes: 7 additions & 0 deletions packages/cli-plugin-command-help/src/pad.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function getPadLength(obj: Record<string, any>): number {
let longest = 10
for (const name in obj) {
if (name.length + 1 > longest) longest = name.length + 1
}
return longest
}
4 changes: 2 additions & 2 deletions packages/cli/src/Cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Cli {
async setupCli(): Promise<void> {
if (this.initialized) return
this.initialized = true
// load plugin
// 0. load plugin
this.plugins = await this.resolvePlugins()
// 1. load config
await this.ConfigManager.loadUserConfig()
Expand Down Expand Up @@ -53,7 +53,7 @@ class Cli {
}
const builtInPlugins = []
const builtInMap = ['@xus/cli-plugin-command-help']
for (const pkgName in builtInMap) {
for (const pkgName of builtInMap) {
const plugin = await toPlugin(pkgName)
if (plugin) {
builtInPlugins.push(plugin)
Expand Down
7 changes: 4 additions & 3 deletions packages/cli/src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import chalk from 'chalk'
type LoggerType = 'warn' | 'error' | 'info'

function logger(type: LoggerType = 'info') {
Expand All @@ -8,18 +9,18 @@ function logger(type: LoggerType = 'info') {

export function warn(msg: string): void {
logger('warn')(`
[xus-cli Warning]: ${msg}
${chalk.yellow(`[xus-cli Warning]`)}: ${msg}
`)
}

export function error(msg: string): void {
logger('error')(`
[xus-cli error]: ${msg}
${chalk.red(`[xus-cli error]`)}: ${msg}
`)
}

export function info(msg: string): void {
logger('info')(`
[xus-cli info]: ${msg}
${chalk.green(`[xus-cli info]`)}: ${msg}
`)
}
2 changes: 1 addition & 1 deletion packages/cli/src/utils/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ export async function loadModule<T = any>(
} catch (err) {
error = err
}
return [error, moduleContent.default || null]
return [error, moduleContent?.default || null]
}
3 changes: 2 additions & 1 deletion packages/cli/src/xus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const rawArgs = process.argv.slice(2)
const args = minimist(rawArgs)
const commandName = args._[0]
// 3. run commander
cli.run(commandName, args, rawArgs).catch(() => {
cli.run(commandName, args, rawArgs).catch((err) => {
console.log(err)
process.exit(1)
})

0 comments on commit 45d9dec

Please sign in to comment.