-
-
Notifications
You must be signed in to change notification settings - Fork 591
/
prompt-installation.ts
36 lines (34 loc) · 1.21 KB
/
prompt-installation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { prompt } from 'enquirer';
import { green } from 'colorette';
import { runCommand } from './run-command';
import { getPackageManager } from './get-package-manager';
import { packageExists } from './package-exists';
/**
*
* @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 = 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 packageExists(packageName);
}
// eslint-disable-next-line require-atomic-updates
process.exitCode = 2;
}