diff --git a/src/filterFiles.ts b/src/filterFiles.ts index 60983b32..f5494d34 100644 --- a/src/filterFiles.ts +++ b/src/filterFiles.ts @@ -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))) } diff --git a/src/index.ts b/src/index.ts index ae7bb733..6aa117a0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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. `) }