Skip to content

Commit

Permalink
fix: configtest logic
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Aug 24, 2021
1 parent d141479 commit 41868ba
Showing 1 changed file with 61 additions and 58 deletions.
119 changes: 61 additions & 58 deletions packages/configtest/src/index.ts
@@ -1,62 +1,65 @@
class ConfigTestCommand {
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
async apply(cli: any): Promise<void> {
await cli.makeCommand(
{
name: "configtest [config-path]",
alias: "t",
description: "Validate a webpack configuration.",
pkg: "@webpack-cli/configtest",
dependencies: ["webpack"],
},
[],
async (configPath: string | undefined): Promise<void> => {
const config = await cli.resolveConfig(configPath ? { config: [configPath] } : {});
const configPaths = new Set<string>();

if (Array.isArray(config.options)) {
config.options.forEach((options) => {
if (config.path.get(options)) {
configPaths.add(config.path.get(options));
}
});
} else {
if (config.path.get(config.options)) {
configPaths.add(config.path.get(config.options));
}
}

if (configPaths.size === 0) {
cli.logger.error("No configuration found.");
process.exit(2);
}

cli.logger.info(`Validate '${Array.from(configPaths).join(" ,")}'.`);

const webpack = await cli.loadWebpack();

try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const error: any = webpack.validate(config.options);

// TODO remove this after drop webpack@4
if (error && error.length > 0) {
throw new webpack.WebpackOptionsValidationError(error);
}
} catch (error) {
if (cli.isValidationError(error)) {
cli.logger.error(error.message);
} else {
cli.logger.error(error);
}

process.exit(2);
}

cli.logger.success("There are no validation errors in the given webpack configuration.");
},
);
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
async apply(cli: any): Promise<void> {
await cli.makeCommand(
{
name: "configtest [config-path]",
alias: "t",
description: "Validate a webpack configuration.",
pkg: "@webpack-cli/configtest",
dependencies: ["webpack"],
},
[],
async (configPath: string | undefined): Promise<void> => {
cli.webpack = await cli.loadWebpack();

const config = await cli.resolveConfig(configPath ? { config: [configPath] } : {});
const configPaths = new Set<string>();

if (Array.isArray(config.options)) {
config.options.forEach((options) => {
if (config.path.get(options)) {
configPaths.add(config.path.get(options));
}
});
} else {
if (config.path.get(config.options)) {
configPaths.add(config.path.get(config.options));
}
}

if (configPaths.size === 0) {
cli.logger.error("No configuration found.");
process.exit(2);
}

cli.logger.info(`Validate '${Array.from(configPaths).join(" ,")}'.`);

try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const error: any = cli.webpack.validate(config.options);

// TODO remove this after drop webpack@4
if (error && error.length > 0) {
throw new cli.webpack.WebpackOptionsValidationError(error);
}
} catch (error) {
if (error instanceof cli.webpack.ValidationError ||
error.name === "ValidationError") {
cli.logger.error(error.message);
} else {
cli.logger.error(error);
}

process.exit(2);
}

cli.logger.success(
"There are no validation errors in the given webpack configuration.",
);
},
);
}
}

export default ConfigTestCommand;

0 comments on commit 41868ba

Please sign in to comment.