-
-
Notifications
You must be signed in to change notification settings - Fork 299
/
Copy pathmakePatch.ts
606 lines (538 loc) Β· 17.8 KB
/
makePatch.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
import chalk from "chalk"
import console from "console"
import { renameSync } from "fs"
import {
copySync,
existsSync,
mkdirpSync,
mkdirSync,
realpathSync,
removeSync,
writeFileSync,
} from "fs-extra"
import { dirSync } from "tmp"
import { gzipSync } from "zlib"
import { applyPatch } from "./applyPatches"
import {
getPackageVCSDetails,
maybePrintIssueCreationPrompt,
openIssueCreationLink,
shouldRecommendIssue,
} from "./createIssue"
import { PackageManager } from "./detectPackageManager"
import { removeIgnoredFiles } from "./filterFiles"
import { getPackageResolution } from "./getPackageResolution"
import { getPackageVersion } from "./getPackageVersion"
import { hashFile } from "./hash"
import {
getPatchDetailsFromCliString,
PackageDetails,
PatchedPackageDetails,
} from "./PackageDetails"
import { parsePatchFile } from "./patch/parse"
import { getGroupedPatches } from "./patchFs"
import { dirname, join, resolve } from "./path"
import { resolveRelativeFileDependencies } from "./resolveRelativeFileDependencies"
import { spawnSafeSync } from "./spawnSafe"
import {
clearPatchApplicationState,
getPatchApplicationState,
PatchState,
savePatchApplicationState,
STATE_FILE_NAME,
verifyAppliedPatches,
} from "./stateFile"
function printNoPackageFoundError(
packageName: string,
packageJsonPath: string,
) {
console.log(
`No such package ${packageName}
File not found: ${packageJsonPath}`,
)
}
export function makePatch({
packagePathSpecifier,
appPath,
packageManager,
includePaths,
excludePaths,
patchDir,
createIssue,
mode,
}: {
packagePathSpecifier: string
appPath: string
packageManager: PackageManager
includePaths: RegExp
excludePaths: RegExp
patchDir: string
createIssue: boolean
mode: { type: "overwrite_last" } | { type: "append"; name?: string }
}) {
const packageDetails = getPatchDetailsFromCliString(packagePathSpecifier)
if (!packageDetails) {
console.log("No such package", packagePathSpecifier)
return
}
const state = getPatchApplicationState(packageDetails)
const isRebasing = state?.isRebasing ?? false
// If we are rebasing and no patches have been applied, --append is the only valid option because
// there are no previous patches to overwrite/update
if (
isRebasing &&
state?.patches.filter((p) => p.didApply).length === 0 &&
mode.type === "overwrite_last"
) {
mode = { type: "append", name: "initial" }
}
if (isRebasing && state) {
verifyAppliedPatches({ appPath, patchDir, state })
}
if (
mode.type === "overwrite_last" &&
isRebasing &&
state?.patches.length === 0
) {
mode = { type: "append", name: "initial" }
}
const existingPatches =
getGroupedPatches(patchDir).pathSpecifierToPatchFiles[
packageDetails.pathSpecifier
] || []
// apply all existing patches if appending
// otherwise apply all but the last
const previouslyAppliedPatches = state?.patches.filter((p) => p.didApply)
const patchesToApplyBeforeDiffing: PatchedPackageDetails[] = isRebasing
? mode.type === "append"
? existingPatches.slice(0, previouslyAppliedPatches!.length)
: state!.patches[state!.patches.length - 1].didApply
? existingPatches.slice(0, previouslyAppliedPatches!.length - 1)
: existingPatches.slice(0, previouslyAppliedPatches!.length)
: mode.type === "append"
? existingPatches
: existingPatches.slice(0, -1)
if (createIssue && mode.type === "append") {
console.log("--create-issue is not compatible with --append.")
process.exit(1)
}
if (createIssue && isRebasing) {
console.log("--create-issue is not compatible with rebasing.")
process.exit(1)
}
const numPatchesAfterCreate =
mode.type === "append" || existingPatches.length === 0
? existingPatches.length + 1
: existingPatches.length
const vcs = getPackageVCSDetails(packageDetails)
const canCreateIssue =
!isRebasing &&
shouldRecommendIssue(vcs) &&
numPatchesAfterCreate === 1 &&
mode.type !== "append"
const appPackageJson = require(join(appPath, "package.json"))
const packagePath = join(appPath, packageDetails.path)
const packageJsonPath = join(packagePath, "package.json")
if (!existsSync(packageJsonPath)) {
printNoPackageFoundError(packagePathSpecifier, packageJsonPath)
process.exit(1)
}
const tmpRepo = dirSync({ unsafeCleanup: true })
const tmpRepoPackagePath = join(tmpRepo.name, packageDetails.path)
const tmpRepoNpmRoot = tmpRepoPackagePath.slice(
0,
-`/node_modules/${packageDetails.name}`.length,
)
const tmpRepoPackageJsonPath = join(tmpRepoNpmRoot, "package.json")
try {
const patchesDir = resolve(join(appPath, patchDir))
console.info(chalk.grey("β’"), "Creating temporary folder")
// make a blank package.json
mkdirpSync(tmpRepoNpmRoot)
writeFileSync(
tmpRepoPackageJsonPath,
JSON.stringify({
dependencies: {
[packageDetails.name]: getPackageResolution({
packageDetails,
packageManager,
appPath,
}),
},
resolutions: resolveRelativeFileDependencies(
appPath,
appPackageJson.resolutions || {},
),
}),
)
const packageVersion = getPackageVersion(
join(resolve(packageDetails.path), "package.json"),
)
// copy .npmrc/.yarnrc in case packages are hosted in private registry
// copy .yarn directory as well to ensure installations work in yarn 2
// tslint:disable-next-line:align
;[".npmrc", ".yarnrc", ".yarn"].forEach((rcFile) => {
const rcPath = join(appPath, rcFile)
if (existsSync(rcPath)) {
copySync(rcPath, join(tmpRepo.name, rcFile), { dereference: true })
}
})
if (packageManager === "yarn") {
console.info(
chalk.grey("β’"),
`Installing ${packageDetails.name}@${packageVersion} with yarn`,
)
try {
// try first without ignoring scripts in case they are required
// this works in 99.99% of cases
spawnSafeSync(`yarn`, ["install", "--ignore-engines"], {
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,
},
)
}
} else {
console.info(
chalk.grey("β’"),
`Installing ${packageDetails.name}@${packageVersion} with npm`,
)
try {
// try first without ignoring scripts in case they are required
// this works in 99.99% of cases
spawnSafeSync(`npm`, ["i", "--force"], {
cwd: tmpRepoNpmRoot,
logStdErrOnError: false,
stdio: "ignore",
})
} catch (e) {
// try again while ignoring scripts in case the script depends on
// an implicit context which we haven't reproduced
spawnSafeSync(`npm`, ["i", "--ignore-scripts", "--force"], {
cwd: tmpRepoNpmRoot,
stdio: "ignore",
})
}
}
const git = (...args: string[]) =>
spawnSafeSync("git", args, {
cwd: tmpRepo.name,
env: { ...process.env, HOME: tmpRepo.name },
maxBuffer: 1024 * 1024 * 100,
})
// remove nested node_modules just to be safe
removeSync(join(tmpRepoPackagePath, "node_modules"))
// remove .git just to be safe
removeSync(join(tmpRepoPackagePath, ".git"))
// remove patch-package state file
removeSync(join(tmpRepoPackagePath, STATE_FILE_NAME))
// commit the package
console.info(chalk.grey("β’"), "Diffing your files with clean files")
writeFileSync(join(tmpRepo.name, ".gitignore"), "!/node_modules\n\n")
git("init")
git("config", "--local", "user.name", "patch-package")
git("config", "--local", "user.email", "patch@pack.age")
// remove ignored files first
removeIgnoredFiles(tmpRepoPackagePath, includePaths, excludePaths)
for (const patchDetails of patchesToApplyBeforeDiffing) {
if (
!applyPatch({
patchDetails,
patchDir,
patchFilePath: join(appPath, patchDir, patchDetails.patchFilename),
reverse: false,
cwd: tmpRepo.name,
bestEffort: false,
})
) {
// TODO: add better error message once --rebase is implemented
console.log(
`Failed to apply patch ${patchDetails.patchFilename} to ${packageDetails.pathSpecifier}`,
)
process.exit(1)
}
}
git("add", "-f", packageDetails.path)
git("commit", "--allow-empty", "-m", "init")
// replace package with user's version
removeSync(tmpRepoPackagePath)
// pnpm installs packages as symlinks, copySync would copy only the symlink
copySync(realpathSync(packagePath), tmpRepoPackagePath)
// remove nested node_modules just to be safe
removeSync(join(tmpRepoPackagePath, "node_modules"))
// remove .git just to be safe
removeSync(join(tmpRepoPackagePath, ".git"))
// remove patch-package state file
removeSync(join(tmpRepoPackagePath, STATE_FILE_NAME))
// also remove ignored files like before
removeIgnoredFiles(tmpRepoPackagePath, includePaths, excludePaths)
// stage all files
git("add", "-f", packageDetails.path)
// get diff of changes
const diffResult = git(
"diff",
"--cached",
"--no-color",
"--ignore-space-at-eol",
"--no-ext-diff",
"--src-prefix=a/",
"--dst-prefix=b/",
)
if (diffResult.stdout.length === 0) {
console.log(
`βοΈ Not creating patch file for package '${packagePathSpecifier}'`,
)
console.log(`βοΈ There don't appear to be any changes.`)
if (isRebasing && mode.type === "overwrite_last") {
console.log(
"\nπ‘ To remove a patch file, delete it and then reinstall node_modules from scratch.",
)
}
process.exit(1)
return
}
try {
parsePatchFile(diffResult.stdout.toString())
} catch (e) {
if (
(e as Error).message.includes("Unexpected file mode string: 120000")
) {
console.log(`
βοΈ ${chalk.red.bold("ERROR")}
Your changes involve creating symlinks. patch-package does not yet support
symlinks.
οΈPlease use ${chalk.bold("--include")} and/or ${chalk.bold(
"--exclude",
)} to narrow the scope of your patch if
this was unintentional.
`)
} else {
const outPath = "./patch-package-error.json.gz"
writeFileSync(
outPath,
gzipSync(
JSON.stringify({
error: { message: e.message, stack: e.stack },
patch: diffResult.stdout.toString(),
}),
),
)
console.log(`
βοΈ ${chalk.red.bold("ERROR")}
patch-package was unable to read the patch-file made by git. This should not
happen.
A diagnostic file was written to
${outPath}
Please attach it to a github issue
https://github.com/ds300/patch-package/issues/new?title=New+patch+parse+failed&body=Please+attach+the+diagnostic+file+by+dragging+it+into+here+π
Note that this diagnostic file will contain code from the package you were
attempting to patch.
`)
}
process.exit(1)
return
}
// maybe delete existing
if (mode.type === "append" && !isRebasing && existingPatches.length === 1) {
// if we are appending to an existing patch that doesn't have a sequence number let's rename it
const prevPatch = existingPatches[0]
if (prevPatch.sequenceNumber === undefined) {
const newFileName = createPatchFileName({
packageDetails,
packageVersion,
sequenceNumber: 1,
sequenceName: prevPatch.sequenceName ?? "initial",
})
const oldPath = join(appPath, patchDir, prevPatch.patchFilename)
const newPath = join(appPath, patchDir, newFileName)
renameSync(oldPath, newPath)
prevPatch.sequenceNumber = 1
prevPatch.patchFilename = newFileName
prevPatch.sequenceName = prevPatch.sequenceName ?? "initial"
}
}
const lastPatch = existingPatches[
state ? state.patches.length - 1 : existingPatches.length - 1
] as PatchedPackageDetails | undefined
const sequenceName =
mode.type === "append" ? mode.name : lastPatch?.sequenceName
const sequenceNumber =
mode.type === "append"
? (lastPatch?.sequenceNumber ?? 0) + 1
: lastPatch?.sequenceNumber
const patchFileName = createPatchFileName({
packageDetails,
packageVersion,
sequenceName,
sequenceNumber,
})
const patchPath: string = join(patchesDir, patchFileName)
if (!existsSync(dirname(patchPath))) {
// scoped package
mkdirSync(dirname(patchPath))
}
// if we are inserting a new patch into a sequence we most likely need to update the sequence numbers
if (isRebasing && mode.type === "append") {
const patchesToNudge = existingPatches.slice(state!.patches.length)
if (sequenceNumber === undefined) {
throw new Error("sequenceNumber is undefined while rebasing")
}
if (
patchesToNudge[0]?.sequenceNumber !== undefined &&
patchesToNudge[0].sequenceNumber <= sequenceNumber
) {
let next = sequenceNumber + 1
for (const p of patchesToNudge) {
const newName = createPatchFileName({
packageDetails,
packageVersion,
sequenceName: p.sequenceName,
sequenceNumber: next++,
})
console.log(
"Renaming",
chalk.bold(p.patchFilename),
"to",
chalk.bold(newName),
)
const oldPath = join(appPath, patchDir, p.patchFilename)
const newPath = join(appPath, patchDir, newName)
renameSync(oldPath, newPath)
}
}
}
writeFileSync(patchPath, diffResult.stdout)
console.log(
`${chalk.green("β")} Created file ${join(patchDir, patchFileName)}\n`,
)
const prevState: PatchState[] = patchesToApplyBeforeDiffing.map(
(p): PatchState => ({
patchFilename: p.patchFilename,
didApply: true,
patchContentHash: hashFile(join(appPath, patchDir, p.patchFilename)),
}),
)
const nextState: PatchState[] = [
...prevState,
{
patchFilename: patchFileName,
didApply: true,
patchContentHash: hashFile(patchPath),
},
]
// if any patches come after this one we just made, we should reapply them
let didFailWhileFinishingRebase = false
if (isRebasing) {
const currentPatches = getGroupedPatches(join(appPath, patchDir))
.pathSpecifierToPatchFiles[packageDetails.pathSpecifier]
const previouslyUnappliedPatches = currentPatches.slice(nextState.length)
if (previouslyUnappliedPatches.length) {
console.log(`Fast forwarding...`)
for (const patch of previouslyUnappliedPatches) {
const patchFilePath = join(appPath, patchDir, patch.patchFilename)
if (
!applyPatch({
patchDetails: patch,
patchDir,
patchFilePath,
reverse: false,
cwd: process.cwd(),
bestEffort: false,
})
) {
didFailWhileFinishingRebase = true
logPatchSequenceError({ patchDetails: patch })
nextState.push({
patchFilename: patch.patchFilename,
didApply: false,
patchContentHash: hashFile(patchFilePath),
})
break
} else {
console.log(` ${chalk.green("β")} ${patch.patchFilename}`)
nextState.push({
patchFilename: patch.patchFilename,
didApply: true,
patchContentHash: hashFile(patchFilePath),
})
}
}
}
}
if (isRebasing || numPatchesAfterCreate > 1) {
savePatchApplicationState({
packageDetails,
patches: nextState,
isRebasing: didFailWhileFinishingRebase,
})
} else {
clearPatchApplicationState(packageDetails)
}
if (canCreateIssue) {
if (createIssue) {
openIssueCreationLink({
packageDetails,
patchFileContents: diffResult.stdout.toString(),
packageVersion,
patchPath,
})
} else {
maybePrintIssueCreationPrompt(vcs, packageDetails, packageManager)
}
}
} catch (e) {
console.log(e)
throw e
} finally {
tmpRepo.removeCallback()
}
}
function createPatchFileName({
packageDetails,
packageVersion,
sequenceNumber,
sequenceName,
}: {
packageDetails: PackageDetails
packageVersion: string
sequenceNumber?: number
sequenceName?: string
}) {
const packageNames = packageDetails.packageNames
.map((name) => name.replace(/\//g, "+"))
.join("++")
const nameAndVersion = `${packageNames}+${packageVersion}`
const num =
sequenceNumber === undefined
? ""
: `+${sequenceNumber.toString().padStart(3, "0")}`
const name = !sequenceName ? "" : `+${sequenceName}`
return `${nameAndVersion}${num}${name}.patch`
}
export function logPatchSequenceError({
patchDetails,
}: {
patchDetails: PatchedPackageDetails
}) {
console.log(`
${chalk.red.bold("β ERROR")}
Failed to apply patch file ${chalk.bold(patchDetails.patchFilename)}.
If this patch file is no longer useful, delete it and run
${chalk.bold(`patch-package`)}
To partially apply the patch (if possible) and output a log of errors to fix, run
${chalk.bold(`patch-package --partial`)}
After which you should make any required changes inside ${
patchDetails.path
}, and finally run
${chalk.bold(`patch-package ${patchDetails.pathSpecifier}`)}
to update the patch file.
`)
}