Skip to content

Commit 8f92cb9

Browse files
feat: help for core flags (#1141)
* feat: add core-flags to help * chore: update line order * fix: ignore help flag * chore: converted ternary to arrow function * chore: immediate call arrow * chore: verbose variable names * feat: add usage to commands * chore: update syntax for export * tests: remove output flags from child process * tests: update multiarg help tests * test: replace init arg to create * tests: update string to be tested * chore: update tests * fix: various tests are now working (#1168) * fix: various tests are now working - changed from `bundle.js` to `main.js` - applied merging strategy in the correct moment - fixed some tests * fix: merge config test * chore: update cli-flags * chore: add comment and remove info * chore: update help * chore: update help * chore: fix code quality * chore: update snapshots Co-authored-by: Emanuele <my.burning@gmail.com>
1 parent 38783b6 commit 8f92cb9

File tree

14 files changed

+129
-73
lines changed

14 files changed

+129
-73
lines changed

lib/groups/help.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,30 @@ const chalk = require('chalk');
33
const commandLineUsage = require('command-line-usage');
44

55
class HelpGroup {
6-
outputHelp(command = null) {
7-
if (command !== null) {
8-
const commands = require('../utils/commands').info;
6+
outputHelp(isCommand = true, subject) {
7+
if (subject) {
8+
const info = isCommand ? require('../utils/cli-flags').commands : require('../utils/cli-flags').core;
9+
// Contains object with details about given subject
10+
const options = info.find(commandOrFlag => {
11+
if (isCommand) {
12+
return commandOrFlag.name == subject || commandOrFlag.alias == subject;
13+
}
14+
return commandOrFlag.name === subject.slice(2) || commandOrFlag.alias === subject.slice(1);
15+
});
16+
917
const { bold, underline } = chalk.white;
1018
const header = head => bold(underline(head));
11-
12-
const options = commands.find(cmd => cmd.name === command || cmd.alias === command);
1319
const usage = chalk.keyword('orange')('webpack ' + options.usage);
1420
const description = options.description;
21+
const link = options.link;
22+
23+
process.stdout.write(`${header('Usage')}: ${usage}\n`);
24+
process.stdout.write(`${header('Description')}: ${description}\n`);
25+
26+
if (link) {
27+
process.stdout.write(`${header('Documentation')}: ${link}\n`);
28+
}
1529

16-
process.stdout.write(`${header('Usage')}: ${usage}\n${header('Description')}: ${description}\n`);
1730
if (options.flags) {
1831
const flags = commandLineUsage({
1932
header: 'Options',

lib/utils/cli-flags.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,66 +112,80 @@ module.exports = {
112112
core: [
113113
{
114114
name: 'entry',
115+
usage: '--entry <path to entry file> e.g. ./src/main.js',
115116
type: String,
116117
defaultValue: null,
117118
defaultOption: true,
118119
group: BASIC_GROUP,
119-
description: 'The entry point of your application',
120+
description: 'The entry point of your application.',
121+
link: 'https://webpack.js.org/concepts/#entry',
120122
},
121123
{
122124
name: 'config',
125+
usage: '--config <path to webpack configuration file> e.g. ./webpack.config.js',
123126
alias: 'c',
124127
type: String,
125128
defaultValue: null,
126129
group: CONFIG_GROUP,
127130
description: 'Provide path to a webpack configuration file',
131+
link: 'https://webpack.js.org/configuration/',
128132
},
129133
{
130134
name: 'merge',
135+
usage: '--merge <path to configuration to be merged> e.g. ./webpack.config.js',
131136
alias: 'm',
132137
type: String,
133138
group: CONFIG_GROUP,
134-
description: 'Merge a configuration file',
139+
description: 'Merge a configuration file using webpack-merge',
140+
link: 'https://github.com/survivejs/webpack-merge',
135141
},
136142
{
137143
name: 'progress',
144+
usage: '--progress',
138145
type: Boolean,
139146
group: BASIC_GROUP,
140147
description: 'Print compilation progress during build',
141148
},
142149
{
143150
name: 'silent',
151+
usage: '--silent',
144152
type: Boolean,
145153
group: DISPLAY_GROUP,
146154
description: 'Disable any output that webpack makes',
147155
},
148156
{
149157
name: 'help',
158+
usage: '--help',
150159
type: Boolean,
151160
group: HELP_GROUP,
152161
description: 'Outputs list of supported flags',
153162
},
154163
{
155164
name: 'defaults',
165+
usage: '--defaults',
156166
type: Boolean,
157167
group: BASIC_GROUP,
158168
description: 'Allow webpack to set defaults aggresively',
159169
},
160170
{
161171
name: 'output',
172+
usage: '--output <path to output directory> e.g. ./dist/',
162173
alias: 'o',
163174
group: OUTPUT_GROUP,
164175
type: String,
165176
description: 'Output location of the file generated by webpack',
177+
link: 'https://webpack.js.org/concepts/#output',
166178
},
167179
{
168180
name: 'plugin',
181+
usage: '--plugin',
169182
group: ADVANCED_GROUP,
170183
type: String,
171184
description: 'Load a given plugin',
172185
},
173186
{
174187
name: 'global',
188+
usage: '--global myVar ./global.js',
175189
alias: 'g',
176190
type: String,
177191
multiple: true,
@@ -184,60 +198,72 @@ module.exports = {
184198
type: String,
185199
group: ADVANCED_GROUP,
186200
description: 'Sets the build target',
201+
link: 'https://webpack.js.org/configuration/target/#target',
187202
},
188203
{
189204
name: 'watch',
205+
usage: '--watch',
190206
type: Boolean,
191207
alias: 'w',
192208
group: BASIC_GROUP,
193209
description: 'Watch for files changes',
194210
},
195211
{
196212
name: 'hot',
213+
usage: '--hot',
197214
alias: 'h',
198215
type: Boolean,
199216
group: ADVANCED_GROUP,
200217
description: 'Enables Hot Module Replacement',
201218
},
202219
{
203220
name: 'sourcemap',
221+
usage: '--sourcemap <sourcemap|eval|>',
204222
type: String,
205223
alias: 's',
206224
defaultValue: undefined,
207225
group: BASIC_GROUP,
208226
description: 'Determine source maps to use',
227+
link: 'https://webpack.js.org/configuration/devtool/#devtool',
209228
},
210229
{
211230
name: 'prefetch',
231+
usage: '--prefetch <request>',
212232
type: String,
213233
group: ADVANCED_GROUP,
214234
description: 'Prefetch this request',
215235
},
216236
{
217237
name: 'json',
238+
usage: '--json',
218239
type: Boolean,
219240
alias: 'j',
220241
description: 'Prints result as JSON',
221242
group: DISPLAY_GROUP,
222243
},
223244
{
224245
name: 'dev',
246+
usage: '--dev',
225247
alias: 'd',
226248
type: Boolean,
227249
defaultValue: undefined,
228250
group: BASIC_GROUP,
229251
description: 'Run development build',
252+
link: 'https://webpack.js.org/concepts/#mode',
230253
},
231254
{
232255
name: 'prod',
233256
alias: 'p',
257+
usage: '--prod',
234258
type: Boolean,
235259
defaultValue: undefined,
236260
group: BASIC_GROUP,
237261
description: 'Run production build',
262+
link: 'https://webpack.js.org/concepts/#mode',
238263
},
239264
{
240265
name: 'version',
266+
usage: '--version',
241267
type: Boolean,
242268
group: BASIC_GROUP,
243269
description: 'Get current version',

lib/utils/commands.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const { commands } = require('./cli-flags');
22

3+
// Contains a array of strings with commands and their alias that cli supports
34
const names = commands
45
.map(({ alias, name }) => {
56
if (alias) {
@@ -9,5 +10,4 @@ const names = commands
910
})
1011
.reduce((arr, val) => arr.concat(val), []);
1112

12-
module.exports.names = names;
13-
module.exports.info = commands;
13+
module.exports = { names };

lib/utils/core-flags.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const { core } = require('./cli-flags');
2+
3+
// Contains a array of strings with core cli flags and their alias
4+
const names = core
5+
.map(({ alias, name }) => {
6+
if (name === 'help') return [];
7+
if (alias) {
8+
return [`--${name}`, `-${alias}`];
9+
}
10+
return [`--${name}`];
11+
})
12+
.reduce((arr, val) => arr.concat(val), []);
13+
14+
module.exports = { names };

lib/webpack-cli.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,14 @@ class WebpackCLI extends GroupHelper {
164164

165165
runHelp(args) {
166166
const HelpGroup = require('./groups/help');
167-
const { names } = require('./utils/commands');
168-
const command = names.filter(name => {
167+
const commandNames = require('./utils/commands').names;
168+
const flagNames = require('./utils/core-flags').names;
169+
const allNames = [...commandNames, ...flagNames];
170+
const subject = allNames.filter(name => {
169171
return args.includes(name);
170172
})[0];
171-
return new HelpGroup().outputHelp(command);
173+
const isCommand = commandNames.includes(subject);
174+
return new HelpGroup().outputHelp(isCommand, subject);
172175
}
173176

174177
runVersion() {

packages/generate-loader/package-lock.json

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)