Skip to content

Commit c54bbfd

Browse files
committed
feat: add --git-ignore option
When set, excludes git ignored files from patch creation.
1 parent 50f73bd commit c54bbfd

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ team.
130130
- `--patch-dir`
131131

132132
Specify the name for the directory in which to put the patch files.
133+
134+
- `--git-ignore`
135+
136+
By default, patch-package creates patches disregarding your git-ignore
137+
settings. Set this option to exclude git-ignored files from patches.
133138

134139
#### Nested packages
135140

src/index.ts

+9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const argv = minimist(process.argv.slice(2), {
1919
"reverse",
2020
"help",
2121
"version",
22+
"git-ignore",
2223
],
2324
string: ["patch-dir"],
2425
})
@@ -56,6 +57,7 @@ if (argv.version || argv.v) {
5657
appPath,
5758
argv["use-yarn"] ? "yarn" : null,
5859
)
60+
const gitignore = argv["git-ignore"]
5961
packageNames.forEach((packagePathSpecifier: string) => {
6062
makePatch({
6163
packagePathSpecifier,
@@ -64,6 +66,7 @@ if (argv.version || argv.v) {
6466
includePaths,
6567
excludePaths,
6668
patchDir,
69+
gitignore,
6770
})
6871
})
6972
} else {
@@ -128,5 +131,11 @@ Usage:
128131
${chalk.bold("--patch-dir")}
129132
130133
Specify the name for the directory in which to put the patch files.
134+
135+
${chalk.bold("--git-ignore")}
136+
137+
By default, patch-package creates patches disregarding your git-ignore
138+
settings. Set this option to exclude git-ignored files from patches.
139+
131140
`)
132141
}

src/makePatch.ts

+31-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
mkdirSync,
1010
unlinkSync,
1111
mkdirpSync,
12+
readFileSync,
1213
} from "fs-extra"
1314
import { sync as rimraf } from "rimraf"
1415
import { 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", "[email protected]")
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(/^\/node_modules\/?$\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

Comments
 (0)