Skip to content

Commit

Permalink
Watcher backends: simplify ignore functions, remove anymatch dependen…
Browse files Browse the repository at this point in the history
…cy (#1407)

Summary: Pull Request resolved: #1407

Differential Revision: D67259981
  • Loading branch information
robhogan authored and facebook-github-bot committed Dec 18, 2024
1 parent a4cb0b0 commit 7e72bd8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
1 change: 0 additions & 1 deletion packages/metro-file-map/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
},
"license": "MIT",
"dependencies": {
"anymatch": "^3.0.3",
"debug": "^2.2.0",
"fb-watchman": "^2.0.0",
"flow-enums-runtime": "^0.0.6",
Expand Down
25 changes: 17 additions & 8 deletions packages/metro-file-map/src/watchers/FSEventsWatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ import type {ChangeEventMetadata} from '../flow-types';
// $FlowFixMe[untyped-type-import]
import type {FSEvents} from 'fsevents';

import {isIncluded, recReaddir, typeFromStat} from './common';
// $FlowFixMe[untyped-import] - anymatch
import anymatch from 'anymatch';
import {
isIncluded,
posixPathMatchesPattern,
recReaddir,
typeFromStat,
} from './common';
import EventEmitter from 'events';
import {promises as fsPromises} from 'fs';
import * as path from 'path';
Expand Down Expand Up @@ -63,7 +66,11 @@ export default class FSEventsWatcher extends EventEmitter {

constructor(
dir: string,
opts: $ReadOnly<{
{
ignored,
glob,
dot,
}: $ReadOnly<{
ignored: ?RegExp,
glob: string | $ReadOnlyArray<string>,
dot: boolean,
Expand All @@ -78,10 +85,12 @@ export default class FSEventsWatcher extends EventEmitter {

super();

this.dot = opts.dot || false;
this.ignored = opts.ignored;
this.glob = Array.isArray(opts.glob) ? opts.glob : [opts.glob];
this.doIgnore = opts.ignored ? anymatch(opts.ignored) : () => false;
this.dot = dot || false;
this.ignored = ignored;
this.glob = Array.isArray(glob) ? glob : [glob];
this.doIgnore = ignored
? (filePath: string) => posixPathMatchesPattern(ignored, filePath)
: () => false;

this.root = path.resolve(dir);

Expand Down
37 changes: 31 additions & 6 deletions packages/metro-file-map/src/watchers/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import type {ChangeEventMetadata} from '../flow-types';
import type {Stats} from 'fs';

// $FlowFixMe[untyped-import] - Write libdefs for `anymatch`
const anymatch = require('anymatch');
// $FlowFixMe[untyped-import] - Write libdefs for `micromatch`
const micromatch = require('micromatch');
const platform = require('os').platform();
Expand Down Expand Up @@ -74,8 +72,10 @@ export const assignOptions = function (
if (!Array.isArray(watcher.globs)) {
watcher.globs = [watcher.globs];
}
watcher.doIgnore =
opts.ignored != null ? anymatch(opts.ignored) : () => false;
const ignored = watcher.ignored;
watcher.doIgnore = ignored
? filePath => ignored.test(toPosixSeparators(filePath))
: () => false;

if (opts.watchman == true && opts.watchmanPath != null) {
watcher.watchmanPath = opts.watchmanPath;
Expand Down Expand Up @@ -105,6 +105,26 @@ export function isIncluded(
return micromatch.some(relativePath, globs, {dot});
}

const toPosixSeparators: (filePath: string) => string =
path.sep === '/'
? filePath => filePath
: filePath => filePath.replaceAll(path.sep, '/');

/**
* Whether the given filePath matches the given RegExp, after converting
* (on Windows only) system separators to posix separators.
*
* Conversion to posix is for backwards compatibility with the previous
* anymatch matcher, which normlises all inputs[1]. This may not be consistent
* with other parts of metro-file-map.
*
* [1]: https://github.com/micromatch/anymatch/blob/3.1.1/index.js#L50
*/
export const posixPathMatchesPattern = (
pattern: RegExp,
filePath: string,
): boolean => pattern.test(toPosixSeparators(filePath));

/**
* Traverse a directory recursively calling `callback` on every directory.
*/
Expand All @@ -117,8 +137,13 @@ export function recReaddir(
errorCallback: Error => void,
ignored: ?RegExp,
) {
walker(dir)
.filterDir(currentDir => !anymatch(ignored, currentDir))
const walk = walker(dir);
if (ignored) {
walk.filterDir(
(currentDir: string) => !posixPathMatchesPattern(ignored, currentDir),
);
}
walk
.on('dir', normalizeProxy(dirCallback))
.on('file', normalizeProxy(fileCallback))
.on('symlink', normalizeProxy(symlinkCallback))
Expand Down

0 comments on commit 7e72bd8

Please sign in to comment.