Skip to content

Commit 2ec3191

Browse files
committed
initial support for applying patch sequence
1 parent 1c0cfbe commit 2ec3191

11 files changed

+294
-112
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Test apply-multiple-patches: patch-package fails when a patch in the sequence is invalid 1`] = `
4+
"SNAPSHOT: patch-package fails when a patch in the sequence is invalid
5+
6+
**ERROR** Failed to apply patch for package left-pad at path
7+
8+
node_modules/left-pad
9+
10+
This error was caused because patch-package cannot apply the following patch file:
11+
12+
patches/left-pad+1.3.0+03+broken.patch
13+
14+
Try removing node_modules and trying again. If that doesn't work, maybe there was
15+
an accidental change made to the patch file? Try recreating it by manually
16+
editing the appropriate files and running:
17+
18+
patch-package left-pad
19+
20+
If that doesn't work, then it's a bug in patch-package, so please submit a bug
21+
report. Thanks!
22+
23+
https://github.com/ds300/patch-package/issues
24+
25+
26+
---
27+
patch-package finished with 1 error(s).
28+
END SNAPSHOT"
29+
`;
30+
31+
exports[`Test apply-multiple-patches: patch-package happily applies both good patches 1`] = `
32+
"SNAPSHOT: patch-package happily applies both good patches
33+
patch-package 0.0.0
34+
Applying patches...
35+
[email protected] (1 hello) ✔
36+
[email protected] (2 world) ✔
37+
END SNAPSHOT"
38+
`;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# make sure errors stop the script
2+
set -e
3+
4+
echo "add patch-package"
5+
npm add $1
6+
alias patch-package=./node_modules/.bin/patch-package
7+
8+
echo "SNAPSHOT: patch-package happily applies both good patches"
9+
patch-package
10+
echo "END SNAPSHOT"
11+
12+
cp *broken.patch patches/
13+
14+
(>&2 echo "SNAPSHOT: patch-package fails when a patch in the sequence is invalid")
15+
if patch-package
16+
then
17+
exit 1
18+
fi
19+
(>&2 echo "END SNAPSHOT")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { runIntegrationTest } from "../runIntegrationTest"
2+
runIntegrationTest({
3+
projectName: "apply-multiple-patches",
4+
shouldProduceSnapshots: true,
5+
})
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
diff --git a/node_modules/left-pad/index.js b/node_modules/left-pad/index.js
2+
index e90aec3..409dad7 100644
3+
--- a/node_modules/left-pad/index.js
4+
+++ b/node_modules/left-pad/index.js
5+
@@ -1,3 +1,4 @@
6+
+// hello. this is the first patch
7+
/* Thos program is free software. It comes without any warranty, to
8+
* the extent permitted by applicable law. You can redistribute it
9+
* and/or modify it under the terms of the Do What The Fuck You Want

integration-tests/apply-multiple-patches/package-lock.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "apply-multiple-patches",
3+
"version": "1.0.0",
4+
"description": "integration test for patch-package",
5+
"main": "index.js",
6+
"author": "",
7+
"license": "ISC",
8+
"dependencies": {
9+
"left-pad": "^1.3.0"
10+
}
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
diff --git a/node_modules/left-pad/index.js b/node_modules/left-pad/index.js
2+
index e90aec3..409dad7 100644
3+
--- a/node_modules/left-pad/index.js
4+
+++ b/node_modules/left-pad/index.js
5+
@@ -1,3 +1,4 @@
6+
+// hello. this is the first patch
7+
/* This program is free software. It comes without any warranty, to
8+
* the extent permitted by applicable law. You can redistribute it
9+
* and/or modify it under the terms of the Do What The Fuck You Want
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
diff --git a/node_modules/left-pad/index.js b/node_modules/left-pad/index.js
2+
index e90aec3..409dad7 100644
3+
--- a/node_modules/left-pad/index.js
4+
+++ b/node_modules/left-pad/index.js
5+
@@ -1,3 +1,4 @@
6+
+// hello. this is the first patch
7+
/* This program is free software. It comes without any warranty, to
8+
* the extent permitted by applicable law. You can redistribute it
9+
* and/or modify it under the terms of the Do What The Fuck You Want

src/PackageDetails.test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ Object {
9797
"patchFilename": "banana++apple+0.4.2.patch",
9898
"path": "node_modules/banana/node_modules/apple",
9999
"pathSpecifier": "banana/apple",
100+
"sequenceName": undefined,
101+
"sequenceNumber": undefined,
100102
"version": "0.4.2",
101103
}
102104
`)
@@ -119,6 +121,8 @@ Object {
119121
"patchFilename": "@types+banana++@types+apple++@mollusc+man+0.4.2-banana-tree.patch",
120122
"path": "node_modules/@types/banana/node_modules/@types/apple/node_modules/@mollusc/man",
121123
"pathSpecifier": "@types/banana/@types/apple/@mollusc/man",
124+
"sequenceName": undefined,
125+
"sequenceNumber": undefined,
122126
"version": "0.4.2-banana-tree",
123127
}
124128
`)
@@ -140,6 +144,8 @@ Object {
140144
"patchFilename": "@types+banana.patch++hello+0.4.2-banana-tree.patch",
141145
"path": "node_modules/@types/banana.patch/node_modules/hello",
142146
"pathSpecifier": "@types/banana.patch/hello",
147+
"sequenceName": undefined,
148+
"sequenceNumber": undefined,
143149
"version": "0.4.2-banana-tree",
144150
}
145151
`)
@@ -161,8 +167,53 @@ Object {
161167
"patchFilename": "@types+banana.patch++hello+0.4.2-banana-tree.dev.patch",
162168
"path": "node_modules/@types/banana.patch/node_modules/hello",
163169
"pathSpecifier": "@types/banana.patch/hello",
170+
"sequenceName": undefined,
171+
"sequenceNumber": undefined,
164172
"version": "0.4.2-banana-tree",
165173
}
174+
`)
175+
})
176+
177+
it("works for ordered patches", () => {
178+
expect(getPackageDetailsFromPatchFilename("left-pad+1.3.0+02+world"))
179+
.toMatchInlineSnapshot(`
180+
Object {
181+
"humanReadablePathSpecifier": "left-pad",
182+
"isDevOnly": false,
183+
"isNested": false,
184+
"name": "left-pad",
185+
"packageNames": Array [
186+
"left-pad",
187+
],
188+
"patchFilename": "left-pad+1.3.0+02+world",
189+
"path": "node_modules/left-pad",
190+
"pathSpecifier": "left-pad",
191+
"sequenceName": "world",
192+
"sequenceNumber": 2,
193+
"version": "1.3.0",
194+
}
195+
`)
196+
197+
expect(
198+
getPackageDetailsFromPatchFilename(
199+
"@microsoft/api-extractor+2.0.0+01+FixThing",
200+
),
201+
).toMatchInlineSnapshot(`
202+
Object {
203+
"humanReadablePathSpecifier": "@microsoft/api-extractor",
204+
"isDevOnly": false,
205+
"isNested": false,
206+
"name": "@microsoft/api-extractor",
207+
"packageNames": Array [
208+
"@microsoft/api-extractor",
209+
],
210+
"patchFilename": "@microsoft/api-extractor+2.0.0+01+FixThing",
211+
"path": "node_modules/@microsoft/api-extractor",
212+
"pathSpecifier": "@microsoft/api-extractor",
213+
"sequenceName": "FixThing",
214+
"sequenceNumber": 1,
215+
"version": "2.0.0",
216+
}
166217
`)
167218
})
168219
})

src/PackageDetails.ts

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export interface PatchedPackageDetails extends PackageDetails {
1313
version: string
1414
patchFilename: string
1515
isDevOnly: boolean
16+
sequenceName?: string
17+
sequenceNumber?: number
1618
}
1719

1820
export function parseNameAndVersion(
@@ -89,27 +91,6 @@ export function parseNameAndVersion(
8991
export function getPackageDetailsFromPatchFilename(
9092
patchFilename: string,
9193
): PatchedPackageDetails | null {
92-
const legacyMatch = patchFilename.match(
93-
/^([^+=]+?)(:|\+)(\d+\.\d+\.\d+.*?)(\.dev)?\.patch$/,
94-
)
95-
96-
if (legacyMatch) {
97-
const name = legacyMatch[1]
98-
const version = legacyMatch[3]
99-
100-
return {
101-
packageNames: [name],
102-
pathSpecifier: name,
103-
humanReadablePathSpecifier: name,
104-
path: join("node_modules", name),
105-
name,
106-
version,
107-
isNested: false,
108-
patchFilename,
109-
isDevOnly: patchFilename.endsWith(".dev.patch"),
110-
}
111-
}
112-
11394
const parts = patchFilename
11495
.replace(/(\.dev)?\.patch$/, "")
11596
.split("++")
@@ -141,6 +122,8 @@ export function getPackageDetailsFromPatchFilename(
141122
isNested: parts.length > 1,
142123
packageNames: parts.map(({ packageName: name }) => name),
143124
isDevOnly: patchFilename.endsWith(".dev.patch"),
125+
sequenceName: lastPart.sequenceName,
126+
sequenceNumber: lastPart.sequenceNumber,
144127
}
145128
}
146129

0 commit comments

Comments
 (0)