9
9
mkdirSync ,
10
10
unlinkSync ,
11
11
mkdirpSync ,
12
+ readFileSync ,
12
13
} from "fs-extra"
13
14
import { sync as rimraf } from "rimraf"
14
15
import { copySync } from "fs-extra"
@@ -41,13 +42,15 @@ export function makePatch({
41
42
includePaths,
42
43
excludePaths,
43
44
patchDir,
45
+ gitignore,
44
46
} : {
45
47
packagePathSpecifier : string
46
48
appPath : string
47
49
packageManager : PackageManager
48
50
includePaths : RegExp
49
51
excludePaths : RegExp
50
52
patchDir : string
53
+ gitignore : boolean
51
54
} ) {
52
55
const packageDetails = getPatchDetailsFromCliString ( packagePathSpecifier )
53
56
@@ -167,15 +170,40 @@ export function makePatch({
167
170
168
171
// commit the package
169
172
console . info ( chalk . grey ( "•" ) , "Diffing your files with clean files" )
170
- writeFileSync ( join ( tmpRepo . name , ".gitignore" ) , "!/node_modules\n\n" )
171
173
git ( "init" )
172
174
git ( "config" , "--local" , "user.name" , "patch-package" )
173
175
git ( "config" , "--local" , "user.email" , "[email protected] " )
174
176
177
+ if ( gitignore ) {
178
+ // pertain project's .gitignore and .git/info/exclude
179
+ // but remove ignoring node_modules/
180
+ const removeIgnoreNodeModules = ( str : string ) : string =>
181
+ str . replace ( / ^ \/ n o d e _ m o d u l e s \/ ? $ \n / gm, "" )
182
+ const gitIgnorePath = join ( appPath , ".gitignore" )
183
+ const gitignoreContent : string = existsSync ( gitIgnorePath )
184
+ ? readFileSync ( gitIgnorePath , {
185
+ encoding : "utf-8" ,
186
+ } )
187
+ : ""
188
+ const gitInfoExcludePath = join ( appPath , ".git" , "info" , "exclude" )
189
+ const gitInfoExcludeContent : string = existsSync ( gitInfoExcludePath )
190
+ ? readFileSync ( gitInfoExcludePath , { encoding : "utf-8" } )
191
+ : ""
192
+ writeFileSync (
193
+ join ( tmpRepo . name , ".gitignore" ) ,
194
+ [ gitInfoExcludeContent , gitignoreContent , "\n" ]
195
+ . map ( removeIgnoreNodeModules )
196
+ . join ( "\n" ) ,
197
+ )
198
+ }
199
+
175
200
// remove ignored files first
176
201
removeIgnoredFiles ( tmpRepoPackagePath , includePaths , excludePaths )
177
202
178
- git ( "add" , "-f" , packageDetails . path )
203
+ const gitAdd = ( ...args : string [ ] ) =>
204
+ git ( "add" , ...[ ...( gitignore ? [ ] : [ "-f" ] ) , ...args ] )
205
+
206
+ gitAdd ( packageDetails . path )
179
207
git ( "commit" , "--allow-empty" , "-m" , "init" )
180
208
181
209
// replace package with user's version
@@ -192,7 +220,7 @@ export function makePatch({
192
220
removeIgnoredFiles ( tmpRepoPackagePath , includePaths , excludePaths )
193
221
194
222
// stage all files
195
- git ( "add" , "-f" , packageDetails . path )
223
+ gitAdd ( packageDetails . path )
196
224
197
225
// get diff of changes
198
226
const diffResult = git (
0 commit comments