metro-file-map 0.84.2 → 0.84.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/package.json +1 -1
  2. package/src/Watcher.d.ts +6 -9
  3. package/src/Watcher.js +66 -39
  4. package/src/Watcher.js.flow +84 -51
  5. package/src/crawlers/node/index.d.ts +3 -5
  6. package/src/crawlers/node/index.js +4 -1
  7. package/src/crawlers/node/index.js.flow +8 -6
  8. package/src/crawlers/watchman/index.d.ts +5 -12
  9. package/src/crawlers/watchman/index.js.flow +2 -6
  10. package/src/flow-types.d.ts +81 -32
  11. package/src/flow-types.js.flow +89 -29
  12. package/src/index.d.ts +4 -4
  13. package/src/index.js +145 -120
  14. package/src/index.js.flow +199 -149
  15. package/src/lib/FileSystemChangeAggregator.d.ts +40 -0
  16. package/src/lib/FileSystemChangeAggregator.js +89 -0
  17. package/src/lib/FileSystemChangeAggregator.js.flow +143 -0
  18. package/src/lib/TreeFS.d.ts +16 -8
  19. package/src/lib/TreeFS.js +67 -16
  20. package/src/lib/TreeFS.js.flow +89 -16
  21. package/src/plugins/DependencyPlugin.d.ts +5 -36
  22. package/src/plugins/DependencyPlugin.js +26 -48
  23. package/src/plugins/DependencyPlugin.js.flow +22 -100
  24. package/src/plugins/FileDataPlugin.d.ts +55 -0
  25. package/src/plugins/FileDataPlugin.js +41 -0
  26. package/src/plugins/FileDataPlugin.js.flow +76 -0
  27. package/src/plugins/HastePlugin.d.ts +3 -11
  28. package/src/plugins/HastePlugin.js +11 -11
  29. package/src/plugins/HastePlugin.js.flow +12 -12
  30. package/src/plugins/MockPlugin.d.ts +3 -5
  31. package/src/plugins/MockPlugin.js +17 -20
  32. package/src/plugins/MockPlugin.js.flow +18 -22
  33. package/src/watchers/FallbackWatcher.js +19 -3
  34. package/src/watchers/FallbackWatcher.js.flow +28 -5
  35. package/src/watchers/NativeWatcher.d.ts +2 -2
  36. package/src/watchers/NativeWatcher.js +27 -5
  37. package/src/watchers/NativeWatcher.js.flow +33 -6
  38. package/src/watchers/common.d.ts +3 -1
  39. package/src/watchers/common.js +6 -1
  40. package/src/watchers/common.js.flow +1 -0
@@ -6,7 +6,7 @@
6
6
  *
7
7
  * @noformat
8
8
  * @oncall react_native
9
- * @generated SignedSource<<81805d051693b746e75928fe6ed3dbca>>
9
+ * @generated SignedSource<<d9402d4670982b1e675e1edd9201cf75>>
10
10
  *
11
11
  * This file was translated from Flow by scripts/generateTypeScriptDefinitions.js
12
12
  * Original file: packages/metro-file-map/src/plugins/MockPlugin.js
@@ -16,13 +16,13 @@
16
16
  */
17
17
 
18
18
  import type {
19
- FileMapDelta,
20
19
  FileMapPlugin,
21
20
  FileMapPluginInitOptions,
22
21
  FileMapPluginWorker,
23
22
  MockMap as IMockMap,
24
23
  Path,
25
24
  RawMockMap,
25
+ ReadonlyFileSystemChanges,
26
26
  } from '../flow-types';
27
27
 
28
28
  export declare const CACHE_VERSION: 2;
@@ -39,9 +39,7 @@ declare class MockPlugin implements FileMapPlugin<RawMockMap, void>, IMockMap {
39
39
  constructor($$PARAM_0$$: MockMapOptions);
40
40
  initialize($$PARAM_0$$: FileMapPluginInitOptions<RawMockMap>): Promise<void>;
41
41
  getMockModule(name: string): null | undefined | Path;
42
- bulkUpdate(delta: FileMapDelta): void;
43
- onNewOrModifiedFile(relativeFilePath: Path): void;
44
- onRemovedFile(relativeFilePath: Path): void;
42
+ onChanged(delta: ReadonlyFileSystemChanges<null | undefined | void>): void;
45
43
  getSerializableSnapshot(): RawMockMap;
46
44
  assertValid(): void;
47
45
  getCacheKey(): string;
@@ -51,15 +51,12 @@ class MockPlugin {
51
51
  if (pluginState != null && pluginState.version === this.#raw.version) {
52
52
  this.#raw = pluginState;
53
53
  } else {
54
- this.bulkUpdate({
55
- addedOrModified: [
56
- ...files.fileIterator({
57
- includeNodeModules: false,
58
- includeSymlinks: false,
59
- }),
60
- ].map(({ canonicalPath }) => [canonicalPath, null]),
61
- removed: [],
62
- });
54
+ for (const { canonicalPath } of files.fileIterator({
55
+ includeNodeModules: false,
56
+ includeSymlinks: false,
57
+ })) {
58
+ this.#onFileAdded(canonicalPath);
59
+ }
63
60
  }
64
61
  }
65
62
  getMockModule(name) {
@@ -72,22 +69,22 @@ class MockPlugin {
72
69
  (0, _normalizePathSeparatorsToSystem.default)(mockPosixRelativePath),
73
70
  );
74
71
  }
75
- bulkUpdate(delta) {
76
- for (const [relativeFilePath] of delta.removed) {
77
- this.onRemovedFile(relativeFilePath);
72
+ onChanged(delta) {
73
+ for (const [canonicalPath] of delta.removedFiles) {
74
+ this.#onFileRemoved(canonicalPath);
78
75
  }
79
- for (const [relativeFilePath] of delta.addedOrModified) {
80
- this.onNewOrModifiedFile(relativeFilePath);
76
+ for (const [canonicalPath] of delta.addedFiles) {
77
+ this.#onFileAdded(canonicalPath);
81
78
  }
82
79
  }
83
- onNewOrModifiedFile(relativeFilePath) {
84
- const absoluteFilePath = this.#pathUtils.normalToAbsolute(relativeFilePath);
80
+ #onFileAdded(canonicalPath) {
81
+ const absoluteFilePath = this.#pathUtils.normalToAbsolute(canonicalPath);
85
82
  if (!this.#mocksPattern.test(absoluteFilePath)) {
86
83
  return;
87
84
  }
88
85
  const mockName = (0, _getMockName.default)(absoluteFilePath);
89
86
  const posixRelativePath = (0, _normalizePathSeparatorsToPosix.default)(
90
- relativeFilePath,
87
+ canonicalPath,
91
88
  );
92
89
  const existingMockPosixPath = this.#raw.mocks.get(mockName);
93
90
  if (existingMockPosixPath != null) {
@@ -104,8 +101,8 @@ class MockPlugin {
104
101
  }
105
102
  this.#raw.mocks.set(mockName, posixRelativePath);
106
103
  }
107
- onRemovedFile(relativeFilePath) {
108
- const absoluteFilePath = this.#pathUtils.normalToAbsolute(relativeFilePath);
104
+ #onFileRemoved(canonicalPath) {
105
+ const absoluteFilePath = this.#pathUtils.normalToAbsolute(canonicalPath);
109
106
  if (!this.#mocksPattern.test(absoluteFilePath)) {
110
107
  return;
111
108
  }
@@ -113,7 +110,7 @@ class MockPlugin {
113
110
  const duplicates = this.#raw.duplicates.get(mockName);
114
111
  if (duplicates != null) {
115
112
  const posixRelativePath = (0, _normalizePathSeparatorsToPosix.default)(
116
- relativeFilePath,
113
+ canonicalPath,
117
114
  );
118
115
  duplicates.delete(posixRelativePath);
119
116
  if (duplicates.size === 1) {
@@ -10,13 +10,13 @@
10
10
  */
11
11
 
12
12
  import type {
13
- FileMapDelta,
14
13
  FileMapPlugin,
15
14
  FileMapPluginInitOptions,
16
15
  FileMapPluginWorker,
17
16
  MockMap as IMockMap,
18
17
  Path,
19
18
  RawMockMap,
19
+ ReadonlyFileSystemChanges,
20
20
  } from '../flow-types';
21
21
 
22
22
  import normalizePathSeparatorsToPosix from '../lib/normalizePathSeparatorsToPosix';
@@ -79,15 +79,12 @@ export default class MockPlugin
79
79
  this.#raw = pluginState;
80
80
  } else {
81
81
  // Otherwise, traverse all files to rebuild
82
- this.bulkUpdate({
83
- addedOrModified: [
84
- ...files.fileIterator({
85
- includeNodeModules: false,
86
- includeSymlinks: false,
87
- }),
88
- ].map(({canonicalPath}) => [canonicalPath, null]),
89
- removed: [],
90
- });
82
+ for (const {canonicalPath} of files.fileIterator({
83
+ includeNodeModules: false,
84
+ includeSymlinks: false,
85
+ })) {
86
+ this.#onFileAdded(canonicalPath);
87
+ }
91
88
  }
92
89
  }
93
90
 
@@ -102,24 +99,24 @@ export default class MockPlugin
102
99
  );
103
100
  }
104
101
 
105
- bulkUpdate(delta: FileMapDelta<>): void {
102
+ onChanged(delta: ReadonlyFileSystemChanges<?void>): void {
106
103
  // Process removals first so that moves aren't treated as duplicates.
107
- for (const [relativeFilePath] of delta.removed) {
108
- this.onRemovedFile(relativeFilePath);
104
+ for (const [canonicalPath] of delta.removedFiles) {
105
+ this.#onFileRemoved(canonicalPath);
109
106
  }
110
- for (const [relativeFilePath] of delta.addedOrModified) {
111
- this.onNewOrModifiedFile(relativeFilePath);
107
+ for (const [canonicalPath] of delta.addedFiles) {
108
+ this.#onFileAdded(canonicalPath);
112
109
  }
113
110
  }
114
111
 
115
- onNewOrModifiedFile(relativeFilePath: Path): void {
116
- const absoluteFilePath = this.#pathUtils.normalToAbsolute(relativeFilePath);
112
+ #onFileAdded(canonicalPath: Path): void {
113
+ const absoluteFilePath = this.#pathUtils.normalToAbsolute(canonicalPath);
117
114
  if (!this.#mocksPattern.test(absoluteFilePath)) {
118
115
  return;
119
116
  }
120
117
 
121
118
  const mockName = getMockName(absoluteFilePath);
122
- const posixRelativePath = normalizePathSeparatorsToPosix(relativeFilePath);
119
+ const posixRelativePath = normalizePathSeparatorsToPosix(canonicalPath);
123
120
 
124
121
  const existingMockPosixPath = this.#raw.mocks.get(mockName);
125
122
  if (existingMockPosixPath != null) {
@@ -141,16 +138,15 @@ export default class MockPlugin
141
138
  this.#raw.mocks.set(mockName, posixRelativePath);
142
139
  }
143
140
 
144
- onRemovedFile(relativeFilePath: Path): void {
145
- const absoluteFilePath = this.#pathUtils.normalToAbsolute(relativeFilePath);
141
+ #onFileRemoved(canonicalPath: Path): void {
142
+ const absoluteFilePath = this.#pathUtils.normalToAbsolute(canonicalPath);
146
143
  if (!this.#mocksPattern.test(absoluteFilePath)) {
147
144
  return;
148
145
  }
149
146
  const mockName = getMockName(absoluteFilePath);
150
147
  const duplicates = this.#raw.duplicates.get(mockName);
151
148
  if (duplicates != null) {
152
- const posixRelativePath =
153
- normalizePathSeparatorsToPosix(relativeFilePath);
149
+ const posixRelativePath = normalizePathSeparatorsToPosix(canonicalPath);
154
150
  duplicates.delete(posixRelativePath);
155
151
  if (duplicates.size === 1) {
156
152
  this.#raw.duplicates.delete(mockName);
@@ -99,9 +99,19 @@ class FallbackWatcher extends _AbstractWatcher.AbstractWatcher {
99
99
  }
100
100
  }
101
101
  #unregisterDir(dirpath) {
102
- if (this.#dirRegistry[dirpath]) {
103
- delete this.#dirRegistry[dirpath];
102
+ const removedFiles = [];
103
+ for (const registeredDir of Object.keys(this.#dirRegistry)) {
104
+ if (
105
+ registeredDir === dirpath ||
106
+ registeredDir.startsWith(dirpath + _path.default.sep)
107
+ ) {
108
+ for (const filename of Object.keys(this.#dirRegistry[registeredDir])) {
109
+ removedFiles.push(_path.default.join(registeredDir, filename));
110
+ }
111
+ delete this.#dirRegistry[registeredDir];
112
+ }
104
113
  }
114
+ return removedFiles;
105
115
  }
106
116
  #registered(fullpath) {
107
117
  const dir = _path.default.dirname(fullpath);
@@ -294,7 +304,13 @@ class FallbackWatcher extends _AbstractWatcher.AbstractWatcher {
294
304
  return;
295
305
  }
296
306
  this.#unregister(fullPath);
297
- this.#unregisterDir(fullPath);
307
+ const removedFiles = this.#unregisterDir(fullPath);
308
+ for (const removedFile of removedFiles) {
309
+ this.#emitEvent({
310
+ event: DELETE_EVENT,
311
+ relativePath: _path.default.relative(this.root, removedFile),
312
+ });
313
+ }
298
314
  if (registered) {
299
315
  this.#emitEvent({
300
316
  event: DELETE_EVENT,
@@ -124,12 +124,27 @@ export default class FallbackWatcher extends AbstractWatcher {
124
124
  }
125
125
 
126
126
  /**
127
- * Removes a dir from the registry.
127
+ * Removes a dir from the registry, returning all files that were registered
128
+ * under it (recursively).
128
129
  */
129
- #unregisterDir(dirpath: string): void {
130
- if (this.#dirRegistry[dirpath]) {
131
- delete this.#dirRegistry[dirpath];
130
+ #unregisterDir(dirpath: string): Array<string> {
131
+ const removedFiles: Array<string> = [];
132
+
133
+ // Find and remove all entries under this directory
134
+ for (const registeredDir of Object.keys(this.#dirRegistry)) {
135
+ if (
136
+ registeredDir === dirpath ||
137
+ registeredDir.startsWith(dirpath + path.sep)
138
+ ) {
139
+ // Collect all files in this directory
140
+ for (const filename of Object.keys(this.#dirRegistry[registeredDir])) {
141
+ removedFiles.push(path.join(registeredDir, filename));
142
+ }
143
+ delete this.#dirRegistry[registeredDir];
144
+ }
132
145
  }
146
+
147
+ return removedFiles;
133
148
  }
134
149
 
135
150
  /**
@@ -350,7 +365,15 @@ export default class FallbackWatcher extends AbstractWatcher {
350
365
  return;
351
366
  }
352
367
  this.#unregister(fullPath);
353
- this.#unregisterDir(fullPath);
368
+ // When a directory is deleted, emit delete events for all files we
369
+ // knew about under that directory
370
+ const removedFiles = this.#unregisterDir(fullPath);
371
+ for (const removedFile of removedFiles) {
372
+ this.#emitEvent({
373
+ event: DELETE_EVENT,
374
+ relativePath: path.relative(this.root, removedFile),
375
+ });
376
+ }
354
377
  if (registered) {
355
378
  this.#emitEvent({event: DELETE_EVENT, relativePath});
356
379
  }
@@ -5,7 +5,7 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  *
7
7
  * @noformat
8
- * @generated SignedSource<<8239479487abff6df70ee54f9e05f352>>
8
+ * @generated SignedSource<<b68c5620efd3f5bec83279059d0d1b4e>>
9
9
  *
10
10
  * This file was translated from Flow by scripts/generateTypeScriptDefinitions.js
11
11
  * Original file: packages/metro-file-map/src/watchers/NativeWatcher.js
@@ -50,6 +50,6 @@ declare class NativeWatcher extends AbstractWatcher {
50
50
  * End watching.
51
51
  */
52
52
  stopWatching(): Promise<void>;
53
- _handleEvent(relativePath: string): void;
53
+ _handleEvent(event: string, relativePath: string): void;
54
54
  }
55
55
  export default NativeWatcher;
@@ -39,6 +39,7 @@ function _interopRequireWildcard(e, t) {
39
39
  const debug = require("debug")("Metro:NativeWatcher");
40
40
  const TOUCH_EVENT = "touch";
41
41
  const DELETE_EVENT = "delete";
42
+ const RECRAWL_EVENT = "recrawl";
42
43
  class NativeWatcher extends _AbstractWatcher.AbstractWatcher {
43
44
  #fsWatcher;
44
45
  static isSupported() {
@@ -57,8 +58,8 @@ class NativeWatcher extends _AbstractWatcher.AbstractWatcher {
57
58
  persistent: false,
58
59
  recursive: true,
59
60
  },
60
- (_event, relativePath) => {
61
- this._handleEvent(relativePath).catch((error) => {
61
+ (event, relativePath) => {
62
+ this._handleEvent(event, relativePath).catch((error) => {
62
63
  this.emitError(error);
63
64
  });
64
65
  },
@@ -71,13 +72,23 @@ class NativeWatcher extends _AbstractWatcher.AbstractWatcher {
71
72
  this.#fsWatcher.close();
72
73
  }
73
74
  }
74
- async _handleEvent(relativePath) {
75
+ async _handleEvent(event, relativePath) {
75
76
  const absolutePath = path.resolve(this.root, relativePath);
76
77
  if (this.doIgnore(relativePath)) {
77
- debug("Ignoring event on %s (root: %s)", relativePath, this.root);
78
+ debug(
79
+ 'Ignoring event "%s" on %s (root: %s)',
80
+ event,
81
+ relativePath,
82
+ this.root,
83
+ );
78
84
  return;
79
85
  }
80
- debug("Handling event on %s (root: %s)", relativePath, this.root);
86
+ debug(
87
+ 'Handling event "%s" on %s (root: %s)',
88
+ event,
89
+ relativePath,
90
+ this.root,
91
+ );
81
92
  try {
82
93
  const stat = await _fs.promises.lstat(absolutePath);
83
94
  const type = (0, _common.typeFromStat)(stat);
@@ -89,6 +100,17 @@ class NativeWatcher extends _AbstractWatcher.AbstractWatcher {
89
100
  ) {
90
101
  return;
91
102
  }
103
+ if (type === "d" && event === "rename") {
104
+ debug(
105
+ "Directory rename detected on %s, requesting recrawl",
106
+ relativePath,
107
+ );
108
+ this.emitFileEvent({
109
+ event: RECRAWL_EVENT,
110
+ relativePath,
111
+ });
112
+ return;
113
+ }
92
114
  this.emitFileEvent({
93
115
  event: TOUCH_EVENT,
94
116
  relativePath,
@@ -21,6 +21,7 @@ const debug = require('debug')('Metro:NativeWatcher');
21
21
 
22
22
  const TOUCH_EVENT = 'touch';
23
23
  const DELETE_EVENT = 'delete';
24
+ const RECRAWL_EVENT = 'recrawl';
24
25
 
25
26
  /**
26
27
  * NativeWatcher uses Node's native fs.watch API with recursive: true.
@@ -74,9 +75,8 @@ export default class NativeWatcher extends AbstractWatcher {
74
75
  // ~instant on macOS or Windows.
75
76
  recursive: true,
76
77
  },
77
- (_event, relativePath) => {
78
- // _event is always 'rename' on macOS, so we don't use it.
79
- this._handleEvent(relativePath).catch(error => {
78
+ (event, relativePath) => {
79
+ this._handleEvent(event, relativePath).catch(error => {
80
80
  this.emitError(error);
81
81
  });
82
82
  },
@@ -95,13 +95,23 @@ export default class NativeWatcher extends AbstractWatcher {
95
95
  }
96
96
  }
97
97
 
98
- async _handleEvent(relativePath: string) {
98
+ async _handleEvent(event: string, relativePath: string) {
99
99
  const absolutePath = path.resolve(this.root, relativePath);
100
100
  if (this.doIgnore(relativePath)) {
101
- debug('Ignoring event on %s (root: %s)', relativePath, this.root);
101
+ debug(
102
+ 'Ignoring event "%s" on %s (root: %s)',
103
+ event,
104
+ relativePath,
105
+ this.root,
106
+ );
102
107
  return;
103
108
  }
104
- debug('Handling event on %s (root: %s)', relativePath, this.root);
109
+ debug(
110
+ 'Handling event "%s" on %s (root: %s)',
111
+ event,
112
+ relativePath,
113
+ this.root,
114
+ );
105
115
 
106
116
  try {
107
117
  const stat = await fsPromises.lstat(absolutePath);
@@ -116,6 +126,23 @@ export default class NativeWatcher extends AbstractWatcher {
116
126
  return;
117
127
  }
118
128
 
129
+ // For directory "rename" events, notify that we need a recrawl since we
130
+ // wont' receive events for unmodified files underneath a moved (or
131
+ // cloned) directory. Renames are fired by the OS on moves, clones, and
132
+ // creations. We ignore "change" events because they indiciate a change
133
+ // to directory metadata, rather than its path or existence.
134
+ if (type === 'd' && event === 'rename') {
135
+ debug(
136
+ 'Directory rename detected on %s, requesting recrawl',
137
+ relativePath,
138
+ );
139
+ this.emitFileEvent({
140
+ event: RECRAWL_EVENT,
141
+ relativePath,
142
+ });
143
+ return;
144
+ }
145
+
119
146
  this.emitFileEvent({
120
147
  event: TOUCH_EVENT,
121
148
  relativePath,
@@ -6,7 +6,7 @@
6
6
  *
7
7
  * @noformat
8
8
  * @oncall react_native
9
- * @generated SignedSource<<4f8f87abcb055b965a90e994a55ed177>>
9
+ * @generated SignedSource<<ebebfbca9d43e034fde8489e1d9f2dbb>>
10
10
  *
11
11
  * This file was translated from Flow by scripts/generateTypeScriptDefinitions.js
12
12
  * Original file: packages/metro-file-map/src/watchers/common.js
@@ -29,6 +29,8 @@ export declare const DELETE_EVENT: 'delete';
29
29
  export declare type DELETE_EVENT = typeof DELETE_EVENT;
30
30
  export declare const TOUCH_EVENT: 'touch';
31
31
  export declare type TOUCH_EVENT = typeof TOUCH_EVENT;
32
+ export declare const RECRAWL_EVENT: 'recrawl';
33
+ export declare type RECRAWL_EVENT = typeof RECRAWL_EVENT;
32
34
  export declare const ALL_EVENT: 'all';
33
35
  export declare type ALL_EVENT = typeof ALL_EVENT;
34
36
  export type WatcherOptions = Readonly<{
@@ -3,7 +3,11 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true,
5
5
  });
6
- exports.TOUCH_EVENT = exports.DELETE_EVENT = exports.ALL_EVENT = void 0;
6
+ exports.TOUCH_EVENT =
7
+ exports.RECRAWL_EVENT =
8
+ exports.DELETE_EVENT =
9
+ exports.ALL_EVENT =
10
+ void 0;
7
11
  exports.includedByGlob = includedByGlob;
8
12
  exports.posixPathMatchesPattern = void 0;
9
13
  exports.typeFromStat = typeFromStat;
@@ -14,6 +18,7 @@ function _interopRequireDefault(e) {
14
18
  }
15
19
  const DELETE_EVENT = (exports.DELETE_EVENT = "delete");
16
20
  const TOUCH_EVENT = (exports.TOUCH_EVENT = "touch");
21
+ const RECRAWL_EVENT = (exports.RECRAWL_EVENT = "recrawl");
17
22
  const ALL_EVENT = (exports.ALL_EVENT = "all");
18
23
  function includedByGlob(type, globs, dot, relativePath) {
19
24
  if (globs.length === 0 || type !== "f") {
@@ -26,6 +26,7 @@ import path from 'path';
26
26
  */
27
27
  export const DELETE_EVENT = 'delete';
28
28
  export const TOUCH_EVENT = 'touch';
29
+ export const RECRAWL_EVENT = 'recrawl';
29
30
  export const ALL_EVENT = 'all';
30
31
 
31
32
  export type WatcherOptions = Readonly<{