Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: lib/helpers/install.ts to better support pnpm and properly respect root argument #64418

Merged
merged 2 commits into from
Apr 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 36 additions & 66 deletions packages/next/src/lib/helpers/install.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { yellow } from '../picocolors'
import spawn from 'next/dist/compiled/cross-spawn'

export type PackageManager = 'npm' | 'pnpm' | 'yarn'
import type { PackageManager } from './get-pkg-manager'

interface InstallArgs {
/**
* Indicate whether to install packages using npm, pnpm or Yarn.
* Indicate whether to install packages using npm, pnpm, or yarn.
*/
packageManager: PackageManager
/**
* Indicate whether there is an active Internet connection.
* Indicate whether there is an active internet connection.
*/
isOnline: boolean
/**
Expand All @@ -19,81 +18,52 @@ interface InstallArgs {
}

/**
* Spawn a package manager installation with either Yarn or NPM.
* 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[] | null,
dependencies: string[],
{ packageManager, isOnline, devDependencies }: InstallArgs
): Promise<void> {
/**
* (p)npm-specific command-line flags.
*/
const npmFlags: string[] = []
/**
* Yarn-specific command-line flags.
*/
const yarnFlags: string[] = []
/**
* Return a Promise that resolves once the installation is finished.
*/
return new Promise((resolve, reject) => {
let args: string[]
let command = packageManager
const useYarn = packageManager === 'yarn'
let args: string[] = []

if (dependencies && dependencies.length) {
/**
* If there are dependencies, run a variation of `{packageManager} add`.
*/
if (useYarn) {
/**
* Call `yarn add --exact (--offline)? (-D)? ...`.
*/
args = ['add', '--exact']
if (!isOnline) args.push('--offline')
args.push('--cwd', root)
if (devDependencies) args.push('--dev')
args.push(...dependencies)
} else {
/**
* Call `(p)npm install [--save|--save-dev] ...`.
*/
args = ['install', '--save-exact']
args.push(devDependencies ? '--save-dev' : '--save')
args.push(...dependencies)
}
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 {
/**
* If there are no dependencies, run a variation of `{packageManager}
* install`.
*/
args = ['install']
if (!isOnline) {
console.log(yellow('You appear to be offline.'))
if (useYarn) {
console.log(yellow('Falling back to the local Yarn cache.'))
console.log()
args.push('--offline')
} else {
console.log()
}
}
// npm
args = ['install', '--save-exact']
args.push(devDependencies ? '--save-dev' : '--save')
}
/**
* Add any package manager-specific flags.
*/
if (useYarn) {
args.push(...yarnFlags)
} else {
args.push(...npmFlags)

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(command, args, {
const child = spawn(packageManager, args, {
cwd: root,
stdio: 'inherit',
env: {
...process.env,
Expand All @@ -106,7 +76,7 @@ export function install(
})
child.on('close', (code) => {
if (code !== 0) {
reject({ command: `${command} ${args.join(' ')}` })
reject({ command: `${packageManager} ${args.join(' ')}` })
return
}
resolve()
Expand Down
Loading