Skip to content

Commit

Permalink
feat: install required packages for loading command automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Jul 4, 2021
1 parent 45d1fea commit 1614b04
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
35 changes: 19 additions & 16 deletions packages/webpack-cli/lib/utils/prompt-installation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
18 changes: 11 additions & 7 deletions packages/webpack-cli/lib/webpack-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 1614b04

Please sign in to comment.