Skip to content

Commit ac5afd6

Browse files
ds300gomain
authored andcommitted
Merge pull request ds300#225 from mikehardy/patch-1
docs: add '--patch-dir' to command-line help
2 parents 2696a0f + 8856b54 commit ac5afd6

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/filterFiles.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,49 @@
11
import { join } from "./path"
22
import { removeSync } from "fs-extra"
33
import klawSync from "klaw-sync"
4+
import { spawnSafeSync } from "./spawnSafe"
5+
6+
const gitExcludePaths = (dir: string): RegExp => {
7+
const anyRegExp = (regExps: RegExp[], flags?: string): RegExp =>
8+
regExps.length <= 0
9+
? /(?!)/ // never matches
10+
: new RegExp(regExps.map(regExp => regExp.source).join("|"), flags)
11+
const escapeRegExp = (str: string): string =>
12+
str.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&")
13+
const gitExcludeFilesResult = spawnSafeSync(
14+
"git",
15+
["ls-files", "-o", "-i", "--exclude-standard"],
16+
{
17+
cwd: dir,
18+
throwOnError: false,
19+
logStdErrOnError: false,
20+
},
21+
)
22+
return anyRegExp(
23+
gitExcludeFilesResult.status === 0
24+
? gitExcludeFilesResult.stdout
25+
.toString()
26+
.split(/\n/g)
27+
.map(escapeRegExp)
28+
.map(escaped => new RegExp(escaped))
29+
: [],
30+
"i",
31+
)
32+
}
433

534
export function removeIgnoredFiles(
635
dir: string,
736
includePaths: RegExp,
837
excludePaths: RegExp,
938
) {
39+
const gitIgnoredPaths = gitExcludePaths(dir)
1040
klawSync(dir, { nodir: true })
1141
.map(item => item.path.slice(`${dir}/`.length))
1242
.filter(
1343
relativePath =>
14-
!relativePath.match(includePaths) || relativePath.match(excludePaths),
44+
!relativePath.match(includePaths) ||
45+
relativePath.match(excludePaths) ||
46+
relativePath.match(gitIgnoredPaths),
1547
)
1648
.forEach(relativePath => removeSync(join(dir, relativePath)))
1749
}

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,5 +124,9 @@ Usage:
124124
${chalk.bold("--case-sensitive-path-filtering")}
125125
126126
Make regexps used in --include or --exclude filters case-sensitive.
127+
128+
${chalk.bold("--patch-dir")}
129+
130+
Specify the name for the directory in which to put the patch files.
127131
`)
128132
}

0 commit comments

Comments
 (0)