Skip to content

Commit 1ad71e4

Browse files
fix: show version information for plugin and loader (#1661)
1 parent d428868 commit 1ad71e4

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

packages/webpack-cli/lib/groups/HelpGroup.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const chalk = require('chalk');
22
const { core, commands } = require('../utils/cli-flags');
3+
const { defaultCommands } = require('../utils/commands');
34
const commandLineUsage = require('command-line-usage');
45

56
class HelpGroup {
@@ -49,7 +50,7 @@ class HelpGroup {
4950
if (externalPkg && commandsUsed.length === 1 && invalidArgs.length === 0) {
5051
try {
5152
if (commandsUsed.includes(externalPkg.name)) {
52-
const { name, version } = require(`@webpack-cli/${externalPkg.name}/package.json`);
53+
const { name, version } = require(`@webpack-cli/${defaultCommands[externalPkg.name]}/package.json`);
5354
process.stdout.write(`\n${name} ${version}`);
5455
} else {
5556
const { name, version } = require(`${externalPkg.name}/package.json`);

packages/webpack-cli/lib/utils/arg-parser.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
const commander = require('commander');
22
const logger = require('./logger');
33

4-
const defaultCommands = {
5-
init: 'init',
6-
loader: 'generate-loader',
7-
plugin: 'generate-plugin',
8-
info: 'info',
9-
migrate: 'migrate',
10-
serve: 'serve',
11-
};
4+
const { defaultCommands } = require('./commands');
125

136
/**
147
* Creates Argument parser corresponding to the supplied options

packages/webpack-cli/lib/utils/commands.js

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

3+
const defaultCommands = {
4+
init: 'init',
5+
loader: 'generate-loader',
6+
plugin: 'generate-plugin',
7+
info: 'info',
8+
migrate: 'migrate',
9+
serve: 'serve',
10+
};
11+
312
// Contains an array of strings with commands and their aliases that the cli supports
413
const names = commands
514
.map(({ alias, name }) => {
@@ -10,4 +19,7 @@ const names = commands
1019
})
1120
.reduce((arr, val) => arr.concat(val), []);
1221

13-
module.exports = { names };
22+
module.exports = {
23+
defaultCommands,
24+
names,
25+
};

test/version/version-external-packages.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const initPkgJSON = require('../../packages/init/package.json');
55
const servePkgJSON = require('../../packages/serve/package.json');
66
const migratePkgJSON = require('../../packages/migrate/package.json');
77
const infoPkgJSON = require('../../packages/info/package.json');
8+
const pluginPkgJSON = require('../../packages/generate-plugin/package.json');
9+
const loaderPkgJSON = require('../../packages/generate-loader/package.json');
810
const cliPkgJSON = require('../../packages/webpack-cli/package.json');
911

1012
describe('version flag with external packages', () => {
@@ -36,6 +38,20 @@ describe('version flag with external packages', () => {
3638
expect(stderr).toHaveLength(0);
3739
});
3840

41+
it('outputs version with plugin', () => {
42+
const { stdout, stderr } = run(__dirname, ['plugin', '--version'], false);
43+
expect(stdout).toContain(pluginPkgJSON.version);
44+
expect(stdout).toContain(cliPkgJSON.version);
45+
expect(stderr).toHaveLength(0);
46+
});
47+
48+
it('outputs version with loader', () => {
49+
const { stdout, stderr } = run(__dirname, ['loader', '--version'], false);
50+
expect(stdout).toContain(loaderPkgJSON.version);
51+
expect(stdout).toContain(cliPkgJSON.version);
52+
expect(stderr).toHaveLength(0);
53+
});
54+
3955
it(' should throw error for multiple commands', () => {
4056
const { stderr } = run(__dirname, ['init', 'migrate', '--version'], false);
4157
expect(stderr).toContain('You provided multiple commands.');

0 commit comments

Comments
 (0)