|
| 1 | +const { yellow, bold, underline, options } = require('colorette'); |
| 2 | +const commandLineUsage = require('command-line-usage'); |
| 3 | + |
| 4 | +const { core, commands } = require('../utils/cli-flags'); |
| 5 | +const { hasUnknownArgs, allNames, commands: commandNames } = require('../utils/unknown-args'); |
| 6 | +const logger = require('../utils/logger'); |
| 7 | + |
| 8 | +// This function prints a warning about invalid flag |
| 9 | +const printInvalidArgWarning = (args) => { |
| 10 | + const invalidArgs = hasUnknownArgs(args, allNames); |
| 11 | + if (invalidArgs.length > 0) { |
| 12 | + const argType = invalidArgs[0].startsWith('-') ? 'option' : 'command'; |
| 13 | + logger.warn(`You provided an invalid ${argType} '${invalidArgs[0]}'.`); |
| 14 | + } |
| 15 | +}; |
| 16 | + |
| 17 | +// This function is responsible for printing command/flag scoped help |
| 18 | +const printSubHelp = (subject, isCommand) => { |
| 19 | + const info = isCommand ? commands : core; |
| 20 | + // Contains object with details about given subject |
| 21 | + const options = info.find((commandOrFlag) => { |
| 22 | + if (isCommand) { |
| 23 | + return commandOrFlag.name == subject || commandOrFlag.alias == subject; |
| 24 | + } |
| 25 | + return commandOrFlag.name === subject.slice(2) || commandOrFlag.alias === subject.slice(1); |
| 26 | + }); |
| 27 | + |
| 28 | + const header = (head) => bold(underline(head)); |
| 29 | + const flagAlias = options.alias ? (isCommand ? ` ${options.alias} |` : ` -${options.alias},`) : ''; |
| 30 | + const usage = yellow(`webpack${flagAlias} ${options.usage}`); |
| 31 | + const description = options.description; |
| 32 | + const link = options.link; |
| 33 | + |
| 34 | + logger.raw(`${header('Usage')}: ${usage}`); |
| 35 | + logger.raw(`${header('Description')}: ${description}`); |
| 36 | + |
| 37 | + if (link) { |
| 38 | + logger.raw(`${header('Documentation')}: ${link}`); |
| 39 | + } |
| 40 | + |
| 41 | + if (options.flags) { |
| 42 | + const flags = commandLineUsage({ |
| 43 | + header: 'Options', |
| 44 | + optionList: options.flags, |
| 45 | + }); |
| 46 | + logger.raw(flags); |
| 47 | + } |
| 48 | +}; |
| 49 | + |
| 50 | +const printHelp = () => { |
| 51 | + const o = (s) => yellow(s); |
| 52 | + const options = require('../utils/cli-flags'); |
| 53 | + const negatedFlags = options.core |
| 54 | + .filter((flag) => flag.negative) |
| 55 | + .reduce((allFlags, flag) => { |
| 56 | + return [...allFlags, { name: `no-${flag.name}`, description: `Negates ${flag.name}`, type: Boolean }]; |
| 57 | + }, []); |
| 58 | + const title = bold('⬡ ') + underline('webpack') + bold(' ⬡'); |
| 59 | + const desc = 'The build tool for modern web applications'; |
| 60 | + const websitelink = ' ' + underline('https://webpack.js.org'); |
| 61 | + |
| 62 | + const usage = bold('Usage') + ': ' + '`' + o('webpack [...options] | <command>') + '`'; |
| 63 | + const examples = bold('Example') + ': ' + '`' + o('webpack help --flag | <command>') + '`'; |
| 64 | + |
| 65 | + const hh = ` ${title}\n |
| 66 | + ${websitelink}\n |
| 67 | + ${desc}\n |
| 68 | + ${usage}\n |
| 69 | + ${examples}\n |
| 70 | +`; |
| 71 | + return commandLineUsage([ |
| 72 | + { |
| 73 | + content: hh, |
| 74 | + raw: true, |
| 75 | + }, |
| 76 | + { |
| 77 | + header: 'Available Commands', |
| 78 | + content: options.commands.map((cmd) => { |
| 79 | + return { name: `${cmd.name} | ${cmd.alias}`, summary: cmd.description }; |
| 80 | + }), |
| 81 | + }, |
| 82 | + { |
| 83 | + header: 'Options', |
| 84 | + optionList: options.core |
| 85 | + .map((e) => { |
| 86 | + if (e.type.length > 1) e.type = e.type[0]; |
| 87 | + // Here we replace special characters with chalk's escape |
| 88 | + // syntax (`\$&`) to avoid chalk trying to re-process our input. |
| 89 | + // This is needed because chalk supports a form of `{var}` |
| 90 | + // interpolation. |
| 91 | + e.description = e.description.replace(/[{}\\]/g, '\\$&'); |
| 92 | + return e; |
| 93 | + }) |
| 94 | + .concat(negatedFlags), |
| 95 | + }, |
| 96 | + ]); |
| 97 | +}; |
| 98 | + |
| 99 | +const outputHelp = (cliArgs) => { |
| 100 | + options.enabled = !cliArgs.includes('--no-color'); |
| 101 | + printInvalidArgWarning(cliArgs); |
| 102 | + const flagOrCommandUsed = allNames.filter((name) => { |
| 103 | + return cliArgs.includes(name); |
| 104 | + })[0]; |
| 105 | + const isCommand = commandNames.includes(flagOrCommandUsed); |
| 106 | + |
| 107 | + // Print full help when no flag or command is supplied with help |
| 108 | + if (flagOrCommandUsed) { |
| 109 | + printSubHelp(flagOrCommandUsed, isCommand); |
| 110 | + } else { |
| 111 | + logger.raw(printHelp()); |
| 112 | + } |
| 113 | + logger.raw('\n Made with ♥️ by the webpack team'); |
| 114 | +}; |
| 115 | + |
| 116 | +module.exports = outputHelp; |
0 commit comments