| 9 | * @returns A Promise that resolves once the installation is finished. |
| 10 | */ |
| 11 | export async function install( |
| 12 | /** Indicate which package manager to use. */ |
| 13 | packageManager: PackageManager, |
| 14 | /** Indicate whether there is an active Internet connection.*/ |
| 15 | isOnline: boolean |
| 16 | ): Promise<void> { |
| 17 | const args: string[] = ['install'] |
| 18 | if (!isOnline) { |
| 19 | console.log( |
| 20 | yellow('You appear to be offline.\nFalling back to the local cache.') |
| 21 | ) |
| 22 | args.push('--offline') |
| 23 | } |
| 24 | /** |
| 25 | * Return a Promise that resolves once the installation is finished. |
| 26 | */ |
| 27 | return new Promise((resolve, reject) => { |
| 28 | /** |
| 29 | * Spawn the installation process. |
| 30 | */ |
| 31 | const child = spawn(packageManager, args, { |
| 32 | stdio: 'inherit', |
| 33 | env: { |
| 34 | ...process.env, |
| 35 | ADBLOCK: '1', |
| 36 | // we set NODE_ENV to development as pnpm skips dev |
| 37 | // dependencies when production |
| 38 | NODE_ENV: 'development', |
| 39 | DISABLE_OPENCOLLECTIVE: '1', |
| 40 | }, |
| 41 | }) |
| 42 | child.on('close', (code) => { |
| 43 | if (code !== 0) { |
| 44 | reject({ command: `${packageManager} ${args.join(' ')}` }) |
| 45 | return |
| 46 | } |
| 47 | resolve() |
| 48 | }) |
| 49 | }) |
| 50 | } |