Skip to content

Commit

Permalink
refactor: add utils package to webpack-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
anshumanv committed Sep 22, 2020
1 parent 23f3a44 commit 54116ae
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
8 changes: 8 additions & 0 deletions packages/utils/src/package-exists.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function packageExists(packageName: string): boolean {
try {
require(packageName);
return true;
} catch (err) {
return false;
}
}
34 changes: 34 additions & 0 deletions packages/utils/src/prompt-installation.ts
Original file line number Diff line number Diff line change
@@ -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;
}
12 changes: 12 additions & 0 deletions packages/utils/src/run-command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import execa from 'execa';

export async function runCommand(command, args = []): Promise<void> {
try {
await execa(command, args, {
stdio: 'inherit',
shell: true,
});
} catch (e) {
throw new Error(e);
}
}
2 changes: 1 addition & 1 deletion packages/webpack-cli/lib/commands/ExternalCommand.js
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
1 change: 1 addition & 0 deletions packages/webpack-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 54116ae

Please sign in to comment.