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

fix: show version information for plugin and loader subcommands #1661

Merged
merged 2 commits into from
Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion packages/webpack-cli/lib/groups/HelpGroup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const chalk = require('chalk');
const { core, commands } = require('../utils/cli-flags');
const { defaultCommands } = require('../utils/commands');
const commandLineUsage = require('command-line-usage');

class HelpGroup {
Expand Down Expand Up @@ -49,7 +50,7 @@ class HelpGroup {
if (externalPkg && commandsUsed.length === 1 && invalidArgs.length === 0) {
try {
if (commandsUsed.includes(externalPkg.name)) {
const { name, version } = require(`@webpack-cli/${externalPkg.name}/package.json`);
const { name, version } = require(`@webpack-cli/${defaultCommands[externalPkg.name]}/package.json`);
process.stdout.write(`\n${name} ${version}`);
} else {
const { name, version } = require(`${externalPkg.name}/package.json`);
Expand Down
9 changes: 1 addition & 8 deletions packages/webpack-cli/lib/utils/arg-parser.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
const commander = require('commander');
const logger = require('./logger');

const defaultCommands = {
init: 'init',
loader: 'generate-loader',
plugin: 'generate-plugin',
info: 'info',
migrate: 'migrate',
serve: 'serve',
};
const { defaultCommands } = require('./commands');

/**
* Creates Argument parser corresponding to the supplied options
Expand Down
14 changes: 13 additions & 1 deletion packages/webpack-cli/lib/utils/commands.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
const { commands } = require('./cli-flags');

const defaultCommands = {
init: 'init',
loader: 'generate-loader',
plugin: 'generate-plugin',
info: 'info',
migrate: 'migrate',
serve: 'serve',
};

// Contains an array of strings with commands and their aliases that the cli supports
const names = commands
.map(({ alias, name }) => {
Expand All @@ -10,4 +19,7 @@ const names = commands
})
.reduce((arr, val) => arr.concat(val), []);

module.exports = { names };
module.exports = {
defaultCommands,
names,
};
16 changes: 16 additions & 0 deletions test/version/version-external-packages.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ 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 pluginPkgJSON = require('../../packages/generate-plugin/package.json');
const loaderPkgJSON = require('../../packages/generate-loader/package.json');
const cliPkgJSON = require('../../packages/webpack-cli/package.json');

describe('version flag with external packages', () => {
Expand Down Expand Up @@ -36,6 +38,20 @@ describe('version flag with external packages', () => {
expect(stderr).toHaveLength(0);
});

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

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

it(' should throw error for multiple commands', () => {
const { stderr } = run(__dirname, ['init', 'migrate', '--version'], false);
expect(stderr).toContain('You provided multiple commands.');
Expand Down