Skip to content

Commit

Permalink
refactor: code
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Dec 28, 2020
1 parent b28009f commit 4de138c
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 73 deletions.
4 changes: 2 additions & 2 deletions packages/generators/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import addonGenerator from './addon-generator';
import initGenerator from './init-generator';

class GeneratorsCommand {
apply(cli): void {
async apply(cli): Promise<void> {
const { logger } = cli;

cli.makeCommand(
await cli.makeCommand(
{
name: 'loader [output-path]',
alias: 'l',
Expand Down
4 changes: 2 additions & 2 deletions packages/info/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ const DEFAULT_DETAILS: Information = {
};

class InfoCommand {
apply(cli): void {
cli.makeCommand(
async apply(cli): Promise<void> {
await cli.makeCommand(
{
name: 'info',
alias: 'i',
Expand Down
4 changes: 2 additions & 2 deletions packages/init/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { initGenerator } from '@webpack-cli/generators';
import { modifyHelperUtil, npmPackagesExists } from '@webpack-cli/utils';

class InitCommand {
apply(cli): void {
cli.makeCommand(
async apply(cli): Promise<void> {
await cli.makeCommand(
{
name: 'init [scaffold...]',
alias: 'c',
Expand Down
4 changes: 2 additions & 2 deletions packages/migrate/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ function runMigration(currentConfigPath: string, outputConfigPath: string, logge
}

class MigrationCommand {
apply(cli): void {
async apply(cli): Promise<void> {
const { logger } = cli;

cli.makeCommand(
await cli.makeCommand(
{
name: 'migrate <config-path> [new-config-path]',
alias: 'm',
Expand Down
5 changes: 3 additions & 2 deletions packages/serve/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class ServeCommand {
async apply(cli): Promise<void> {
const { logger } = cli;

cli.makeCommand(
await cli.makeCommand(
{
name: 'serve',
alias: 's',
Expand All @@ -20,7 +20,8 @@ class ServeCommand {
// eslint-disable-next-line
devServerFlags = require('webpack-dev-server/bin/cli-flags').devServer;
} catch (error) {
return [];
logger.error(`You need to install 'webpack-dev-server' for running 'webpack serve'.\n${error}`);
process.exit(2);
}

const builtInOptions = cli.getBuiltInOptions();
Expand Down
10 changes: 5 additions & 5 deletions packages/webpack-cli/lib/utils/prompt-installation.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ async function promptInstallation(packageName, preMessage) {
process.exit(2);
}

// yarn uses 'add' command, rest npm and pnpm both use 'install'
const options = [packageManager === 'yarn' ? 'add' : 'install', '-D', packageName];

const commandToBeRun = `${packageManager} ${options.join(' ')}`;

if (preMessage) {
preMessage();
}

// yarn uses 'add' command, rest npm and pnpm both use 'install'
const commandToBeRun = `${packageManager} ${[packageManager === 'yarn' ? 'add' : 'install', '-D', packageName].join(
' ',
)} --ignore-workspace-root-check`;

let installConfirm;

try {
Expand Down
72 changes: 44 additions & 28 deletions packages/webpack-cli/lib/webpack-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class WebpackCLI {
this.utils = { toKebabCase, getPkg, promptInstallation };
}

makeCommand(commandOptions, options, action) {
const command = program.command(commandOptions.name, {
async makeCommand(commandOptions, options, action) {
const command = this.program.command(commandOptions.name, {
noHelp: commandOptions.noHelp,
hidden: commandOptions.hidden,
isDefault: commandOptions.isDefault,
Expand All @@ -56,41 +56,55 @@ class WebpackCLI {
command.pkg = 'webpack-cli';
}

const { forHelp } = this.program;

let allDependenciesInstalled = true;

if (commandOptions.dependencies && commandOptions.dependencies.length > 0) {
for (const dependency of commandOptions.dependencies) {
const isPkgExist = getPkg(dependency);

if (isPkgExist) {
continue;
} else if (!isPkgExist && forHelp) {
allDependenciesInstalled = false;
continue;
}

try {
await promptInstallation(dependency, () => {
logger.error(
`For using '${green(commandOptions.name)}' command you need to install: '${green(dependency)}' package`,
);
});
} catch (error) {
logger.error("Action Interrupted, use 'webpack-cli help' to see possible commands.");
logger.error(error);
process.exit(2);
}
}
}

if (options) {
if (typeof options === 'function') {
options = options();
if (forHelp && !allDependenciesInstalled) {
command.description(
`${commandOptions.description} To see all available options you need to install ${commandOptions.dependencies
.map((dependency) => `'${dependency}'`)
.join(',')}.`,
);
options = [];
} else {
options = options();
}
}

options.forEach((optionForCommand) => {
this.makeOption(command, optionForCommand);
});
}

command.action(async (...args) => {
if (commandOptions.dependencies && commandOptions.dependencies.length > 0) {
for (const dependency of commandOptions.dependencies) {
const isPkgExist = getPkg(dependency);

if (isPkgExist) {
continue;
}

try {
await promptInstallation(dependency, () => {
logger.error(
`For using '${green(commandOptions.name)}' command you need to install: '${green(dependency)}' package`,
);
});
} catch (error) {
logger.error("Action Interrupted, use 'webpack-cli help' to see possible commands.");
logger.error(error);
process.exit(2);
}
}
}

await action(...args);
});
command.action(action);

return command;
}
Expand Down Expand Up @@ -654,6 +668,8 @@ class WebpackCLI {
}
}

this.program.forHelp = true;

const optionsForHelp = [].concat(opts.help && !isDefault ? [commandName] : []).concat(options);

await outputHelp(optionsForHelp, isVerbose, program);
Expand Down
18 changes: 0 additions & 18 deletions test/optimization/optimization.test.js

This file was deleted.

1 change: 0 additions & 1 deletion test/optimization/src/index.js

This file was deleted.

8 changes: 0 additions & 8 deletions test/optimization/webpack.config.js

This file was deleted.

16 changes: 13 additions & 3 deletions test/utils/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,22 @@ const { Writable } = require('readable-stream');
const concat = require('concat-stream');
const { version } = require('webpack');
const stripAnsi = require('strip-ansi');
const { version: devServerVersion } = require('webpack-dev-server/package.json');

const isWebpack5 = version.startsWith('5');

let devServerVersion;

try {
// eslint-disable-next-line
devServerVersion = require('webpack-dev-server/package.json').version;
} catch (error) {
// Nothing
}

const isDevServer4 = devServerVersion && devServerVersion.startsWith('4');

const WEBPACK_PATH = path.resolve(__dirname, '../../packages/webpack-cli/bin/cli.js');
const ENABLE_LOG_COMPILATION = process.env.ENABLE_PIPE || false;
const isWebpack5 = version.startsWith('5');
const isDevServer4 = devServerVersion.startsWith('4');
const isWindows = process.platform === 'win32';

const hyphenToUpperCase = (name) => {
Expand Down

0 comments on commit 4de138c

Please sign in to comment.