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 yarn-path config option in yarnrc #348

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Test yarn-path: uses yarn version specified in yarn-path 1`] = `
"SNAPSHOT: uses yarn version specified in yarn-path
foobarbaz
END SNAPSHOT"
`;
9 changes: 9 additions & 0 deletions integration-tests/yarn-path/add-postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require("fs").writeFileSync(
"./package.json",
JSON.stringify({
...require("./package.json"),
scripts: {
postinstall: "patch-package",
},
}),
)
12 changes: 12 additions & 0 deletions integration-tests/yarn-path/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "yarn-path",
"version": "1.0.0",
"description": "integration test for patch-package",
"main": "index.js",
"author": "",
"license": "ISC",
"dependencies": {
"replace": "^1.0.0",
"rimraf": "^2.6.2"
}
}
20 changes: 20 additions & 0 deletions integration-tests/yarn-path/yarn-path.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# make sure errors stop the script
set -e

echo "add patch-package"
yarn add $1
alias patch-package=./node_modules/.bin/patch-package

echo "Add left-pad"
yarn add [email protected]

echo "replace pad with yarn in left-pad/index.js"
npx replace pad yarn node_modules/left-pad/index.js

echo "write yarnrc with yarn-path"
echo "yarn-path "yarn.js"" > .yarnrc


echo "SNAPSHOT: uses yarn version specified in yarn-path"
patch-package left-pad 2>&1 | grep -i foobarbaz
echo "END SNAPSHOT"
5 changes: 5 additions & 0 deletions integration-tests/yarn-path/yarn-path.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { runIntegrationTest } from "../runIntegrationTest"
runIntegrationTest({
projectName: "yarn-path",
shouldProduceSnapshots: true,
})
2 changes: 2 additions & 0 deletions integration-tests/yarn-path/yarn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
console.error("foobarbaz");
process.exit(1);
113 changes: 113 additions & 0 deletions integration-tests/yarn-path/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


ansi-styles@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-1.0.0.tgz#cb102df1c56f5123eab8b67cd7b98027a0279178"

balanced-match@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767"

brace-expansion@^1.1.7:
version "1.1.11"
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd"
dependencies:
balanced-match "^1.0.0"
concat-map "0.0.1"

chalk@~0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-0.4.0.tgz#5199a3ddcd0c1efe23bc08c1b027b06176e0c64f"
dependencies:
ansi-styles "~1.0.0"
has-color "~0.1.0"
strip-ansi "~0.1.0"

[email protected]:
version "1.2.4"
resolved "https://registry.yarnpkg.com/colors/-/colors-1.2.4.tgz#e0cb41d3e4b20806b3bfc27f4559f01b94bc2f7c"

[email protected]:
version "0.0.1"
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"

fs.realpath@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"

glob@^7.0.5:
version "7.1.2"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.2.tgz#c19c9df9a028702d678612384a6552404c636d15"
dependencies:
fs.realpath "^1.0.0"
inflight "^1.0.4"
inherits "2"
minimatch "^3.0.4"
once "^1.3.0"
path-is-absolute "^1.0.0"

has-color@~0.1.0:
version "0.1.7"
resolved "https://registry.yarnpkg.com/has-color/-/has-color-0.1.7.tgz#67144a5260c34fc3cca677d041daf52fe7b78b2f"

inflight@^1.0.4:
version "1.0.6"
resolved "https://registry.yarnpkg.com/inflight/-/inflight-1.0.6.tgz#49bd6331d7d02d0c09bc910a1075ba8165b56df9"
dependencies:
once "^1.3.0"
wrappy "1"

inherits@2:
version "2.0.3"
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de"

[email protected], minimatch@^3.0.4:
version "3.0.4"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083"
dependencies:
brace-expansion "^1.1.7"

[email protected]:
version "1.8.1"
resolved "https://registry.yarnpkg.com/nomnom/-/nomnom-1.8.1.tgz#2151f722472ba79e50a76fc125bb8c8f2e4dc2a7"
dependencies:
chalk "~0.4.0"
underscore "~1.6.0"

once@^1.3.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1"
dependencies:
wrappy "1"

path-is-absolute@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f"

replace@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/replace/-/replace-1.0.0.tgz#da5235cc6d64d5c3a74e4ef73b487aad0f79a74b"
dependencies:
colors "1.2.4"
minimatch "3.0.4"
nomnom "1.8.1"

rimraf@^2.6.2:
version "2.6.2"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.6.2.tgz#2ed8150d24a16ea8651e6d6ef0f47c4158ce7a36"
dependencies:
glob "^7.0.5"

strip-ansi@~0.1.0:
version "0.1.1"
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-0.1.1.tgz#39e8a98d044d150660abe4a6808acf70bb7bc991"

underscore@~1.6.0:
version "1.6.0"
resolved "https://registry.yarnpkg.com/underscore/-/underscore-1.6.0.tgz#8b38b10cacdef63337b8b24e4ff86d45aea529a8"

wrappy@1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"
28 changes: 22 additions & 6 deletions src/makePatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
unlinkSync,
mkdirpSync,
realpathSync,
readFileSync,
} from "fs-extra"
import { sync as rimraf } from "rimraf"
import { copySync } from "fs-extra"
Expand All @@ -29,6 +30,7 @@ import {
maybePrintIssueCreationPrompt,
openIssueCreationLink,
} from "./createIssue"
import { parse, stringify } from "@yarnpkg/lockfile"

function printNoPackageFoundError(
packageName: string,
Expand Down Expand Up @@ -111,13 +113,27 @@ export function makePatch({
)

// copy .npmrc/.yarnrc in case packages are hosted in private registry
// tslint:disable-next-line:align
;[".npmrc", ".yarnrc"].forEach((rcFile) => {
const rcPath = join(appPath, rcFile)
if (existsSync(rcPath)) {
copySync(rcPath, join(tmpRepo.name, rcFile))
const npmrcPath = join(appPath, ".npmrc")
if (existsSync(npmrcPath)) {
copySync(npmrcPath, join(tmpRepo.name, ".npmrc"))
}

// Update yarn-path config to point to location specified relative to original yarnrc
// https://classic.yarnpkg.com/en/docs/yarnrc/#toc-yarn-path
const yarnrcPath = join(appPath, ".yarnrc")
const tmpYarnrcPath = join(tmpRepo.name, ".yarnrc")
if (existsSync(yarnrcPath)) {
try {
const yarnrc = parse(readFileSync(yarnrcPath).toString("UTF8"))
.object as any
if (yarnrc["yarn-path"]) {
yarnrc["yarn-path"] = join(appPath, yarnrc["yarn-path"])
Copy link
Contributor

@NMinhNguyen NMinhNguyen Nov 18, 2021

Choose a reason for hiding this comment

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

I wonder if path.resolve would be better than path.join in case the path has something like ../

writeFileSync(tmpYarnrcPath, stringify(yarnrc))
}
} catch (e) {
copySync(yarnrcPath, tmpYarnrcPath)
}
})
}

if (packageManager === "yarn") {
console.info(
Expand Down
1 change: 1 addition & 0 deletions typings/@yarnpkg/lockfile.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ declare module "@yarnpkg/lockfile" {
}
}
}
export function stringify(o: object): string
}