/
runHelp.js
138 lines (117 loc) · 5.38 KB
/
runHelp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const { options, green, bold, underline } = require('colorette');
const commandLineUsage = require('command-line-usage');
const { commands, flags } = require('../utils/cli-flags');
const logger = require('../utils/logger');
const outputHelp = (args) => {
if (args.includes('--color')) {
options.enabled = true;
} else if (args.includes('--no-color')) {
options.enabled = false;
}
const hasUnknownVersionArgs = (args, commands, flags) => {
return args.filter((arg) => {
if (arg === 'version' || arg === 'help' || arg === '--help' || arg === '-h' || arg === '--no-color') {
return false;
}
const foundCommand = commands.find((command) => {
return command.name === arg || command.alias === arg;
});
const foundFlag = flags.find((command) => {
return `--${command.name}` === arg || `-${command.alias}` === arg;
});
return !foundCommand && !foundFlag;
});
};
const invalidArgs = hasUnknownVersionArgs(args, commands, flags);
if (invalidArgs.length > 0) {
const argType = invalidArgs[0].startsWith('-') ? 'option' : 'command';
logger.error(`Invalid ${argType} '${invalidArgs[0]}'.`);
logger.error('Run webpack --help to see available commands and arguments.');
process.exit(2);
}
const usedCommand = commands.filter((command) => {
return args.includes(command.name) || args.includes(command.alias);
});
const usedFlag = flags.filter((flag) => {
if (flag.name === 'help' || flag.name === 'color') {
return false;
}
return args.includes(`--${flag.name}`) || args.includes(`-${flag.alias}`);
});
const usedCommandOrFlag = [].concat(usedCommand).concat(usedFlag);
if (usedCommandOrFlag.length > 1) {
logger.error(
`You provided multiple commands or arguments - ${usedCommandOrFlag
.map((usedFlagOrCommand) => {
const isCommand = usedFlagOrCommand.packageName;
return `${isCommand ? 'command ' : 'argument '}'${isCommand ? usedFlagOrCommand.name : `--${usedFlagOrCommand.name}`}'${
usedFlagOrCommand.alias ? ` (alias '${isCommand ? usedFlagOrCommand.alias : `-${usedFlagOrCommand.alias}`}')` : ''
}`;
})
.join(', ')}. Please use only one command at a time.`,
);
process.exit(2);
}
// Print full help when no flag or command is supplied with help
if (usedCommandOrFlag.length === 1) {
const [item] = usedCommandOrFlag;
const isCommand = item.packageName;
const header = (head) => bold(underline(head));
const flagAlias = item.alias ? (isCommand ? ` ${item.alias} |` : ` -${item.alias},`) : '';
const usage = green(`webpack${flagAlias} ${item.usage}`);
const description = item.description;
const link = item.link;
logger.raw(`${header('Usage')}: ${usage}`);
logger.raw(`${header('Description')}: ${description}`);
if (link) {
logger.raw(`${header('Documentation')}: ${link}`);
}
if (item.flags) {
const flags = commandLineUsage({
header: 'Options',
optionList: item.flags,
});
logger.raw(flags);
}
} else {
const negatedFlags = flags
.filter((flag) => flag.negative)
.reduce((allFlags, flag) => {
return [...allFlags, { name: `no-${flag.name}`, description: `Negates ${flag.name}`, type: Boolean }];
}, []);
const title = bold('⬡ ') + underline('webpack') + bold(' ⬡');
const desc = 'The build tool for modern web applications';
const websitelink = ' ' + underline('https://webpack.js.org');
const usage = bold('Usage') + ': ' + '`' + green('webpack [...options] | <command>') + '`';
const examples = bold('Example') + ': ' + '`' + green('webpack help --flag | <command>') + '`';
const hh = ` ${title}\n\n${websitelink}\n\n${desc}\n\n${usage}\n${examples}`;
const output = commandLineUsage([
{ content: hh, raw: true },
{
header: 'Available Commands',
content: commands.map((cmd) => {
return { name: `${cmd.name} | ${cmd.alias}`, summary: cmd.description };
}),
},
{
header: 'Options',
optionList: flags
.map((e) => {
if (e.type.length > 1) {
e.type = e.type[0];
}
// Here we replace special characters with chalk's escape
// syntax (`\$&`) to avoid chalk trying to re-process our input.
// This is needed because chalk supports a form of `{var}`
// interpolation.
e.description = e.description.replace(/[{}\\]/g, '\\$&');
return e;
})
.concat(negatedFlags),
},
]);
logger.raw(output);
}
logger.raw(' Made with ♥️ by the webpack team');
};
module.exports = outputHelp;