From 54116ae8add051493af7a73b648361c01ae43165 Mon Sep 17 00:00:00 2001 From: Anshuman Verma Date: Tue, 22 Sep 2020 20:12:56 +0530 Subject: [PATCH] refactor: add utils package to webpack-cli --- packages/utils/package.json | 1 + packages/utils/src/index.ts | 2 ++ packages/utils/src/package-exists.ts | 8 +++++ packages/utils/src/prompt-installation.ts | 34 +++++++++++++++++++ packages/utils/src/run-command.ts | 12 +++++++ .../lib/commands/ExternalCommand.js | 2 +- packages/webpack-cli/package.json | 1 + 7 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 packages/utils/src/package-exists.ts create mode 100644 packages/utils/src/prompt-installation.ts create mode 100644 packages/utils/src/run-command.ts diff --git a/packages/utils/package.json b/packages/utils/package.json index 6a908653aa6..a382febf9da 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -11,6 +11,7 @@ "dependencies": { "colorette": "^1.2.1", "cross-spawn": "^7.0.3", + "enquirer": "^2.3.6", "execa": "^4.0.0", "findup-sync": "4.0.0", "global-modules": "^2.0.0", diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index 08964bfe208..46145db13f5 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -11,3 +11,5 @@ export * from './validate-identifier'; export * from './prop-types'; export * from './global-packages-path'; export * from './get-package-manager'; +export * from './package-exists'; +export * from './run-command'; diff --git a/packages/utils/src/package-exists.ts b/packages/utils/src/package-exists.ts new file mode 100644 index 00000000000..303f61de4b5 --- /dev/null +++ b/packages/utils/src/package-exists.ts @@ -0,0 +1,8 @@ +export function packageExists(packageName: string): boolean { + try { + require(packageName); + return true; + } catch (err) { + return false; + } +} diff --git a/packages/utils/src/prompt-installation.ts b/packages/utils/src/prompt-installation.ts new file mode 100644 index 00000000000..601f3f74e66 --- /dev/null +++ b/packages/utils/src/prompt-installation.ts @@ -0,0 +1,34 @@ +import { prompt } from 'enquirer'; +import { green } from 'colorette'; +import { runCommand } from './run-command'; + +/** + * + * @param packageName + * @param preMessage Message to show before the question + */ +// eslint-disable-next-line @typescript-eslint/explicit-function-return-type +export async function promptInstallation(packageName: string, preMessage?: Function) { + const packageManager = exports.getPackageManager(); + const options = [packageManager === 'yarn' ? 'add' : 'install', '-D', packageName]; + + const commandToBeRun = `${packageManager} ${options.join(' ')}`; + if (preMessage) { + preMessage(); + } + const question = `Would you like to install ${packageName}? (That will run ${green(commandToBeRun)})`; + const { installConfirm } = await prompt([ + { + type: 'confirm', + name: 'installConfirm', + message: question, + initial: 'Y', + }, + ]); + if (installConfirm) { + await runCommand(commandToBeRun); + return exports.packageExists(packageName); + } + // eslint-disable-next-line require-atomic-updates + process.exitCode = 2; +} diff --git a/packages/utils/src/run-command.ts b/packages/utils/src/run-command.ts new file mode 100644 index 00000000000..96ff7f48a1e --- /dev/null +++ b/packages/utils/src/run-command.ts @@ -0,0 +1,12 @@ +import execa from 'execa'; + +export async function runCommand(command, args = []): Promise { + try { + await execa(command, args, { + stdio: 'inherit', + shell: true, + }); + } catch (e) { + throw new Error(e); + } +} diff --git a/packages/webpack-cli/lib/commands/ExternalCommand.js b/packages/webpack-cli/lib/commands/ExternalCommand.js index afa87e6e862..0b518f911c0 100644 --- a/packages/webpack-cli/lib/commands/ExternalCommand.js +++ b/packages/webpack-cli/lib/commands/ExternalCommand.js @@ -1,7 +1,7 @@ const { yellow, cyan } = require('colorette'); const logger = require('../utils/logger'); const execa = require('execa'); -const { packageExists, promptInstallation } = require('@webpack-cli/package-utils'); +const { packageExists, promptInstallation } = require('@webpack-cli/utils'); const packagePrefix = '@webpack-cli'; diff --git a/packages/webpack-cli/package.json b/packages/webpack-cli/package.json index 2b01d65c0b7..3427446c6eb 100644 --- a/packages/webpack-cli/package.json +++ b/packages/webpack-cli/package.json @@ -26,6 +26,7 @@ "@webpack-cli/info": "^1.0.1-alpha.4", "@webpack-cli/init": "^1.0.1-alpha.5", "@webpack-cli/serve": "^1.0.1-alpha.5", + "@webpack-cli/utils": "^1.0.1-alpha.5", "ansi-escapes": "^4.3.1", "colorette": "^1.2.1", "command-line-usage": "^6.1.0",