|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import assert from "assert"; |
|
import path from "path"; |
|
import util from "util"; |
|
import minimatch from "minimatch"; |
|
|
|
const { Minimatch } = minimatch; |
|
|
|
const minimatchOpts = { dot: true, matchBase: true }; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function normalizePatterns(patterns) { |
|
if (Array.isArray(patterns)) { |
|
return patterns.filter(Boolean); |
|
} |
|
if (typeof patterns === "string" && patterns) { |
|
return [patterns]; |
|
} |
|
return []; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
function toMatcher(patterns) { |
|
if (patterns.length === 0) { |
|
return null; |
|
} |
|
return patterns.map(pattern => { |
|
if (/^\.[/\\]/u.test(pattern)) { |
|
return new Minimatch( |
|
pattern.slice(2), |
|
|
|
|
|
{ ...minimatchOpts, matchBase: false } |
|
); |
|
} |
|
return new Minimatch(pattern, minimatchOpts); |
|
}); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
function patternToJson({ includes, excludes }) { |
|
return { |
|
includes: includes && includes.map(m => m.pattern), |
|
excludes: excludes && excludes.map(m => m.pattern) |
|
}; |
|
} |
|
|
|
|
|
|
|
|
|
class OverrideTester { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static create(files, excludedFiles, basePath) { |
|
const includePatterns = normalizePatterns(files); |
|
const excludePatterns = normalizePatterns(excludedFiles); |
|
let endsWithWildcard = false; |
|
|
|
if (includePatterns.length === 0) { |
|
return null; |
|
} |
|
|
|
|
|
for (const pattern of includePatterns) { |
|
if (path.isAbsolute(pattern) || pattern.includes("..")) { |
|
throw new Error(`Invalid override pattern (expected relative path not containing '..'): ${pattern}`); |
|
} |
|
if (pattern.endsWith("*")) { |
|
endsWithWildcard = true; |
|
} |
|
} |
|
for (const pattern of excludePatterns) { |
|
if (path.isAbsolute(pattern) || pattern.includes("..")) { |
|
throw new Error(`Invalid override pattern (expected relative path not containing '..'): ${pattern}`); |
|
} |
|
} |
|
|
|
const includes = toMatcher(includePatterns); |
|
const excludes = toMatcher(excludePatterns); |
|
|
|
return new OverrideTester( |
|
[{ includes, excludes }], |
|
basePath, |
|
endsWithWildcard |
|
); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static and(a, b) { |
|
if (!b) { |
|
return a && new OverrideTester( |
|
a.patterns, |
|
a.basePath, |
|
a.endsWithWildcard |
|
); |
|
} |
|
if (!a) { |
|
return new OverrideTester( |
|
b.patterns, |
|
b.basePath, |
|
b.endsWithWildcard |
|
); |
|
} |
|
|
|
assert.strictEqual(a.basePath, b.basePath); |
|
return new OverrideTester( |
|
a.patterns.concat(b.patterns), |
|
a.basePath, |
|
a.endsWithWildcard || b.endsWithWildcard |
|
); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor(patterns, basePath, endsWithWildcard = false) { |
|
|
|
|
|
this.patterns = patterns; |
|
|
|
|
|
this.basePath = basePath; |
|
|
|
|
|
this.endsWithWildcard = endsWithWildcard; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
test(filePath) { |
|
if (typeof filePath !== "string" || !path.isAbsolute(filePath)) { |
|
throw new Error(`'filePath' should be an absolute path, but got ${filePath}.`); |
|
} |
|
const relativePath = path.relative(this.basePath, filePath); |
|
|
|
return this.patterns.every(({ includes, excludes }) => ( |
|
(!includes || includes.some(m => m.match(relativePath))) && |
|
(!excludes || !excludes.some(m => m.match(relativePath))) |
|
)); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
toJSON() { |
|
if (this.patterns.length === 1) { |
|
return { |
|
...patternToJson(this.patterns[0]), |
|
basePath: this.basePath |
|
}; |
|
} |
|
return { |
|
AND: this.patterns.map(patternToJson), |
|
basePath: this.basePath |
|
}; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
[util.inspect.custom]() { |
|
return this.toJSON(); |
|
} |
|
} |
|
|
|
export { OverrideTester }; |
|
|