From 41868ba2dbd83f7fc1c0d4e858cb2a6e8c68a71b Mon Sep 17 00:00:00 2001 From: evilebottnawi Date: Sat, 21 Aug 2021 19:30:48 +0300 Subject: [PATCH] fix: configtest logic --- packages/configtest/src/index.ts | 119 ++++++++++++++++--------------- 1 file changed, 61 insertions(+), 58 deletions(-) diff --git a/packages/configtest/src/index.ts b/packages/configtest/src/index.ts index 997bfdb0186..937fa11bb55 100644 --- a/packages/configtest/src/index.ts +++ b/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 { - 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 => { - const config = await cli.resolveConfig(configPath ? { config: [configPath] } : {}); - const configPaths = new Set(); - - 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 { + 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 => { + cli.webpack = await cli.loadWebpack(); + + const config = await cli.resolveConfig(configPath ? { config: [configPath] } : {}); + const configPaths = new Set(); + + 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;