|
|
import { yellow } from '../picocolors' |
|
|
import spawn from 'next/dist/compiled/cross-spawn' |
|
|
import type { PackageManager } from './get-pkg-manager' |
|
|
|
|
|
interface InstallArgs { |
|
|
|
|
|
|
|
|
|
|
|
packageManager: PackageManager |
|
|
|
|
|
|
|
|
|
|
|
isOnline: boolean |
|
|
|
|
|
|
|
|
|
|
|
devDependencies?: boolean |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function install( |
|
|
root: string, |
|
|
dependencies: string[], |
|
|
{ packageManager, isOnline, devDependencies }: InstallArgs |
|
|
): Promise<void> { |
|
|
let args: string[] = [] |
|
|
|
|
|
if (dependencies.length > 0) { |
|
|
if (packageManager === 'yarn') { |
|
|
args = ['add', '--exact'] |
|
|
if (devDependencies) args.push('--dev') |
|
|
} else if (packageManager === 'pnpm') { |
|
|
args = ['add', '--save-exact'] |
|
|
args.push(devDependencies ? '--save-dev' : '--save-prod') |
|
|
} else { |
|
|
|
|
|
args = ['install', '--save-exact'] |
|
|
args.push(devDependencies ? '--save-dev' : '--save') |
|
|
} |
|
|
|
|
|
args.push(...dependencies) |
|
|
} else { |
|
|
args = ['install'] |
|
|
|
|
|
if (!isOnline) { |
|
|
args.push('--offline') |
|
|
console.log(yellow('You appear to be offline.')) |
|
|
if (packageManager !== 'npm') { |
|
|
console.log( |
|
|
yellow(`Falling back to the local ${packageManager} cache.`) |
|
|
) |
|
|
} |
|
|
console.log() |
|
|
} |
|
|
} |
|
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
|
|
|
|
|
|
|
|
const child = spawn(packageManager, args, { |
|
|
cwd: root, |
|
|
stdio: 'inherit', |
|
|
env: { |
|
|
...process.env, |
|
|
ADBLOCK: '1', |
|
|
|
|
|
|
|
|
NODE_ENV: 'development', |
|
|
DISABLE_OPENCOLLECTIVE: '1', |
|
|
}, |
|
|
}) |
|
|
child.on('close', (code) => { |
|
|
if (code !== 0) { |
|
|
reject({ command: `${packageManager} ${args.join(' ')}` }) |
|
|
return |
|
|
} |
|
|
resolve() |
|
|
}) |
|
|
}) |
|
|
} |
|
|
|