From 5b3f57cd15f96f9e3936bf50b53c6c15b4a16e97 Mon Sep 17 00:00:00 2001 From: Noah Manneschmidt Date: Thu, 9 Nov 2023 14:18:56 -0800 Subject: [PATCH] first steps at supporting yarn 2+ --- src/makePatch.ts | 35 +++++++++++++++++++++++------------ src/spawnSafe.ts | 4 ++++ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/makePatch.ts b/src/makePatch.ts index 4d040297..5f3520fc 100644 --- a/src/makePatch.ts +++ b/src/makePatch.ts @@ -191,9 +191,15 @@ export function makePatch({ ) // copy .npmrc/.yarnrc in case packages are hosted in private registry - // copy .yarn directory as well to ensure installations work in yarn 2 + // copy portions of .yarn directory as well to ensure installations work in yarn 2 // tslint:disable-next-line:align - ;[".npmrc", ".yarnrc", ".yarn"].forEach((rcFile) => { + ;[ + ".npmrc", + ".yarnrc", + ".yarnrc.yml", + ".yarn/releases", + ".yarn/plugins", + ].forEach((rcFile) => { const rcPath = join(appPath, rcFile) if (existsSync(rcPath)) { copySync(rcPath, join(tmpRepo.name, rcFile), { dereference: true }) @@ -205,23 +211,30 @@ export function makePatch({ chalk.grey("•"), `Installing ${packageDetails.name}@${packageVersion} with yarn`, ) + const yarnArgs = ["install"] + + // add --ignore-engines flag only for yarn 1 + const yarnVersionCmd = spawnSafeSync(`yarn`, ["--version"], { + cwd: tmpRepoNpmRoot, + logStdErrOnError: false, + }) + if (yarnVersionCmd.stdout.toString().startsWith("1.")) { + yarnArgs.push("--ignore-engines") + } + try { // try first without ignoring scripts in case they are required // this works in 99.99% of cases - spawnSafeSync(`yarn`, ["install", "--ignore-engines"], { + spawnSafeSync(`yarn`, yarnArgs, { cwd: tmpRepoNpmRoot, logStdErrOnError: false, }) } catch (e) { // try again while ignoring scripts in case the script depends on // an implicit context which we haven't reproduced - spawnSafeSync( - `yarn`, - ["install", "--ignore-engines", "--ignore-scripts"], - { - cwd: tmpRepoNpmRoot, - }, - ) + spawnSafeSync(`yarn`, [...yarnArgs, "--ignore-scripts"], { + cwd: tmpRepoNpmRoot, + }) } } else { console.info( @@ -332,7 +345,6 @@ export function makePatch({ ) } process.exit(1) - return } try { @@ -383,7 +395,6 @@ export function makePatch({ `) } process.exit(1) - return } // maybe delete existing diff --git a/src/spawnSafe.ts b/src/spawnSafe.ts index 5278d121..d0c8cdc6 100644 --- a/src/spawnSafe.ts +++ b/src/spawnSafe.ts @@ -28,6 +28,10 @@ export const spawnSafeSync = ( } } if (mergedOptions.throwOnError) { + if (!result.error) { + // Create an error object to capture a useful stack trace + result.error = new Error("command exited with non-zero status") + } throw result } }