Skip to content

Commit f95a442

Browse files
committed
use declared package version. add header comments to patchfiles
1 parent 8b80357 commit f95a442

File tree

6 files changed

+78
-9
lines changed

6 files changed

+78
-9
lines changed

β€Žpackage.jsonβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
"open": "^7.4.2",
8484
"rimraf": "^2.6.3",
8585
"semver": "^5.6.0",
86+
"shlex": "^2.0.2",
8687
"slash": "^2.0.0",
8788
"tmp": "^0.0.33"
8889
},

β€Žsrc/getPackageResolution.tsβ€Ž

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import { readFileSync, existsSync } from "fs-extra"
55
import { parse as parseYarnLockFile } from "@yarnpkg/lockfile"
66
import findWorkspaceRoot from "find-yarn-workspace-root"
77
import { getPackageVersion } from "./getPackageVersion"
8-
import { execSync } from "child_process"
8+
//import { execSync } from "child_process"
99

10-
const isVerbose = global.patchPackageIsVerbose
10+
//const isVerbose = global.patchPackageIsVerbose
1111
const isDebug = global.patchPackageIsDebug
1212

1313
export function getPackageResolution({
@@ -91,6 +91,12 @@ export function getPackageResolution({
9191
)
9292
}
9393

94+
// TODO validate: declaredVersion must not be wildcard
95+
return { version: declaredVersion }
96+
97+
// TODO dont use lockfiles at all?
98+
// package versions should be pinned in /package.json, so it works with all package managers at all times
99+
/*
94100
const lockfile = require("js-yaml").load(
95101
require("fs").readFileSync(join(appPath, "pnpm-lock.yaml"), "utf8"),
96102
)
@@ -174,6 +180,7 @@ export function getPackageResolution({
174180
)
175181
}
176182
return { version: resolvedVersion }
183+
*/
177184
} else {
178185
const lockfile = require(join(
179186
appPath,

β€Žsrc/makePatch.tsβ€Ž

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ import { resolveRelativeFileDependencies } from "./resolveRelativeFileDependenci
2525
import { getPackageResolution } from "./getPackageResolution"
2626
import { parsePatchFile } from "./patch/parse"
2727
import { gzipSync } from "zlib"
28-
import { getPackageVersion } from "./getPackageVersion"
28+
//import { getPackageVersion } from "./getPackageVersion"
2929
import {
3030
maybePrintIssueCreationPrompt,
3131
openIssueCreationLink,
3232
} from "./createIssue"
33+
import { quote as shlexQuote } from "shlex"
3334

3435
const isVerbose = global.patchPackageIsVerbose
3536
const isDebug = global.patchPackageIsDebug
@@ -123,15 +124,25 @@ export function makePatch({
123124
}),
124125
)
125126

127+
/*
126128
// originCommit is more precise than pkg.version
127129
if (isDebug) {
128130
console.log(
129131
`patch-package/makePatch: resolvedVersion.originCommit = ${resolvedVersion.originCommit}`,
130132
)
133+
console.log(
134+
`patch-package/makePatch: resolvedVersion.version = ${resolvedVersion.version}`,
135+
)
131136
}
132137
const packageVersion =
133138
resolvedVersion.originCommit ||
134139
getPackageVersion(join(resolve(packageDetails.path), "package.json"))
140+
*/
141+
142+
// this is broken when installing from git -> version can be a pseudo-version like 1.0.0-canary
143+
//const packageVersion = getPackageVersion(join(resolve(packageDetails.path), "package.json"))
144+
145+
const packageVersion = resolvedVersion.version
135146

136147
if (isDebug) {
137148
console.log(`patch-package/makePatch: packageVersion = ${packageVersion}`)
@@ -359,6 +370,36 @@ export function makePatch({
359370
}
360371
})
361372

373+
const patchPackageVersion = require("../package.json").version
374+
375+
// patchfiles are parsed in patch/parse.ts function parsePatchLines
376+
// -> header comments are ignored
377+
let diffHeader = ""
378+
diffHeader += `# generated by patch-package ${patchPackageVersion} on ${new Date().toLocaleString(
379+
"lt",
380+
)}\n`
381+
diffHeader += `#\n`
382+
const prettyArgv = process.argv.slice()
383+
if (prettyArgv[0].match(/node/)) {
384+
prettyArgv[0] = "npx"
385+
}
386+
if (prettyArgv[1].match(/patch-package/)) {
387+
prettyArgv[1] = "patch-package"
388+
}
389+
diffHeader += `# command:\n`
390+
diffHeader += `# ${prettyArgv.map((a) => shlexQuote(a)).join(" ")}\n`
391+
diffHeader += `#\n`
392+
diffHeader += `# declared package:\n`
393+
diffHeader += `# ${packageDetails.name}: ${resolvedVersion.version}\n` // TODO rename to declaredVersion
394+
if (packageDetails.packageNames.length > 1) {
395+
diffHeader += `#\n`
396+
diffHeader += `# package names:\n`
397+
packageDetails.packageNames.forEach((packageName) => {
398+
diffHeader += `# ${packageName}\n`
399+
})
400+
}
401+
diffHeader += `#\n`
402+
362403
const patchFileName = createPatchFileName({
363404
packageDetails,
364405
packageVersion,
@@ -369,7 +410,7 @@ export function makePatch({
369410
// scoped package
370411
mkdirSync(dirname(patchPath))
371412
}
372-
writeFileSync(patchPath, diffResult.stdout)
413+
writeFileSync(patchPath, diffHeader + diffResult.stdout)
373414
console.log(
374415
`${chalk.green("βœ”")} Created file ${join(patchDir, patchFileName)}\n`,
375416
)
@@ -397,9 +438,18 @@ function createPatchFileName({
397438
packageDetails: PackageDetails
398439
packageVersion: string
399440
}) {
441+
const packageVersionFilename = packageVersion.includes("#")
442+
? packageVersion.split("#")[1] // extract commit hash
443+
: packageVersion.replace(/\//g, "_")
444+
if (isVerbose) {
445+
console.log(
446+
`patch-package/makePatch: packageVersion ${packageVersion} -> packageVersionFilename ${packageVersionFilename}`,
447+
)
448+
}
449+
400450
const packageNames = packageDetails.packageNames
401451
.map((name) => name.replace(/\//g, "+"))
402452
.join("++")
403453

404-
return `${packageNames}+${packageVersion}.patch`
454+
return `${packageNames}+${packageVersionFilename}.patch`
405455
}

β€Žsrc/patch/parse.tsβ€Ž

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,16 @@ function parsePatchLines(
175175
const line = lines[i]
176176

177177
if (state === "parsing header") {
178-
if (line.startsWith("@@")) {
178+
if (line.startsWith("#")) {
179+
// ignore metadata comment line in header
180+
// in the unidiff format, all header lines before `--- a/` are comment lines
181+
// git-diff uses these comment lines to store metadata
182+
// https://stackoverflow.com/questions/18979120
183+
if (global.patchPackageIsDebug) {
184+
console.log(`patch-package/patch/parse: ignore comment line: ${line}`)
185+
}
186+
continue
187+
} else if (line.startsWith("@@")) {
179188
state = "parsing hunks"
180189
currentFilePatch.hunks = []
181190
i--
@@ -206,9 +215,10 @@ function parsePatchLines(
206215
} else if (line.startsWith("rename to ")) {
207216
currentFilePatch.renameTo = line.slice("rename to ".length).trim()
208217
} else if (line.startsWith("index ")) {
209-
const match = line.match(/(\w+)\.\.(\w+)/)
218+
const match = line.match(/(\w+)\.\.(\w+)/) // TODO match whole line
219+
//const match = line.match(/^index (\w+)\.\.(\w+)$/)
210220
if (!match) {
211-
continue
221+
continue // TODO handle parse error?
212222
}
213223
currentFilePatch.beforeHash = match[1]
214224
currentFilePatch.afterHash = match[2]
File renamed without changes.

β€Žtslint.jsonβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"object-literal-key-quotes": [false],
1919
"max-line-length": false,
2020
"no-default-export": true,
21-
"no-namespace": false
21+
"no-namespace": false,
22+
"comment-format": false
2223
},
2324
"rulesDirectory": []
2425
}

0 commit comments

Comments
Β (0)