1- import { bold , green , red , yellow } from "chalk"
1+ import chalk from "chalk"
22import { getPatchFiles } from "./patchFs"
33import { executeEffects } from "./patch/apply"
4- import { existsSync , readFileSync } from "fs-extra"
4+ import { existsSync } from "fs-extra"
55import { join , resolve } from "./path"
66import { posix } from "path"
7- import { getPackageDetailsFromPatchFilename } from "./PackageDetails"
8- import { parsePatchFile } from "./patch/parse"
7+ import {
8+ getPackageDetailsFromPatchFilename ,
9+ PackageDetails ,
10+ } from "./PackageDetails"
911import { reversePatch } from "./patch/reverse"
1012import isCi from "is-ci"
1113import semver from "semver"
14+ import { readPatch } from "./patch/read"
1215
1316// don't want to exit(1) on postinsall locally.
1417// see https://github.com/ds300/patch-package/issues/86
@@ -34,9 +37,10 @@ function getInstalledPackageVersion({
3437 const packageDir = join ( appPath , path )
3538 if ( ! existsSync ( packageDir ) ) {
3639 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 } ` ,
4044 )
4145
4246 return null
@@ -47,28 +51,28 @@ function getInstalledPackageVersion({
4751 return semver . valid ( version )
4852}
4953
50- export const applyPatchesForApp = (
54+ export function applyPatchesForApp (
5155 appPath : string ,
5256 reverse : boolean ,
53- patchDir : string = "patches" ,
54- ) : void => {
57+ patchDir : string ,
58+ ) : void {
5559 const patchesDirectory = join ( appPath , patchDir )
5660 const files = findPatchFiles ( patchesDirectory )
5761
5862 if ( files . length === 0 ) {
59- console . error ( red ( "No patch files found" ) )
63+ console . error ( chalk . red ( "No patch files found" ) )
6064 return
6165 }
6266
6367 files . forEach ( filename => {
64- const details = getPackageDetailsFromPatchFilename ( filename )
68+ const packageDetails = getPackageDetailsFromPatchFilename ( filename )
6569
66- if ( ! details ) {
70+ if ( ! packageDetails ) {
6771 console . warn ( `Unrecognized patch file in patches directory ${ filename } ` )
6872 return
6973 }
7074
71- const { name, version, path, pathSpecifier } = details
75+ const { name, version, path, pathSpecifier } = packageDetails
7276
7377 const installedPackageVersion = getInstalledPackageVersion ( {
7478 appPath,
@@ -80,7 +84,14 @@ export const applyPatchesForApp = (
8084 return
8185 }
8286
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+ ) {
8495 // yay patch was applied successfully
8596 // print warning if version mismatch
8697 if ( installedPackageVersion !== version ) {
@@ -92,7 +103,9 @@ export const applyPatchesForApp = (
92103 path,
93104 } )
94105 } else {
95- console . log ( `${ bold ( pathSpecifier ) } @${ version } ${ green ( "✔" ) } ` )
106+ console . log (
107+ `${ chalk . bold ( pathSpecifier ) } @${ version } ${ chalk . green ( "✔" ) } ` ,
108+ )
96109 }
97110 } else {
98111 // completely failed to apply patch
@@ -119,12 +132,18 @@ export const applyPatchesForApp = (
119132 } )
120133}
121134
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 } )
128147 try {
129148 executeEffects ( reverse ? reversePatch ( patch ) : patch , { dryRun : false } )
130149 } catch ( e ) {
@@ -152,18 +171,18 @@ function printVersionMismatchWarning({
152171 path : string
153172} ) {
154173 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
156175
157176 Don't worry! This is probably fine. The patch was still applied
158177 successfully. Here's the deets:
159178
160179 Patch file created for
161180
162- ${ packageName } @${ bold ( originalVersion ) }
181+ ${ packageName } @${ chalk . bold ( originalVersion ) }
163182
164183 applied to
165184
166- ${ packageName } @${ bold ( actualVersion ) }
185+ ${ packageName } @${ chalk . bold ( actualVersion ) }
167186
168187 At path
169188
@@ -173,7 +192,7 @@ ${red("Warning:")} patch-package detected a patch file version mismatch
173192 breakage even though the patch was applied successfully. Make sure the package
174193 still behaves like you expect (you wrote tests, right?) and then run
175194
176- ${ bold ( `patch-package ${ pathSpecifier } ` ) }
195+ ${ chalk . bold ( `patch-package ${ pathSpecifier } ` ) }
177196
178197 to update the version in the patch file name and make this warning go away.
179198` )
@@ -191,8 +210,8 @@ function printBrokenPatchFileError({
191210 pathSpecifier : string
192211} ) {
193212 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` ,
196215 ) }
197216
198217 ${ path }
@@ -231,13 +250,13 @@ function printPatchApplictionFailureError({
231250 pathSpecifier : string
232251} ) {
233252 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` ,
236255 ) }
237256
238257 ${ path }
239258
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
241260 made the patch file for it. This introduced conflicts with your patch,
242261 just like a merge conflict in Git when separate incompatible changes are
243262 made to the same piece of code.
@@ -256,7 +275,7 @@ ${red.bold("**ERROR**")} ${red(
256275
257276 Info:
258277 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 ) }
261280` )
262281}
0 commit comments