Unverified Commit 64da71cf authored by Ethan Arrowood's avatar Ethan Arrowood Committed by GitHub

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

This fixes and improves the `lib/helpers/install.ts` file (`install()`)
method to better support pnpm and properly respect the `root` argument.

I encountered an issue where by running `next <command> <project-dir>`,
and the `<command>` involved installing missing dependencies (like
`lint` or my upcoming `experimental-test`), it was not executing pnpm in
my `<project-dir>` because pnpm doesn't have a `--cwd` flag.



Closes NEXT-3092
Co-authored-by: 's avatarZack Tanner <1939140+ztanner@users.noreply.github.com>
parent 03b7a0fb
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
/**
......@@ -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,
......@@ -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()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment