File size: 2,247 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { yellow } from '../picocolors'
import spawn from 'next/dist/compiled/cross-spawn'
import type { PackageManager } from './get-pkg-manager'

interface InstallArgs {
  /**
   * Indicate whether to install packages using npm, pnpm, or yarn.
   */
  packageManager: PackageManager
  /**
   * Indicate whether there is an active internet connection.
   */
  isOnline: boolean
  /**
   * Indicate whether the given dependencies are devDependencies.
   */
  devDependencies?: boolean
}

/**
 * Spawn a package manager installation with either npm, pnpm, or yarn.
 *
 * @returns A Promise that resolves once the installation is finished.
 */
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 {
      // npm
      args = ['install', '--save-exact']
      args.push(devDependencies ? '--save-dev' : '--save')
    }

    args.push(...dependencies)
  } else {
    args = ['install'] // npm, pnpm, and yarn all support `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) => {
    /**
     * Spawn the installation process.
     */
    const child = spawn(packageManager, args, {
      cwd: root,
      stdio: 'inherit',
      env: {
        ...process.env,
        ADBLOCK: '1',
        // we set NODE_ENV to development as pnpm skips dev
        // dependencies when production
        NODE_ENV: 'development',
        DISABLE_OPENCOLLECTIVE: '1',
      },
    })
    child.on('close', (code) => {
      if (code !== 0) {
        reject({ command: `${packageManager} ${args.join(' ')}` })
        return
      }
      resolve()
    })
  })
}