Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(webpack-cli): --version for external packages #1421

Merged
merged 5 commits into from
Apr 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/webpack-cli/lib/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async function runCLI(cli, commandIsUsed) {
cli.runHelp(process.argv);
return;
} else if (versionFlagExists) {
cli.runVersion();
cli.runVersion(commandIsUsed);
return;
}

Expand Down
29 changes: 27 additions & 2 deletions packages/webpack-cli/lib/groups/HelpGroup.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const chalk = require('chalk');
const { core, commands } = require('../utils/cli-flags');
const commandLineUsage = require('command-line-usage');

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

outputVersion() {
outputVersion(externalPkg) {
const commandsUsed = () => {
return process.argv.filter((val) => commands.find(({ name }) => name === val));
};

if (externalPkg && commandsUsed().length === 1) {
try {
if (commandsUsed().includes(externalPkg.name)) {
const { name, version } = require(`@webpack-cli/${externalPkg.name}/package.json`);
process.stdout.write(`\n${name} ${version}`);
} else {
const { name, version } = require(`${externalPkg.name}/package.json`);
process.stdout.write(`\n${name} ${version}`);
snitin315 marked this conversation as resolved.
Show resolved Hide resolved
}
} catch (e) {
console.error(chalk.red('\nError: External package not found.'));
process.exitCode = -1;
}
}

if (commandsUsed().length > 1) {
console.error(chalk.red('\nYou provided multiple commands. Please use only one command at a time.\n'));
process.exit();
}

const pkgJSON = require('../../package.json');
const webpack = require('webpack');
process.stdout.write(`\nwebpack-cli ${pkgJSON.version}`);
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack-cli/lib/utils/cli-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ module.exports = {
},
{
name: 'version',
usage: '--version',
usage: '--version | --version <external-package>',
alias: 'v',
type: Boolean,
group: BASIC_GROUP,
Expand Down
4 changes: 2 additions & 2 deletions packages/webpack-cli/lib/webpack-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,9 @@ class WebpackCLI extends GroupHelper {
return new HelpGroup().outputHelp(isCommand, subject);
}

runVersion() {
runVersion(externalPkg) {
const HelpGroup = require('./groups/HelpGroup');
return new HelpGroup().outputVersion();
return new HelpGroup().outputVersion(externalPkg);
}
}

Expand Down
43 changes: 43 additions & 0 deletions test/version/version-external-packages.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

const { run } = require('../utils/test-utils');
const initPkgJSON = require('../../packages/init/package.json');
const servePkgJSON = require('../../packages/serve/package.json');
const migratePkgJSON = require('../../packages/migrate/package.json');
const infoPkgJSON = require('../../packages/info/package.json');
const cliPkgJSON = require('../../packages/webpack-cli/package.json');

describe('version flag with external packages', () => {
it('outputs version with init', () => {
const { stdout, stderr } = run(__dirname, ['init', '--version']);
expect(stdout).toContain(initPkgJSON.version);
expect(stdout).toContain(cliPkgJSON.version);
expect(stderr).toHaveLength(0);
});

it('outputs version with info', () => {
const { stdout, stderr } = run(__dirname, ['info', '--version']);
expect(stdout).toContain(infoPkgJSON.version);
expect(stdout).toContain(cliPkgJSON.version);
expect(stderr).toHaveLength(0);
});

it('outputs version with serve', () => {
const { stdout, stderr } = run(__dirname, ['serve', '--version']);
expect(stdout).toContain(servePkgJSON.version);
expect(stdout).toContain(cliPkgJSON.version);
expect(stderr).toHaveLength(0);
});

it('outputs version with migrate', () => {
const { stdout, stderr } = run(__dirname, ['migrate', '--version']);
expect(stdout).toContain(migratePkgJSON.version);
expect(stdout).toContain(cliPkgJSON.version);
expect(stderr).toHaveLength(0);
});

it(' should throw error for multiple commands', () => {
const { stderr } = run(__dirname, ['init', 'migrate', '--version']);
expect(stderr).toContain('You provided multiple commands.');
});
});
snitin315 marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 0 additions & 6 deletions test/version/version-multi-args.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ describe('version flag with multiple arguments', () => {
expect(stderr).toHaveLength(0);
});

it('outputs version with multiple commands', () => {
const { stdout, stderr } = run(__dirname, ['version', 'init']);
expect(stdout).toContain(pkgJSON.version);
expect(stderr).toHaveLength(0);
});

it('does not output version with help command', () => {
const { stdout, stderr } = run(__dirname, ['version', 'help']);
expect(stdout).not.toContain(pkgJSON.version);
Expand Down