help.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. const { chalk } = require('@vue/cli-shared-utils')
  2. const getPadLength = require('../util/getPadLength')
  3. module.exports = (api, options) => {
  4. api.registerCommand('help', args => {
  5. const commandName = args._[0]
  6. if (!commandName) {
  7. logMainHelp()
  8. } else {
  9. logHelpForCommand(commandName, api.service.commands[commandName])
  10. }
  11. })
  12. function logMainHelp () {
  13. console.log(
  14. `\n Usage: vue-cli-service <command> [options]\n` +
  15. `\n Commands:\n`
  16. )
  17. const commands = api.service.commands
  18. const padLength = getPadLength(commands)
  19. for (const name in commands) {
  20. if (name !== 'help') {
  21. const opts = commands[name].opts || {}
  22. console.log(` ${
  23. chalk.blue(name.padEnd(padLength))
  24. }${
  25. opts.description || ''
  26. }`)
  27. }
  28. }
  29. console.log(`\n run ${
  30. chalk.green(`vue-cli-service help [command]`)
  31. } for usage of a specific command.\n`)
  32. }
  33. function logHelpForCommand (name, command) {
  34. if (!command) {
  35. console.log(chalk.red(`\n command "${name}" does not exist.`))
  36. } else {
  37. const opts = command.opts || {}
  38. if (opts.usage) {
  39. console.log(`\n Usage: ${opts.usage}`)
  40. }
  41. if (opts.options) {
  42. console.log(`\n Options:\n`)
  43. const padLength = getPadLength(opts.options)
  44. for (const [flags, description] of Object.entries(opts.options)) {
  45. console.log(` ${
  46. chalk.blue(flags.padEnd(padLength))
  47. }${
  48. description
  49. }`)
  50. }
  51. }
  52. if (opts.details) {
  53. console.log()
  54. console.log(opts.details.split('\n').map(line => ` ${line}`).join('\n'))
  55. }
  56. console.log()
  57. }
  58. }
  59. }