Skip to content

Commit 756a8ff

Browse files
feat(webpack-cli): --version for external packages (#1421)
* feat(webpack-cli): --version for external packages * feat: handle multiple commands * chore: refactor Co-authored-by: James George <jamesgeorge998001@gmail.com>
1 parent f2de342 commit 756a8ff

File tree

6 files changed

+74
-12
lines changed

6 files changed

+74
-12
lines changed

packages/webpack-cli/lib/bootstrap.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ async function runCLI(cli, commandIsUsed) {
5151
cli.runHelp(process.argv);
5252
return;
5353
} else if (versionFlagExists) {
54-
cli.runVersion();
54+
cli.runVersion(commandIsUsed);
5555
return;
5656
}
5757

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
const chalk = require('chalk');
2+
const { core, commands } = require('../utils/cli-flags');
23
const commandLineUsage = require('command-line-usage');
34

45
class HelpGroup {
56
outputHelp(isCommand = true, subject) {
67
if (subject) {
7-
const info = isCommand ? require('../utils/cli-flags').commands : require('../utils/cli-flags').core;
8+
const info = isCommand ? commands : core;
89
// Contains object with details about given subject
910
const options = info.find((commandOrFlag) => {
1011
if (isCommand) {
@@ -39,7 +40,31 @@ class HelpGroup {
3940
process.stdout.write('\n Made with ♥️ by the webpack team \n');
4041
}
4142

42-
outputVersion() {
43+
outputVersion(externalPkg) {
44+
const commandsUsed = () => {
45+
return process.argv.filter((val) => commands.find(({ name }) => name === val));
46+
};
47+
48+
if (externalPkg && commandsUsed().length === 1) {
49+
try {
50+
if (commandsUsed().includes(externalPkg.name)) {
51+
const { name, version } = require(`@webpack-cli/${externalPkg.name}/package.json`);
52+
process.stdout.write(`\n${name} ${version}`);
53+
} else {
54+
const { name, version } = require(`${externalPkg.name}/package.json`);
55+
process.stdout.write(`\n${name} ${version}`);
56+
}
57+
} catch (e) {
58+
console.error(chalk.red('\nError: External package not found.'));
59+
process.exitCode = -1;
60+
}
61+
}
62+
63+
if (commandsUsed().length > 1) {
64+
console.error(chalk.red('\nYou provided multiple commands. Please use only one command at a time.\n'));
65+
process.exit();
66+
}
67+
4368
const pkgJSON = require('../../package.json');
4469
const webpack = require('webpack');
4570
process.stdout.write(`\nwebpack-cli ${pkgJSON.version}`);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ module.exports = {
254254
},
255255
{
256256
name: 'version',
257-
usage: '--version',
257+
usage: '--version | --version <external-package>',
258258
alias: 'v',
259259
type: Boolean,
260260
group: BASIC_GROUP,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,9 @@ class WebpackCLI extends GroupHelper {
312312
return new HelpGroup().outputHelp(isCommand, subject);
313313
}
314314

315-
runVersion() {
315+
runVersion(externalPkg) {
316316
const HelpGroup = require('./groups/HelpGroup');
317-
return new HelpGroup().outputVersion();
317+
return new HelpGroup().outputVersion(externalPkg);
318318
}
319319
}
320320

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
3+
const { run } = require('../utils/test-utils');
4+
const initPkgJSON = require('../../packages/init/package.json');
5+
const servePkgJSON = require('../../packages/serve/package.json');
6+
const migratePkgJSON = require('../../packages/migrate/package.json');
7+
const infoPkgJSON = require('../../packages/info/package.json');
8+
const cliPkgJSON = require('../../packages/webpack-cli/package.json');
9+
10+
describe('version flag with external packages', () => {
11+
it('outputs version with init', () => {
12+
const { stdout, stderr } = run(__dirname, ['init', '--version']);
13+
expect(stdout).toContain(initPkgJSON.version);
14+
expect(stdout).toContain(cliPkgJSON.version);
15+
expect(stderr).toHaveLength(0);
16+
});
17+
18+
it('outputs version with info', () => {
19+
const { stdout, stderr } = run(__dirname, ['info', '--version']);
20+
expect(stdout).toContain(infoPkgJSON.version);
21+
expect(stdout).toContain(cliPkgJSON.version);
22+
expect(stderr).toHaveLength(0);
23+
});
24+
25+
it('outputs version with serve', () => {
26+
const { stdout, stderr } = run(__dirname, ['serve', '--version']);
27+
expect(stdout).toContain(servePkgJSON.version);
28+
expect(stdout).toContain(cliPkgJSON.version);
29+
expect(stderr).toHaveLength(0);
30+
});
31+
32+
it('outputs version with migrate', () => {
33+
const { stdout, stderr } = run(__dirname, ['migrate', '--version']);
34+
expect(stdout).toContain(migratePkgJSON.version);
35+
expect(stdout).toContain(cliPkgJSON.version);
36+
expect(stderr).toHaveLength(0);
37+
});
38+
39+
it(' should throw error for multiple commands', () => {
40+
const { stderr } = run(__dirname, ['init', 'migrate', '--version']);
41+
expect(stderr).toContain('You provided multiple commands.');
42+
});
43+
});

test/version/version-multi-args.test.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ describe('version flag with multiple arguments', () => {
1010
expect(stderr).toHaveLength(0);
1111
});
1212

13-
it('outputs version with multiple commands', () => {
14-
const { stdout, stderr } = run(__dirname, ['version', 'init']);
15-
expect(stdout).toContain(pkgJSON.version);
16-
expect(stderr).toHaveLength(0);
17-
});
18-
1913
it('does not output version with help command', () => {
2014
const { stdout, stderr } = run(__dirname, ['version', 'help']);
2115
expect(stdout).not.toContain(pkgJSON.version);

0 commit comments

Comments
 (0)