Skip to content

Commit

Permalink
feat: add --git-ignore option
Browse files Browse the repository at this point in the history
When set, excludes git ignored files from patch creation.
  • Loading branch information
gomain committed Sep 10, 2020
1 parent 50f73bd commit c54bbfd
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ team.
- `--patch-dir`

Specify the name for the directory in which to put the patch files.

- `--git-ignore`

By default, patch-package creates patches disregarding your git-ignore
settings. Set this option to exclude git-ignored files from patches.

#### Nested packages

Expand Down
9 changes: 9 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const argv = minimist(process.argv.slice(2), {
"reverse",
"help",
"version",
"git-ignore",
],
string: ["patch-dir"],
})
Expand Down Expand Up @@ -56,6 +57,7 @@ if (argv.version || argv.v) {
appPath,
argv["use-yarn"] ? "yarn" : null,
)
const gitignore = argv["git-ignore"]
packageNames.forEach((packagePathSpecifier: string) => {
makePatch({
packagePathSpecifier,
Expand All @@ -64,6 +66,7 @@ if (argv.version || argv.v) {
includePaths,
excludePaths,
patchDir,
gitignore,
})
})
} else {
Expand Down Expand Up @@ -128,5 +131,11 @@ Usage:
${chalk.bold("--patch-dir")}
Specify the name for the directory in which to put the patch files.
${chalk.bold("--git-ignore")}
By default, patch-package creates patches disregarding your git-ignore
settings. Set this option to exclude git-ignored files from patches.
`)
}
34 changes: 31 additions & 3 deletions src/makePatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
mkdirSync,
unlinkSync,
mkdirpSync,
readFileSync,
} from "fs-extra"
import { sync as rimraf } from "rimraf"
import { copySync } from "fs-extra"
Expand Down Expand Up @@ -41,13 +42,15 @@ export function makePatch({
includePaths,
excludePaths,
patchDir,
gitignore,
}: {
packagePathSpecifier: string
appPath: string
packageManager: PackageManager
includePaths: RegExp
excludePaths: RegExp
patchDir: string
gitignore: boolean
}) {
const packageDetails = getPatchDetailsFromCliString(packagePathSpecifier)

Expand Down Expand Up @@ -167,15 +170,40 @@ export function makePatch({

// commit the package
console.info(chalk.grey("•"), "Diffing your files with clean files")
writeFileSync(join(tmpRepo.name, ".gitignore"), "!/node_modules\n\n")
git("init")
git("config", "--local", "user.name", "patch-package")
git("config", "--local", "user.email", "patch@pack.age")

if (gitignore) {
// pertain project's .gitignore and .git/info/exclude
// but remove ignoring node_modules/
const removeIgnoreNodeModules = (str: string): string =>
str.replace(/^\/node_modules\/?$\n/gm, "")
const gitIgnorePath = join(appPath, ".gitignore")
const gitignoreContent: string = existsSync(gitIgnorePath)
? readFileSync(gitIgnorePath, {
encoding: "utf-8",
})
: ""
const gitInfoExcludePath = join(appPath, ".git", "info", "exclude")
const gitInfoExcludeContent: string = existsSync(gitInfoExcludePath)
? readFileSync(gitInfoExcludePath, { encoding: "utf-8" })
: ""
writeFileSync(
join(tmpRepo.name, ".gitignore"),
[gitInfoExcludeContent, gitignoreContent, "\n"]
.map(removeIgnoreNodeModules)
.join("\n"),
)
}

// remove ignored files first
removeIgnoredFiles(tmpRepoPackagePath, includePaths, excludePaths)

git("add", "-f", packageDetails.path)
const gitAdd = (...args: string[]) =>
git("add", ...[...(gitignore ? [] : ["-f"]), ...args])

gitAdd(packageDetails.path)
git("commit", "--allow-empty", "-m", "init")

// replace package with user's version
Expand All @@ -192,7 +220,7 @@ export function makePatch({
removeIgnoredFiles(tmpRepoPackagePath, includePaths, excludePaths)

// stage all files
git("add", "-f", packageDetails.path)
gitAdd(packageDetails.path)

// get diff of changes
const diffResult = git(
Expand Down

0 comments on commit c54bbfd

Please sign in to comment.