(parentName = null, includeDescriptions = true)
| 23 | } |
| 24 | |
| 25 | static getUsage (parentName = null, includeDescriptions = true) { |
| 26 | const { aliases: cmdAliases } = require('./utils/cmd-list') |
| 27 | const seenExclusive = new Set() |
| 28 | const wrapWidth = 80 |
| 29 | const { description, usage = [''], name } = this |
| 30 | |
| 31 | // Resolve to a definitions array: if the command has its own definitions, use those directly; otherwise resolve params from the global definitions pool. |
| 32 | let cmdDefs |
| 33 | if (this.definitions) { |
| 34 | cmdDefs = this.definitions |
| 35 | } else if (this.params) { |
| 36 | cmdDefs = this.params.map(p => definitions[p]).filter(Boolean) |
| 37 | } |
| 38 | |
| 39 | // If this is a subcommand, prepend parent name |
| 40 | const fullCommandName = parentName ? `${parentName} ${name}` : name |
| 41 | |
| 42 | const fullUsage = [ |
| 43 | `${description}`, |
| 44 | '', |
| 45 | 'Usage:', |
| 46 | ] |
| 47 | if (usage) { |
| 48 | fullUsage.push(...usage.map(u => `npm ${fullCommandName} ${u}`.trim())) |
| 49 | } |
| 50 | |
| 51 | if (this.subcommands) { |
| 52 | for (const sub in this.subcommands) { |
| 53 | fullUsage.push(`npm ${fullCommandName} ${sub} ${this.subcommands[sub].usage}`) |
| 54 | } |
| 55 | fullUsage.push('') |
| 56 | fullUsage.push('Subcommands:') |
| 57 | const subcommandEntries = Object.entries(this.subcommands) |
| 58 | for (let i = 0; i < subcommandEntries.length; i++) { |
| 59 | const [subName, SubCommand] = subcommandEntries[i] |
| 60 | fullUsage.push(` ${subName}`) |
| 61 | if (SubCommand.description) { |
| 62 | fullUsage.push(` ${SubCommand.description}`) |
| 63 | } |
| 64 | // Add space between subcommands except after the last one |
| 65 | if (i < subcommandEntries.length - 1) { |
| 66 | fullUsage.push('') |
| 67 | } |
| 68 | } |
| 69 | fullUsage.push('') |
| 70 | fullUsage.push(`Run "npm ${name} <subcommand> --help" for more info on a subcommand.`) |
| 71 | } |
| 72 | |
| 73 | if (cmdDefs) { |
| 74 | let results = '' |
| 75 | let line = '' |
| 76 | for (const def of cmdDefs) { |
| 77 | /* istanbul ignore next */ |
| 78 | if (seenExclusive.has(def.key)) { |
| 79 | continue |
| 80 | } |
| 81 | let paramUsage = def.usage |
| 82 | if (def.exclusive) { |
no test coverage detected