1
- import { bold , green , red , yellow } from "chalk"
1
+ import chalk from "chalk"
2
2
import { getPatchFiles } from "./patchFs"
3
3
import { executeEffects } from "./patch/apply"
4
- import { existsSync , readFileSync } from "fs-extra"
4
+ import { existsSync } from "fs-extra"
5
5
import { join , resolve } from "./path"
6
6
import { posix } from "path"
7
- import { getPackageDetailsFromPatchFilename } from "./PackageDetails"
8
- import { parsePatchFile } from "./patch/parse"
7
+ import {
8
+ getPackageDetailsFromPatchFilename ,
9
+ PackageDetails ,
10
+ } from "./PackageDetails"
9
11
import { reversePatch } from "./patch/reverse"
10
12
import isCi from "is-ci"
11
13
import semver from "semver"
14
+ import { readPatch } from "./patch/read"
12
15
13
16
// don't want to exit(1) on postinsall locally.
14
17
// see https://github.com/ds300/patch-package/issues/86
@@ -34,9 +37,10 @@ function getInstalledPackageVersion({
34
37
const packageDir = join ( appPath , path )
35
38
if ( ! existsSync ( packageDir ) ) {
36
39
console . log (
37
- `${ yellow ( "Warning:" ) } Patch file found for package ${ posix . basename (
38
- pathSpecifier ,
39
- ) } ` + ` which is not present at ${ packageDir } ` ,
40
+ `${ chalk . yellow (
41
+ "Warning:" ,
42
+ ) } Patch file found for package ${ posix . basename ( pathSpecifier ) } ` +
43
+ ` which is not present at ${ packageDir } ` ,
40
44
)
41
45
42
46
return null
@@ -47,28 +51,28 @@ function getInstalledPackageVersion({
47
51
return semver . valid ( version )
48
52
}
49
53
50
- export const applyPatchesForApp = (
54
+ export function applyPatchesForApp (
51
55
appPath : string ,
52
56
reverse : boolean ,
53
- patchDir : string = "patches" ,
54
- ) : void => {
57
+ patchDir : string ,
58
+ ) : void {
55
59
const patchesDirectory = join ( appPath , patchDir )
56
60
const files = findPatchFiles ( patchesDirectory )
57
61
58
62
if ( files . length === 0 ) {
59
- console . error ( red ( "No patch files found" ) )
63
+ console . error ( chalk . red ( "No patch files found" ) )
60
64
return
61
65
}
62
66
63
67
files . forEach ( filename => {
64
- const details = getPackageDetailsFromPatchFilename ( filename )
68
+ const packageDetails = getPackageDetailsFromPatchFilename ( filename )
65
69
66
- if ( ! details ) {
70
+ if ( ! packageDetails ) {
67
71
console . warn ( `Unrecognized patch file in patches directory ${ filename } ` )
68
72
return
69
73
}
70
74
71
- const { name, version, path, pathSpecifier } = details
75
+ const { name, version, path, pathSpecifier } = packageDetails
72
76
73
77
const installedPackageVersion = getInstalledPackageVersion ( {
74
78
appPath,
@@ -80,7 +84,14 @@ export const applyPatchesForApp = (
80
84
return
81
85
}
82
86
83
- if ( applyPatch ( resolve ( patchesDirectory , filename ) as string , reverse ) ) {
87
+ if (
88
+ applyPatch ( {
89
+ patchFilePath : resolve ( patchesDirectory , filename ) as string ,
90
+ reverse,
91
+ packageDetails,
92
+ patchDir,
93
+ } )
94
+ ) {
84
95
// yay patch was applied successfully
85
96
// print warning if version mismatch
86
97
if ( installedPackageVersion !== version ) {
@@ -92,7 +103,9 @@ export const applyPatchesForApp = (
92
103
path,
93
104
} )
94
105
} else {
95
- console . log ( `${ bold ( pathSpecifier ) } @${ version } ${ green ( "✔" ) } ` )
106
+ console . log (
107
+ `${ chalk . bold ( pathSpecifier ) } @${ version } ${ chalk . green ( "✔" ) } ` ,
108
+ )
96
109
}
97
110
} else {
98
111
// completely failed to apply patch
@@ -119,12 +132,18 @@ export const applyPatchesForApp = (
119
132
} )
120
133
}
121
134
122
- export const applyPatch = (
123
- patchFilePath : string ,
124
- reverse : boolean ,
125
- ) : boolean => {
126
- const patchFileContents = readFileSync ( patchFilePath ) . toString ( )
127
- const patch = parsePatchFile ( patchFileContents )
135
+ export function applyPatch ( {
136
+ patchFilePath,
137
+ reverse,
138
+ packageDetails,
139
+ patchDir,
140
+ } : {
141
+ patchFilePath : string
142
+ reverse : boolean
143
+ packageDetails : PackageDetails
144
+ patchDir : string
145
+ } ) : boolean {
146
+ const patch = readPatch ( { patchFilePath, packageDetails, patchDir } )
128
147
try {
129
148
executeEffects ( reverse ? reversePatch ( patch ) : patch , { dryRun : false } )
130
149
} catch ( e ) {
@@ -152,18 +171,18 @@ function printVersionMismatchWarning({
152
171
path : string
153
172
} ) {
154
173
console . warn ( `
155
- ${ red ( "Warning:" ) } patch-package detected a patch file version mismatch
174
+ ${ chalk . red ( "Warning:" ) } patch-package detected a patch file version mismatch
156
175
157
176
Don't worry! This is probably fine. The patch was still applied
158
177
successfully. Here's the deets:
159
178
160
179
Patch file created for
161
180
162
- ${ packageName } @${ bold ( originalVersion ) }
181
+ ${ packageName } @${ chalk . bold ( originalVersion ) }
163
182
164
183
applied to
165
184
166
- ${ packageName } @${ bold ( actualVersion ) }
185
+ ${ packageName } @${ chalk . bold ( actualVersion ) }
167
186
168
187
At path
169
188
@@ -173,7 +192,7 @@ ${red("Warning:")} patch-package detected a patch file version mismatch
173
192
breakage even though the patch was applied successfully. Make sure the package
174
193
still behaves like you expect (you wrote tests, right?) and then run
175
194
176
- ${ bold ( `patch-package ${ pathSpecifier } ` ) }
195
+ ${ chalk . bold ( `patch-package ${ pathSpecifier } ` ) }
177
196
178
197
to update the version in the patch file name and make this warning go away.
179
198
` )
@@ -191,8 +210,8 @@ function printBrokenPatchFileError({
191
210
pathSpecifier : string
192
211
} ) {
193
212
console . error ( `
194
- ${ red . bold ( "**ERROR**" ) } ${ red (
195
- `Failed to apply patch for package ${ bold ( packageName ) } at path` ,
213
+ ${ chalk . red . bold ( "**ERROR**" ) } ${ chalk . red (
214
+ `Failed to apply patch for package ${ chalk . bold ( packageName ) } at path` ,
196
215
) }
197
216
198
217
${ path }
@@ -231,13 +250,13 @@ function printPatchApplictionFailureError({
231
250
pathSpecifier : string
232
251
} ) {
233
252
console . error ( `
234
- ${ red . bold ( "**ERROR**" ) } ${ red (
235
- `Failed to apply patch for package ${ bold ( packageName ) } at path` ,
253
+ ${ chalk . red . bold ( "**ERROR**" ) } ${ chalk . red (
254
+ `Failed to apply patch for package ${ chalk . bold ( packageName ) } at path` ,
236
255
) }
237
256
238
257
${ path }
239
258
240
- This error was caused because ${ bold ( packageName ) } has changed since you
259
+ This error was caused because ${ chalk . bold ( packageName ) } has changed since you
241
260
made the patch file for it. This introduced conflicts with your patch,
242
261
just like a merge conflict in Git when separate incompatible changes are
243
262
made to the same piece of code.
@@ -256,7 +275,7 @@ ${red.bold("**ERROR**")} ${red(
256
275
257
276
Info:
258
277
Patch file: patches/${ patchFileName }
259
- Patch was made for version: ${ green . bold ( originalVersion ) }
260
- Installed version: ${ red . bold ( actualVersion ) }
278
+ Patch was made for version: ${ chalk . green . bold ( originalVersion ) }
279
+ Installed version: ${ chalk . red . bold ( actualVersion ) }
261
280
` )
262
281
}
0 commit comments