UNPKG

1.2 kBJavaScriptView Raw
1import { execa } from 'execa';
2import { detectPackageManager } from './utils.js';
3const installCommand = {
4 npm: 'install',
5 pnpm: 'add',
6 yarn: 'add',
7 bun: 'install'
8};
9const devFlag = {
10 npm: '--save-dev',
11 pnpm: '--save-dev',
12 yarn: '--dev',
13 bun: '--dev'
14};
15export async function installPackages(cwd, packages, dev) {
16 const pm = detectPackageManager();
17 const devParam = dev ? devFlag[pm] : '';
18 console.log('\n');
19 const p = execa(pm, [installCommand[pm], ...packages, devParam], {
20 cwd,
21 stdout: process.stdout,
22 stderr: process.stderr
23 });
24 const { stdout, stderr, exitCode } = await p;
25 if (exitCode !== 0) {
26 const cmd = getInstallCommand(pm, packages, dev);
27 const customError = ('⚠️ An unknown error happened! Please retry ' +
28 `installing dependencies via "${cmd}"\n\n` +
29 `Error: ${stderr || stdout || 'unknown'}`);
30 console.error(customError);
31 return false;
32 }
33 return true;
34}
35export function getInstallCommand(pm, packages, dev) {
36 const devParam = dev ? devFlag[pm] : '';
37 return `${pm} ${installCommand[pm]} ${packages.join(' ')} ${devParam}`;
38}