Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): add help command #8391 #8400

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions packages/taro-cli/src/presets/commands/help.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { IPluginContext } from '@tarojs/service'

function supplementBlank (length) {
return Array(length).map(() => '').join(' ')
}

export function printHelpLog (command, optionsList: Map<string, string>, synopsisList?: Set<string>) {
console.log(`Usage: taro ${command} [options]`)
console.log()
console.log('Options:')
const keys = Array.from(optionsList.keys())
const maxLength = keys.reduce((v1, v2) => {
return v1.length > v2.length ? v1 : v2
}).length + 3
optionsList.forEach((v, k) => {
const supplementBlankLength = maxLength - k.length
console.log(` ${k}${supplementBlank(supplementBlankLength)}${v}`)
})
if (synopsisList && synopsisList.size) {
console.log()
console.log('Synopsis:')
synopsisList.forEach(item => {
console.log(` $ ${item}`)
})
}
}

export default (ctx: IPluginContext) => {
ctx.registerCommand({
name: 'help',
synopsisList: [
'taro -h',
'taro --help'
],
async fn ({ _, options }) {
const command = ctx.ctx.commands.get(_[1])
if (!command) {
console.log(`命令 taro help ${_[1]} 中 ${_[1]} 不存在,试试 taro -h 再看看呢?`)
return
}
const defaultOptionsMap = new Map()
defaultOptionsMap.set('-h, --help', 'output usage information')
let customOptionsMap = new Map(Object.entries(options))
if (command?.optionsMap) {
customOptionsMap = new Map(Object.entries(command?.optionsMap))
}
const optionsMap = new Map([...customOptionsMap, ...defaultOptionsMap])
printHelpLog(command.name, optionsMap, command?.synopsisList ? new Set(command?.synopsisList) : new Set())
}
})
}
1 change: 1 addition & 0 deletions packages/taro-cli/src/presets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default () => {
path.resolve(__dirname, 'commands', 'convert.js'),
path.resolve(__dirname, 'commands', 'update.js'),
path.resolve(__dirname, 'commands', 'inspect.js'),
path.resolve(__dirname, 'commands', 'help.js'),

// files
path.resolve(__dirname, 'files', 'writeFileToDist.js'),
Expand Down