From b3d2a6dae15141c47cd776b3cf78d7e67449898c Mon Sep 17 00:00:00 2001 From: Mark Molinaro Date: Mon, 13 Sep 2021 20:28:59 +0000 Subject: [PATCH] Support yarn-path config option --- .../__snapshots__/yarn-path.test.ts.snap | 7 ++ .../yarn-path/add-postinstall.js | 9 ++ integration-tests/yarn-path/package.json | 12 ++ integration-tests/yarn-path/yarn-path.sh | 20 ++++ integration-tests/yarn-path/yarn-path.test.ts | 5 + integration-tests/yarn-path/yarn.js | 2 + integration-tests/yarn-path/yarn.lock | 113 ++++++++++++++++++ src/makePatch.ts | 28 ++++- typings/@yarnpkg/lockfile.d.ts | 1 + 9 files changed, 191 insertions(+), 6 deletions(-) create mode 100644 integration-tests/yarn-path/__snapshots__/yarn-path.test.ts.snap create mode 100644 integration-tests/yarn-path/add-postinstall.js create mode 100644 integration-tests/yarn-path/package.json create mode 100755 integration-tests/yarn-path/yarn-path.sh create mode 100644 integration-tests/yarn-path/yarn-path.test.ts create mode 100644 integration-tests/yarn-path/yarn.js create mode 100644 integration-tests/yarn-path/yarn.lock diff --git a/integration-tests/yarn-path/__snapshots__/yarn-path.test.ts.snap b/integration-tests/yarn-path/__snapshots__/yarn-path.test.ts.snap new file mode 100644 index 00000000..301ab863 --- /dev/null +++ b/integration-tests/yarn-path/__snapshots__/yarn-path.test.ts.snap @@ -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" +`; diff --git a/integration-tests/yarn-path/add-postinstall.js b/integration-tests/yarn-path/add-postinstall.js new file mode 100644 index 00000000..c976ddae --- /dev/null +++ b/integration-tests/yarn-path/add-postinstall.js @@ -0,0 +1,9 @@ +require("fs").writeFileSync( + "./package.json", + JSON.stringify({ + ...require("./package.json"), + scripts: { + postinstall: "patch-package", + }, + }), +) \ No newline at end of file diff --git a/integration-tests/yarn-path/package.json b/integration-tests/yarn-path/package.json new file mode 100644 index 00000000..9e50ebc4 --- /dev/null +++ b/integration-tests/yarn-path/package.json @@ -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" + } +} \ No newline at end of file diff --git a/integration-tests/yarn-path/yarn-path.sh b/integration-tests/yarn-path/yarn-path.sh new file mode 100755 index 00000000..8f256a7b --- /dev/null +++ b/integration-tests/yarn-path/yarn-path.sh @@ -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 left-pad@1.1.3 + +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" \ No newline at end of file diff --git a/integration-tests/yarn-path/yarn-path.test.ts b/integration-tests/yarn-path/yarn-path.test.ts new file mode 100644 index 00000000..42a765b4 --- /dev/null +++ b/integration-tests/yarn-path/yarn-path.test.ts @@ -0,0 +1,5 @@ +import { runIntegrationTest } from "../runIntegrationTest" +runIntegrationTest({ + projectName: "yarn-path", + shouldProduceSnapshots: true, +}) diff --git a/integration-tests/yarn-path/yarn.js b/integration-tests/yarn-path/yarn.js new file mode 100644 index 00000000..023b48f4 --- /dev/null +++ b/integration-tests/yarn-path/yarn.js @@ -0,0 +1,2 @@ +console.error("foobarbaz"); +process.exit(1); \ No newline at end of file diff --git a/integration-tests/yarn-path/yarn.lock b/integration-tests/yarn-path/yarn.lock new file mode 100644 index 00000000..bbd88073 --- /dev/null +++ b/integration-tests/yarn-path/yarn.lock @@ -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" + +colors@1.2.4: + version "1.2.4" + resolved "https://registry.yarnpkg.com/colors/-/colors-1.2.4.tgz#e0cb41d3e4b20806b3bfc27f4559f01b94bc2f7c" + +concat-map@0.0.1: + 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" + +minimatch@3.0.4, 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" + +nomnom@1.8.1: + 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" diff --git a/src/makePatch.ts b/src/makePatch.ts index 13696703..275e5637 100644 --- a/src/makePatch.ts +++ b/src/makePatch.ts @@ -10,6 +10,7 @@ import { unlinkSync, mkdirpSync, realpathSync, + readFileSync, } from "fs-extra" import { sync as rimraf } from "rimraf" import { copySync } from "fs-extra" @@ -29,6 +30,7 @@ import { maybePrintIssueCreationPrompt, openIssueCreationLink, } from "./createIssue" +import { parse, stringify } from "@yarnpkg/lockfile" function printNoPackageFoundError( packageName: string, @@ -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"]) + writeFileSync(tmpYarnrcPath, stringify(yarnrc)) + } + } catch (e) { + copySync(yarnrcPath, tmpYarnrcPath) } - }) + } if (packageManager === "yarn") { console.info( diff --git a/typings/@yarnpkg/lockfile.d.ts b/typings/@yarnpkg/lockfile.d.ts index c8eb93d5..45cd6b01 100644 --- a/typings/@yarnpkg/lockfile.d.ts +++ b/typings/@yarnpkg/lockfile.d.ts @@ -10,4 +10,5 @@ declare module "@yarnpkg/lockfile" { } } } + export function stringify(o: object): string }