@@ -62,7 +62,7 @@ function getInstalledPackageVersion({
62
62
)
63
63
}
64
64
65
- exit ( )
65
+ throw new Error ( "applyPatches" )
66
66
}
67
67
68
68
const { version } = require ( join ( packageDir , "package.json" ) )
@@ -78,7 +78,7 @@ function getInstalledPackageVersion({
78
78
) } `,
79
79
)
80
80
81
- exit ( )
81
+ throw new Error ( "applyPatches" )
82
82
}
83
83
84
84
return result as string
@@ -87,10 +87,12 @@ function getInstalledPackageVersion({
87
87
export function applyPatchesForApp ( {
88
88
appPath,
89
89
reverse,
90
+ ignoreErrors,
90
91
patchDir,
91
92
} : {
92
93
appPath : string
93
94
reverse : boolean
95
+ ignoreErrors : boolean
94
96
patchDir : string
95
97
} ) : void {
96
98
const patchesDirectory = join ( appPath , patchDir )
@@ -101,91 +103,112 @@ export function applyPatchesForApp({
101
103
return
102
104
}
103
105
104
- files . forEach ( filename => {
105
- const packageDetails = getPackageDetailsFromPatchFilename ( filename )
106
+ let hasFailed = false
107
+ files . forEach ( ( filename , idx ) => {
108
+ try {
109
+ const packageDetails = getPackageDetailsFromPatchFilename ( filename )
106
110
107
- if ( ! packageDetails ) {
108
- console . warn ( `Unrecognized patch file in patches directory ${ filename } ` )
109
- return
110
- }
111
+ if ( ! packageDetails ) {
112
+ console . warn ( `Unrecognized patch file in patches directory ${ filename } ` )
113
+ return
114
+ }
111
115
112
- const {
113
- name,
114
- version,
115
- path,
116
- pathSpecifier,
117
- isDevOnly,
118
- patchFilename,
119
- } = packageDetails
120
-
121
- const installedPackageVersion = getInstalledPackageVersion ( {
122
- appPath,
123
- path,
124
- pathSpecifier,
125
- isDevOnly :
126
- isDevOnly ||
127
- // check for direct-dependents in prod
128
- ( process . env . NODE_ENV === "production" &&
129
- packageIsDevDependency ( { appPath, packageDetails } ) ) ,
130
- patchFilename,
131
- } )
132
- if ( ! installedPackageVersion ) {
133
- // it's ok we're in production mode and this is a dev only package
134
- console . log (
135
- `Skipping dev-only ${ chalk . bold ( pathSpecifier ) } @${ version } ${ chalk . blue (
136
- "✔" ,
137
- ) } `,
138
- )
139
- return
140
- }
116
+ const {
117
+ name,
118
+ version,
119
+ path,
120
+ pathSpecifier,
121
+ isDevOnly,
122
+ patchFilename,
123
+ } = packageDetails
141
124
142
- if (
143
- applyPatch ( {
144
- patchFilePath : resolve ( patchesDirectory , filename ) as string ,
145
- reverse,
146
- packageDetails,
147
- patchDir,
125
+ const installedPackageVersion = getInstalledPackageVersion ( {
126
+ appPath,
127
+ path,
128
+ pathSpecifier,
129
+ isDevOnly :
130
+ isDevOnly ||
131
+ // check for direct-dependents in prod
132
+ ( process . env . NODE_ENV === "production" &&
133
+ packageIsDevDependency ( { appPath, packageDetails } ) ) ,
134
+ patchFilename,
148
135
} )
149
- ) {
150
- // yay patch was applied successfully
151
- // print warning if version mismatch
152
- if ( installedPackageVersion !== version ) {
153
- printVersionMismatchWarning ( {
154
- packageName : name ,
155
- actualVersion : installedPackageVersion ,
156
- originalVersion : version ,
157
- pathSpecifier,
158
- path,
159
- } )
160
- } else {
136
+ if ( ! installedPackageVersion ) {
137
+ // it's ok we're in production mode and this is a dev only package
161
138
console . log (
162
- `${ chalk . bold ( pathSpecifier ) } @${ version } ${ chalk . green ( "✔" ) } ` ,
139
+ `Skipping dev-only ${ chalk . bold (
140
+ pathSpecifier ,
141
+ ) } @${ version } ${ chalk . blue ( "✔" ) } `,
163
142
)
143
+ return
164
144
}
165
- } else {
166
- // completely failed to apply patch
167
- // TODO: propagate useful error messages from patch application
168
- if ( installedPackageVersion === version ) {
169
- printBrokenPatchFileError ( {
170
- packageName : name ,
171
- patchFileName : filename ,
172
- pathSpecifier,
173
- path,
145
+
146
+ if (
147
+ applyPatch ( {
148
+ patchFilePath : resolve ( patchesDirectory , filename ) as string ,
149
+ reverse,
150
+ packageDetails,
151
+ patchDir,
174
152
} )
153
+ ) {
154
+ // yay patch was applied successfully
155
+ // print warning if version mismatch
156
+ if ( installedPackageVersion !== version ) {
157
+ printVersionMismatchWarning ( {
158
+ packageName : name ,
159
+ actualVersion : installedPackageVersion ,
160
+ originalVersion : version ,
161
+ pathSpecifier,
162
+ path,
163
+ } )
164
+ } else {
165
+ console . log (
166
+ `${ chalk . bold ( pathSpecifier ) } @${ version } ${ chalk . green ( "✔" ) } ` ,
167
+ )
168
+ }
175
169
} else {
176
- printPatchApplictionFailureError ( {
177
- packageName : name ,
178
- actualVersion : installedPackageVersion ,
179
- originalVersion : version ,
180
- patchFileName : filename ,
181
- path,
182
- pathSpecifier,
183
- } )
170
+ // completely failed to apply patch
171
+ // TODO: propagate useful error messages from patch application
172
+ if ( installedPackageVersion === version ) {
173
+ printBrokenPatchFileError ( {
174
+ packageName : name ,
175
+ patchFileName : filename ,
176
+ pathSpecifier,
177
+ path,
178
+ } )
179
+ } else {
180
+ printPatchApplictionFailureError ( {
181
+ packageName : name ,
182
+ actualVersion : installedPackageVersion ,
183
+ originalVersion : version ,
184
+ patchFileName : filename ,
185
+ path,
186
+ pathSpecifier,
187
+ } )
188
+ }
189
+
190
+ throw new Error ( "applyPatches" )
191
+ }
192
+ } catch ( err ) {
193
+ if ( err . message !== "applyPatches" ) {
194
+ throw err
195
+ }
196
+ if ( ! ignoreErrors ) {
197
+ exit ( )
198
+ }
199
+ hasFailed = true
200
+ if ( idx < files . length - 1 ) {
201
+ console . warn (
202
+ `${ chalk . yellow ( "Warning:" ) } Option ${ chalk . bold (
203
+ "--ignore-errors" ,
204
+ ) } was set, moving on to next patch.`,
205
+ )
184
206
}
185
-
186
- exit ( )
187
207
}
188
208
} )
209
+ if ( hasFailed ) {
210
+ exit ( )
211
+ }
189
212
}
190
213
191
214
export function applyPatch ( {
0 commit comments