Skip to content

Commit

Permalink
Merge pull request ds300#225 from mikehardy/patch-1
Browse files Browse the repository at this point in the history
docs: add '--patch-dir' to command-line help
  • Loading branch information
ds300 authored and gomain committed Sep 7, 2020
2 parents 2696a0f + 8856b54 commit ac5afd6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/filterFiles.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,49 @@
import { join } from "./path"
import { removeSync } from "fs-extra"
import klawSync from "klaw-sync"
import { spawnSafeSync } from "./spawnSafe"

const gitExcludePaths = (dir: string): RegExp => {
const anyRegExp = (regExps: RegExp[], flags?: string): RegExp =>
regExps.length <= 0
? /(?!)/ // never matches
: new RegExp(regExps.map(regExp => regExp.source).join("|"), flags)
const escapeRegExp = (str: string): string =>
str.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&")
const gitExcludeFilesResult = spawnSafeSync(
"git",
["ls-files", "-o", "-i", "--exclude-standard"],
{
cwd: dir,
throwOnError: false,
logStdErrOnError: false,
},
)
return anyRegExp(
gitExcludeFilesResult.status === 0
? gitExcludeFilesResult.stdout
.toString()
.split(/\n/g)
.map(escapeRegExp)
.map(escaped => new RegExp(escaped))
: [],
"i",
)
}

export function removeIgnoredFiles(
dir: string,
includePaths: RegExp,
excludePaths: RegExp,
) {
const gitIgnoredPaths = gitExcludePaths(dir)
klawSync(dir, { nodir: true })
.map(item => item.path.slice(`${dir}/`.length))
.filter(
relativePath =>
!relativePath.match(includePaths) || relativePath.match(excludePaths),
!relativePath.match(includePaths) ||
relativePath.match(excludePaths) ||
relativePath.match(gitIgnoredPaths),
)
.forEach(relativePath => removeSync(join(dir, relativePath)))
}
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,9 @@ Usage:
${chalk.bold("--case-sensitive-path-filtering")}
Make regexps used in --include or --exclude filters case-sensitive.
${chalk.bold("--patch-dir")}
Specify the name for the directory in which to put the patch files.
`)
}

0 comments on commit ac5afd6

Please sign in to comment.