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 { yellow } from '../picocolors'
import spawn from 'next/dist/compiled/cross-spawn' import spawn from 'next/dist/compiled/cross-spawn'
import type { PackageManager } from './get-pkg-manager'
export type PackageManager = 'npm' | 'pnpm' | 'yarn'
interface InstallArgs { interface InstallArgs {
/** /**
* Indicate whether to install packages using npm, pnpm or Yarn. * Indicate whether to install packages using npm, pnpm, or yarn.
*/ */
packageManager: PackageManager packageManager: PackageManager
/** /**
* Indicate whether there is an active Internet connection. * Indicate whether there is an active internet connection.
*/ */
isOnline: boolean isOnline: boolean
/** /**
...@@ -19,81 +18,52 @@ interface InstallArgs { ...@@ -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. * @returns A Promise that resolves once the installation is finished.
*/ */
export function install( export function install(
root: string, root: string,
dependencies: string[] | null, dependencies: string[],
{ packageManager, isOnline, devDependencies }: InstallArgs { packageManager, isOnline, devDependencies }: InstallArgs
): Promise<void> { ): Promise<void> {
/** let args: string[] = []
* (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'
if (dependencies && dependencies.length) { if (dependencies.length > 0) {
/** if (packageManager === 'yarn') {
* If there are dependencies, run a variation of `{packageManager} add`. args = ['add', '--exact']
*/ if (devDependencies) args.push('--dev')
if (useYarn) { } else if (packageManager === 'pnpm') {
/** args = ['add', '--save-exact']
* Call `yarn add --exact (--offline)? (-D)? ...`. args.push(devDependencies ? '--save-dev' : '--save-prod')
*/
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)
}
} else { } else {
/** // npm
* If there are no dependencies, run a variation of `{packageManager} args = ['install', '--save-exact']
* install`. args.push(devDependencies ? '--save-dev' : '--save')
*/
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()
}
}
} }
/**
* Add any package manager-specific flags. args.push(...dependencies)
*/ } else {
if (useYarn) { args = ['install'] // npm, pnpm, and yarn all support `install`
args.push(...yarnFlags)
} else { if (!isOnline) {
args.push(...npmFlags) 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. * Spawn the installation process.
*/ */
const child = spawn(command, args, { const child = spawn(packageManager, args, {
cwd: root,
stdio: 'inherit', stdio: 'inherit',
env: { env: {
...process.env, ...process.env,
...@@ -106,7 +76,7 @@ export function install( ...@@ -106,7 +76,7 @@ export function install(
}) })
child.on('close', (code) => { child.on('close', (code) => {
if (code !== 0) { if (code !== 0) {
reject({ command: `${command} ${args.join(' ')}` }) reject({ command: `${packageManager} ${args.join(' ')}` })
return return
} }
resolve() 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