1
1
import chalk from "chalk"
2
2
import { join , dirname , resolve } from "./path"
3
+ import { basename } from "path"
3
4
import { spawnSafeSync } from "./spawnSafe"
4
5
import { PackageManager } from "./detectPackageManager"
5
6
import { removeIgnoredFiles } from "./filterFiles"
@@ -25,7 +26,7 @@ import { resolveRelativeFileDependencies } from "./resolveRelativeFileDependenci
25
26
import { getPackageResolution } from "./getPackageResolution"
26
27
import { parsePatchFile } from "./patch/parse"
27
28
import { gzipSync } from "zlib"
28
- // import { getPackageVersion } from "./getPackageVersion"
29
+ import { getPackageVersion } from "./getPackageVersion"
29
30
import {
30
31
maybePrintIssueCreationPrompt ,
31
32
openIssueCreationLink ,
@@ -34,6 +35,7 @@ import { quote as shlexQuote } from "shlex"
34
35
35
36
const isVerbose = global . patchPackageIsVerbose
36
37
const isDebug = global . patchPackageIsDebug
38
+ const isTest = process . env . NODE_ENV == 'test'
37
39
38
40
function printNoPackageFoundError (
39
41
packageName : string ,
@@ -73,6 +75,13 @@ export function makePatch({
73
75
const packagePath = join ( appPath , packageDetails . path )
74
76
const packageJsonPath = join ( packagePath , "package.json" )
75
77
78
+ if ( isDebug ) {
79
+ console . log ( `patch-package/makePatch: appPath = ${ appPath } ` )
80
+ console . log ( `patch-package/makePatch: packagePath = ${ packagePath } ` )
81
+ console . log ( `patch-package/makePatch: appPackageJson:` )
82
+ console . dir ( appPackageJson )
83
+ }
84
+
76
85
if ( ! existsSync ( packageJsonPath ) ) {
77
86
printNoPackageFoundError ( packagePathSpecifier , packageJsonPath )
78
87
process . exit ( 1 )
@@ -102,6 +111,12 @@ export function makePatch({
102
111
103
112
console . info ( chalk . grey ( "•" ) , "Creating temporary folder" )
104
113
114
+ if ( isDebug ) {
115
+ console . log (
116
+ `patch-package/makePatch: tmpRepoNpmRoot = ${ tmpRepoNpmRoot } ` ,
117
+ )
118
+ }
119
+
105
120
const resolvedVersion = getPackageResolution ( {
106
121
packageDetails,
107
122
packageManager,
@@ -124,36 +139,79 @@ export function makePatch({
124
139
} ) ,
125
140
)
126
141
127
- /*
142
+ const declaredVersion = ( ( ) => {
143
+ var v = resolvedVersion . version
144
+ // https://docs.npmjs.com/cli/v7/configuring-npm/package-json
145
+ // <protocol>://[<user>[:<password>]@]<hostname>[:<port>][:][/]<path>[#<commit-ish> | #semver:<semver>]
146
+ // pnpm uses link: protocol instead of file:
147
+ // TODO add more protocols?
148
+ var m = v . match ( / ^ ( f i l e | l i n k | h t t p | h t t p s | g i t | g i t \+ h t t p s | g i t \+ h t t p | g i t \+ s s h | g i t \+ f i l e | g i t h u b ) : ( .+ ) $ / )
149
+ if ( m ) {
150
+ var protocol = m [ 1 ]
151
+ var location = m [ 2 ]
152
+ var isGit = protocol . startsWith ( 'git' )
153
+ var gitCommit = isGit ? location . split ( '#' ) . slice ( - 1 ) [ 0 ] : null
154
+ if ( isGit && ! gitCommit ) {
155
+ throw new Error ( `error: found wildcard git version ${ v } . package.json must pin the exact version of ${ packageDetails . name } in the format <protocol>:<packagePath>#<commitHash>` )
156
+ }
157
+ if ( isGit ) {
158
+ return { full : v , protocol, location, gitCommit }
159
+ }
160
+ else {
161
+ // sample: https://registry.yarnpkg.com/left-pad/-/left-pad-1.3.0.tgz#5b8a3a7765dfe001261dde915589e782f8c94d1e
162
+ // hash is sha1sum of tgz file
163
+ // -> use version number from package's package.json
164
+ var version = getPackageVersion ( join ( resolve ( packageDetails . path ) , "package.json" ) )
165
+ if ( isVerbose ) {
166
+ console . log ( `patch-package/makePatch: warning: using version ${ version } from ${ packageDetails . name } /package.json` )
167
+ }
168
+ return { version }
169
+ }
170
+ }
171
+ if ( ! v . match ( / ^ [ 0 - 9 ] + \. [ 0 - 9 ] + \. [ 0 - 9 ] + / ) ) {
172
+ throw new Error ( `error: found wildcard version. package.json must pin the exact version of ${ packageDetails . name } in the format <package>@<major>.<minor>.<patch>` )
173
+ }
174
+ return { full : v , version : v }
175
+ } ) ( )
176
+
177
+ const packageVersion = (
178
+ declaredVersion . version || declaredVersion . gitCommit || declaredVersion . full
179
+ )
180
+
128
181
// originCommit is more precise than pkg.version
129
182
if ( isDebug ) {
130
- console.log(
131
- `patch-package/makePatch: resolvedVersion.originCommit = ${resolvedVersion.originCommit}`,
132
- )
133
- console.log(
134
- `patch-package/makePatch: resolvedVersion.version = ${resolvedVersion.version}`,
135
- )
183
+ //console.log(`patch-package/makePatch: resolvedVersion.originCommit = ${resolvedVersion.originCommit}`)
184
+ console . log ( `patch-package/makePatch: resolvedVersion.version = ${ resolvedVersion . version } ` )
185
+ console . log ( `patch-package/makePatch: packageVersion = ${ packageVersion } ` )
136
186
}
137
- const packageVersion =
138
- resolvedVersion.originCommit ||
139
- getPackageVersion(join(resolve(packageDetails.path), "package.json"))
140
- */
187
+
188
+ //const packageVersion =
189
+ // resolvedVersion.originCommit ||
190
+ // getPackageVersion(join(resolve(packageDetails.path), "package.json"))
141
191
142
192
// this is broken when installing from git -> version can be a pseudo-version like 1.0.0-canary
143
193
//const packageVersion = getPackageVersion(join(resolve(packageDetails.path), "package.json"))
144
194
145
- const packageVersion = resolvedVersion . version
195
+ // TODO rename resolvedVersion -> declaredVersion
196
+
197
+ // FIXME false positive
198
+ // test integration-tests/create-issue/create-issue.test.ts
199
+ // -> patching left-pad prompts to submit an issue
200
+ // https://registry.yarnpkg.com/left-pad/-/left-pad-1.3.0.tgz#5b8a3a7765dfe001261dde915589e782f8c94d1e
201
+ // hash is sha checksum of tgz file -> just use the version 1.3.0
202
+ /*
203
+ const packageVersion = (
204
+ !resolvedVersion.version.match(/^(file:|link:)/) ? resolvedVersion.version :
205
+ getPackageVersion(join(resolve(packageDetails.path), "package.json"))
206
+ )
207
+ */
146
208
147
209
if ( isDebug ) {
210
+ console . log ( `patch-package/makePatch: resolvedVersion.version = ${ resolvedVersion . version } ` )
211
+ console . log ( `patch-package/makePatch: getPackageVersion -> ${ getPackageVersion ( join ( resolve ( packageDetails . path ) , "package.json" ) ) } ` )
148
212
console . log ( `patch-package/makePatch: packageVersion = ${ packageVersion } ` )
149
- console . log (
150
- `patch-package/makePatch: package path = ${ packageDetails . path } ` ,
151
- )
152
- console . log (
153
- `patch-package/makePatch: package path resolved = ${ resolve (
154
- packageDetails . path ,
155
- ) } `,
156
- )
213
+ console . log ( `patch-package/makePatch: package path = ${ packageDetails . path } ` )
214
+ console . log ( `patch-package/makePatch: package path resolved = ${ resolve ( packageDetails . path ) } ` )
157
215
}
158
216
159
217
// copy .npmrc/.yarnrc in case packages are hosted in private registry
@@ -375,9 +433,11 @@ export function makePatch({
375
433
// patchfiles are parsed in patch/parse.ts function parsePatchLines
376
434
// -> header comments are ignored
377
435
let diffHeader = ""
378
- diffHeader += `# generated by patch-package ${ patchPackageVersion } on ${ new Date ( ) . toLocaleString (
379
- "lt" ,
380
- ) } \n`
436
+ const dateStr = (
437
+ isTest ? '1980-01-01 00:00:00' : // mock date
438
+ new Date ( ) . toLocaleString ( "lt" )
439
+ )
440
+ diffHeader += `# generated by patch-package ${ patchPackageVersion } on ${ dateStr } \n`
381
441
diffHeader += `#\n`
382
442
const prettyArgv = process . argv . slice ( )
383
443
if ( prettyArgv [ 0 ] . match ( / n o d e / ) ) {
@@ -390,14 +450,26 @@ export function makePatch({
390
450
diffHeader += `# ${ prettyArgv . map ( ( a ) => shlexQuote ( a ) ) . join ( " " ) } \n`
391
451
diffHeader += `#\n`
392
452
diffHeader += `# declared package:\n`
393
- diffHeader += `# ${ packageDetails . name } : ${ resolvedVersion . version } \n` // TODO rename to declaredVersion
453
+ // TODO rename resolvedVersion.version to declaredVersion
454
+ const declaredPackageStr = (
455
+ isTest ? ( ( ) => {
456
+ const v = resolvedVersion . version
457
+ const b = basename ( v )
458
+ if ( b != v ) return `file:/mocked/path/to/${ b } ` // mock path // TODO keep the relative path? as declared in /package.json. see getPackageResolution "resolve relative file path"
459
+ return v
460
+ } ) ( ) :
461
+ resolvedVersion . version
462
+ )
463
+ diffHeader += `# ${ packageDetails . name } : ${ declaredPackageStr } \n`
464
+ /* redundant. this is visible from command, sample: npx patch-package wrap-ansi/string-width -> packageNames: wrap-ansi, string-width
394
465
if (packageDetails.packageNames.length > 1) {
395
466
diffHeader += `#\n`
396
467
diffHeader += `# package names:\n`
397
468
packageDetails.packageNames.forEach((packageName) => {
398
469
diffHeader += `# ${packageName}\n`
399
470
})
400
471
}
472
+ */
401
473
diffHeader += `#\n`
402
474
403
475
const patchFileName = createPatchFileName ( {
@@ -438,18 +510,9 @@ function createPatchFileName({
438
510
packageDetails : PackageDetails
439
511
packageVersion : string
440
512
} ) {
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
-
450
513
const packageNames = packageDetails . packageNames
451
514
. map ( ( name ) => name . replace ( / \/ / g, "+" ) )
452
515
. join ( "++" )
453
516
454
- return `${ packageNames } +${ packageVersionFilename } .patch`
517
+ return `${ packageNames } +${ packageVersion } .patch`
455
518
}
0 commit comments