Skip to content

Commit 2f1c855

Browse files
committed
fix lerna canary support
1 parent 6fa4e38 commit 2f1c855

File tree

8 files changed

+77
-29
lines changed

8 files changed

+77
-29
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Test lerna-canary: making patch 1`] = `
4+
"SNAPSHOT: making patch
5+
patch-package 0.0.0
6+
• Creating temporary folder
7+
• Installing @parcel/[email protected] with yarn
8+
• Diffing your files with clean files
9+
✔ Created file patches/@parcel+codeframe+2.0.0-nightly.137.patch
10+
END SNAPSHOT"
11+
`;
12+
13+
exports[`Test lerna-canary: the patch applies 1`] = `
14+
"SNAPSHOT: the patch applies
15+
patch-package 0.0.0
16+
Applying patches...
17+
@parcel/[email protected]
18+
END SNAPSHOT"
19+
`;
20+
21+
exports[`Test lerna-canary: the patch looks like this 1`] = `
22+
"SNAPSHOT: the patch looks like this
23+
diff --git a/node_modules/@parcel/codeframe/src/codeframe.js b/node_modules/@parcel/codeframe/src/codeframe.js
24+
index 2bf2c1c..ef0695b 100644
25+
--- a/node_modules/@parcel/codeframe/src/codeframe.js
26+
+++ b/node_modules/@parcel/codeframe/src/codeframe.js
27+
@@ -41,7 +41,7 @@ const highlightSyntax = (txt: string, lang?: string): string => {
28+
return emphasize.highlightAuto(txt).value;
29+
};
30+
31+
-export default function codeFrame(
32+
+export default function patch-package(
33+
code: string,
34+
highlights: Array<DiagnosticCodeHighlight>,
35+
// $FlowFixMe
36+
END SNAPSHOT"
37+
`;

integration-tests/lerna-canary/lerna-canary.sh

+8-8
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@ set -e
33

44
echo "add patch-package"
55
yarn add $1
6+
alias patch-package=./node_modules/.bin/patch-package
67

8+
# this test makes sure that we can patch packages with build metadata in their version strings e.g. 4.5.6+commitsha
79
echo "Add @parcel/codeframe"
810
yarn add @parcel/[email protected]
911

1012
echo "replace codeframe with yarn in @parcel/codefram/src/codeframe.js"
11-
npx replace codeframe yarn node_modules/@parcel/codeframe/src/codeframe.js
13+
npx replace codeFrame patch-package node_modules/@parcel/codeframe/src/codeframe.js
1214

1315
echo "SNAPSHOT: making patch"
14-
npx patch-package @parcel/codeframe
16+
patch-package @parcel/codeframe
1517
echo "END SNAPSHOT"
1618

1719
echo "SNAPSHOT: the patch looks like this"
18-
cat patches/@parcel/codeframe+2.0.0-nightly.137.patch
20+
cat patches/@parcel+codeframe+2.0.0-nightly.137.patch
1921
echo "END SNAPSHOT"
2022

2123
echo "reinstall node_modules"
@@ -27,12 +29,10 @@ if grep yarn node_modules/@parcel/codeframe/src/codeframe.js ; then
2729
exit 1
2830
fi
2931

30-
echo "add patch-package to postinstall hook"
31-
node ./add-postinstall.js
32-
3332
echo "reinstall node_modules"
3433
npx rimraf node_modules
3534
yarn
3635

37-
echo "patch-package did run"
38-
grep yarn node_modules/@parcel/codeframe/src/codeframe.js
36+
echo "SNAPSHOT: the patch applies"
37+
patch-package
38+
echo "END SNAPSHOT"
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
import { runIntegrationTest } from "../runIntegrationTest"
2-
runIntegrationTest({projectName: "lerna-canary", shouldProduceSnapshots: false})
2+
runIntegrationTest({
3+
projectName: "lerna-canary",
4+
shouldProduceSnapshots: true,
5+
})

package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
"patch-package": "./index.js"
1414
},
1515
"scripts": {
16-
"precommit": "tsc --noEmit && lint-staged",
1716
"prepublishOnly": "yarn run clean && yarn run build",
1817
"build": "tsc --project tsconfig.build.json",
1918
"new-integration-test": "ts-node integration-tests/newIntegrationTest.ts",
@@ -22,6 +21,11 @@
2221
"prepack": "rimraf dist/**/*.test.js",
2322
"test": "./run-tests.sh --runInBand"
2423
},
24+
"husky": {
25+
"hooks": {
26+
"pre-commit": "tsc --noEmit && lint-staged"
27+
}
28+
},
2529
"lint-staged": {
2630
"*.ts": [
2731
"tslint -c tslint.json -p tsconfig.json",
@@ -59,7 +63,7 @@
5963
"jest": "^24.5.0",
6064
"lint-staged": "^8.1.5",
6165
"np": "^6.2.0",
62-
"prettier": "^1.18.2",
66+
"prettier": "^2.2.1",
6367
"randomstring": "^1.1.5",
6468
"ts-jest": "^24.0.0",
6569
"ts-node": "8.0.3",

src/getPackageResolution.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { PackageManager, detectPackageManager } from "./detectPackageManager"
44
import { readFileSync, existsSync } from "fs-extra"
55
import { parse as parseYarnLockFile } from "@yarnpkg/lockfile"
66
import findWorkspaceRoot from "find-yarn-workspace-root"
7+
import { getPackageVersion } from "./getPackageVersion"
78

89
export function getPackageResolution({
910
packageDetails,
@@ -31,12 +32,9 @@ export function getPackageResolution({
3132
throw new Error("Can't parse lock file")
3233
}
3334

34-
const installedVersionLong = require(join(
35-
resolve(appPath, packageDetails.path),
36-
"package.json",
37-
)).version as string
38-
const indexOfPlus = installedVersionLong.indexOf('+');
39-
const installedVersion = indexOfPlus === -1 ? installedVersionLong : installedVersionLong.substring(0, indexOfPlus)
35+
const installedVersion = getPackageVersion(
36+
join(resolve(appPath, packageDetails.path), "package.json"),
37+
)
4038

4139
const entries = Object.entries(appLockFile.object).filter(
4240
([k, v]) =>
@@ -89,7 +87,8 @@ export function getPackageResolution({
8987
}
9088
lockFileStack.reverse()
9189
const relevantStackEntry = lockFileStack.find(
92-
entry => entry.dependencies && packageDetails.name in entry.dependencies,
90+
(entry) =>
91+
entry.dependencies && packageDetails.name in entry.dependencies,
9392
)
9493
const pkg = relevantStackEntry.dependencies[packageDetails.name]
9594
return pkg.resolved || pkg.from || pkg.version

src/getPackageVersion.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export function getPackageVersion(packageJsonPath: string): string {
2+
// remove build metadata
3+
return require(packageJsonPath).version.replace(/\+.*$/, "")
4+
}

src/makePatch.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { resolveRelativeFileDependencies } from "./resolveRelativeFileDependenci
2222
import { getPackageResolution } from "./getPackageResolution"
2323
import { parsePatchFile } from "./patch/parse"
2424
import { gzipSync } from "zlib"
25+
import { getPackageVersion } from "./getPackageVersion"
2526

2627
function printNoPackageFoundError(
2728
packageName: string,
@@ -97,13 +98,13 @@ export function makePatch({
9798
}),
9899
)
99100

100-
const packageVersion = require(join(
101-
resolve(packageDetails.path),
102-
"package.json",
103-
)).version as string
101+
const packageVersion = getPackageVersion(
102+
join(resolve(packageDetails.path), "package.json"),
103+
)
104104

105105
// copy .npmrc/.yarnrc in case packages are hosted in private registry
106-
[".npmrc", ".yarnrc"].forEach(rcFile => {
106+
// tslint:disable-next-line:align
107+
;[".npmrc", ".yarnrc"].forEach((rcFile) => {
107108
const rcPath = join(appPath, rcFile)
108109
if (existsSync(rcPath)) {
109110
copySync(rcPath, join(tmpRepo.name, rcFile))
@@ -264,11 +265,11 @@ export function makePatch({
264265
}
265266

266267
const packageNames = packageDetails.packageNames
267-
.map(name => name.replace(/\//g, "+"))
268+
.map((name) => name.replace(/\//g, "+"))
268269
.join("++")
269270

270271
// maybe delete existing
271-
getPatchFiles(patchDir).forEach(filename => {
272+
getPatchFiles(patchDir).forEach((filename) => {
272273
const deets = getPackageDetailsFromPatchFilename(filename)
273274
if (deets && deets.path === packageDetails.path) {
274275
unlinkSync(join(patchDir, filename))

yarn.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -4042,10 +4042,10 @@ prepend-http@^2.0.0:
40424042
resolved "https://registry.yarnpkg.com/prepend-http/-/prepend-http-2.0.0.tgz#e92434bfa5ea8c19f41cdfd401d741a3c819d897"
40434043
integrity sha1-6SQ0v6XqjBn0HN/UAddBo8gZ2Jc=
40444044

4045-
prettier@^1.18.2:
4046-
version "1.18.2"
4047-
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.18.2.tgz#6823e7c5900017b4bd3acf46fe9ac4b4d7bda9ea"
4048-
integrity sha512-OeHeMc0JhFE9idD4ZdtNibzY0+TPHSpSSb9h8FqtP+YnoZZ1sl8Vc9b1sasjfymH3SonAF4QcA2+mzHPhMvIiw==
4045+
prettier@^2.2.1:
4046+
version "2.2.1"
4047+
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.2.1.tgz#795a1a78dd52f073da0cd42b21f9c91381923ff5"
4048+
integrity sha512-PqyhM2yCjg/oKkFPtTGUojv7gnZAoG80ttl45O6x2Ug/rMJw4wcc9k6aaf2hibP7BGVCCM33gZoGjyvt9mm16Q==
40494049

40504050
pretty-format@^24.5.0:
40514051
version "24.5.0"

0 commit comments

Comments
 (0)