Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('bootstraps micromatch', () => {
// This is a workaround for https://github.com/tschaub/mock-fs/issues/213
// Because micromatch() loads packages dynamically which doesn't work if the filesystem is being mocked
// So we need to pre-load them now
require('micromatch').match('file.txt', '**/**')
})
export function matchesFilter(filter: IgnoreFilter, basename: string, loc: string): boolean {
let filterByBasename = true;
if (filter.base && filter.base !== '.') {
loc = path.relative(filter.base, loc);
filterByBasename = false;
}
// the micromatch regex expects unix path separators
loc = loc.replace('\\', '/');
return (
filter.regex.test(loc) ||
filter.regex.test(`/${loc}`) ||
(filterByBasename && filter.regex.test(basename)) ||
mm.isMatch(loc, filter.pattern)
);
}
if (res.some(re => re.test(context.filename))) return true;
if (fns.some(fn => fn(filename))) return true;
if (strings.length > 0) {
const possibleDirs = getPossibleDirs(context);
const absolutePatterns = strings.map(pattern => {
// Preserve the "!" prefix so that micromatch can use it for negation.
const negate = pattern[0] === "!";
if (negate) pattern = pattern.slice(1);
return (negate ? "!" : "") + path.resolve(dirname, pattern);
});
if (
micromatch(possibleDirs, absolutePatterns, { nocase: true }).length > 0
) {
return true;
}
}
return false;
}
lookupAlias(aliases, filename, dir) {
// First, try looking up the exact filename
let alias = aliases[filename];
if (alias == null) {
// Otherwise, try replacing glob keys
for (let key in aliases) {
let val = aliases[key];
if (typeof val === 'string' && isGlob(key)) {
let re = micromatch.makeRe(key, {capture: true});
if (re.test(filename)) {
alias = filename.replace(re, val);
break;
}
}
}
}
if (typeof alias === 'string') {
return this.resolveFilename(alias, dir);
}
return typeof alias === 'string' ? alias : null;
}
lookupAlias(aliases, filename, dir) {
// First, try looking up the exact filename
let alias = aliases[filename];
if (alias == null) {
// Otherwise, try replacing glob keys
for (let key in aliases) {
let val = aliases[key];
if (typeof val === 'string' && isGlob(key)) {
let re = micromatch.makeRe(key, {capture: true});
if (re.test(filename)) {
alias = filename.replace(re, val);
break;
}
}
}
}
if (typeof alias === 'string') {
return this.resolveFilename(alias, dir);
} else if (alias === false) {
return false;
}
return null;
}
// Only acknowledge languages with icons
if(!languageIcon)
return null;
// Lazily require the micromatch dependency due to its weight.
const Micromatch = require("micromatch");
pattern = path.dirname(filePath) + "/" + (/^\//.test(pattern) ? "" : "**") + "/" + pattern;
pattern = path.resolve(pattern);
// Normalise path separators on Windows
if("win32" === process.platform)
pattern = pattern.replace(/\\/g, "/");
pattern = Micromatch.makeRe(pattern, {
unixify: false,
nonegate: true,
dot: true,
});
return pattern
? [pattern, languageIcon]
: null;
})
.filter(a => a);
const filter = (filePath) => {
// The filePath is absolute, but should be relative as micromatch will
// match against the relative path of the routes. Matching against
// an absolute path requires the use of path.join which causes issues on Windows:
// https://github.com/micromatch/micromatch/issues/95
filePath = path.relative(srcPath, filePath)
const fileName = path.parse(filePath).base
const isIgnored = mm.any(filePath, ignoredFiles)
const isJunk = junk.is(fileName)
// Copy file when it's not ignored or not junk
const copy = isIgnored === false && isJunk === false
if (opts.verbose === true) {
if (copy === false) log(`{cyan:Skipping file: {grey:${ filePath }`)
if (copy === true) log(`{cyan:Copying file: {grey:${ filePath }`)
}
// Return true to include, false to exclude
return copy
}
test: (what: string) => {
// this refactor is a tad overly verbose but makes for easy debugging
const pattern = getMatcherString(id, resolutionBase);
const fn = mm.matcher(pattern, { dot: true });
const result = fn(what);
return result;
}
};
exports.create = function (globs) {
if (!Array.isArray(globs)) globs = [globs]
var positives = []
var negatives = []
var accumulator = []
for (var i = 0; i < globs.length; i++) {
var glob = globs[i]
if (typeof glob === "string" && glob[0] === "!") {
// Create Minimatch instances for negative glob patterns
accumulator.push(micromatch.matcher(resolveGlob(glob)))
} else if (glob instanceof RegExp) {
accumulator.push(glob)
} else {
positives.push(glob)
negatives.push(accumulator.slice())
}
}
if (positives.length === 0) {
throw new Error("Missing positive glob")
}
var opts = {
nodir: true,
symlinks: {},
statCache: {},
matchSingle(fileName, pattern) {
if (!pattern) {
return false;
}
// Cache the micropatch "pattern parsing"
let isMatch = this.cache[pattern];
if (!isMatch) {
isMatch = micromatch.matcher(pattern);
this.cache[pattern] = isMatch;
}
return isMatch(fileName);
}