99 mkdirSync ,
1010 unlinkSync ,
1111 mkdirpSync ,
12+ readFileSync ,
1213} from "fs-extra"
1314import { sync as rimraf } from "rimraf"
1415import { copySync } from "fs-extra"
@@ -41,13 +42,15 @@ export function makePatch({
4142 includePaths,
4243 excludePaths,
4344 patchDir,
45+ gitignore,
4446} : {
4547 packagePathSpecifier : string
4648 appPath : string
4749 packageManager : PackageManager
4850 includePaths : RegExp
4951 excludePaths : RegExp
5052 patchDir : string
53+ gitignore : boolean
5154} ) {
5255 const packageDetails = getPatchDetailsFromCliString ( packagePathSpecifier )
5356
@@ -167,15 +170,40 @@ export function makePatch({
167170
168171 // commit the package
169172 console . info ( chalk . grey ( "•" ) , "Diffing your files with clean files" )
170- writeFileSync ( join ( tmpRepo . name , ".gitignore" ) , "!/node_modules\n\n" )
171173 git ( "init" )
172174 git ( "config" , "--local" , "user.name" , "patch-package" )
173175 git ( "config" , "--local" , "user.email" , "patch@pack.age" )
174176
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+
175200 // remove ignored files first
176201 removeIgnoredFiles ( tmpRepoPackagePath , includePaths , excludePaths )
177202
178- git ( "add" , "-f" , packageDetails . path )
203+ const gitAdd = ( ...args : string [ ] ) =>
204+ git ( "add" , ...[ ...( gitignore ? [ ] : [ "-f" ] ) , ...args ] )
205+
206+ gitAdd ( packageDetails . path )
179207 git ( "commit" , "--allow-empty" , "-m" , "init" )
180208
181209 // replace package with user's version
@@ -192,7 +220,7 @@ export function makePatch({
192220 removeIgnoredFiles ( tmpRepoPackagePath , includePaths , excludePaths )
193221
194222 // stage all files
195- git ( "add" , "-f" , packageDetails . path )
223+ gitAdd ( packageDetails . path )
196224
197225 // get diff of changes
198226 const diffResult = git (
0 commit comments