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

Support creating patches with Yarn 2+ #506

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
35 changes: 23 additions & 12 deletions src/makePatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand All @@ -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,
})
Comment on lines +217 to +220
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a more elegant way to interrogate the currently version of yarn?

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(
Expand Down Expand Up @@ -332,7 +345,6 @@ export function makePatch({
)
}
process.exit(1)
return
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These returns are dead code, as process.exit ends the process immediately and does not return

}

try {
Expand Down Expand Up @@ -383,7 +395,6 @@ export function makePatch({
`)
}
process.exit(1)
return
}

// maybe delete existing
Expand Down
4 changes: 4 additions & 0 deletions src/spawnSafe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down