From 1614b040d3b795a9a027bfcdc9e1b26d8422dc91 Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Sun, 4 Jul 2021 18:43:52 +0530 Subject: [PATCH] feat: install required packages for loading command automatically --- .../lib/utils/prompt-installation.js | 35 ++++++++++--------- packages/webpack-cli/lib/webpack-cli.js | 18 ++++++---- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/packages/webpack-cli/lib/utils/prompt-installation.js b/packages/webpack-cli/lib/utils/prompt-installation.js index e870019335a..713ace6dec1 100644 --- a/packages/webpack-cli/lib/utils/prompt-installation.js +++ b/packages/webpack-cli/lib/utils/prompt-installation.js @@ -5,8 +5,9 @@ const prompt = require("./prompt"); * * @param packageName * @param preMessage Message to show before the question + * @param forceInstallation Do not show prompt and force installation */ -async function promptInstallation(packageName, preMessage) { +async function promptInstallation(packageName, preMessage, forceInstallation) { const packageManager = utils.getPackageManager(); if (!packageManager) { @@ -26,21 +27,23 @@ async function promptInstallation(packageName, preMessage) { ].join(" ")}`; const { colors } = utils; - let installConfirm; - - try { - installConfirm = await prompt({ - message: `[webpack-cli] Would you like to install '${colors.green( - packageName, - )}' package? (That will run '${colors.green(commandToBeRun)}') (${colors.yellow( - "Y/n", - )})`, - defaultResponse: "Y", - stream: process.stderr, - }); - } catch (error) { - utils.logger.error(error); - process.exit(2); + let installConfirm = forceInstallation; + + if (!forceInstallation) { + try { + installConfirm = await prompt({ + message: `[webpack-cli] Would you like to install '${colors.green( + packageName, + )}' package? (That will run '${colors.green(commandToBeRun)}') (${colors.yellow( + "Y/n", + )})`, + defaultResponse: "Y", + stream: process.stderr, + }); + } catch (error) { + utils.logger.error(error); + process.exit(2); + } } if (installConfirm) { diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index 5ab16847c01..0212465c82f 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -880,13 +880,17 @@ class WebpackCLI { const { promptInstallation, colors } = this.utils; - pkg = await promptInstallation(pkg, () => { - this.logger.error( - `For using this command you need to install: '${colors.green( - pkg, - )}' package`, - ); - }); + pkg = await promptInstallation( + pkg, + () => { + this.logger.error( + `For using this command webpack-cli will need to install: '${colors.green( + pkg, + )}' package`, + ); + }, + true, + ); } let loadedCommand;