Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/metro-file-map/src/Watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type WatcherOptions = {
extensions: $ReadOnlyArray<string>,
forceNodeFilesystemAPI: boolean,
healthCheckFilePrefix: string,
ignoreForCrawl: string => boolean,
ignoreForCrawl: (filePath: string) => boolean,
ignorePatternForWatch: RegExp,
previousState: CrawlerOptions['previousState'],
perfLogger: ?PerfLogger,
Expand Down
2 changes: 1 addition & 1 deletion packages/metro-file-map/src/__tests__/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ describe('FileMap', () => {
});

test('exports constants', () => {
expect(FileMap.H).toBe(require('../constants'));
expect(FileMap.H).toBe(require('../constants').constants);
});

test('ignores files given a pattern', async () => {
Expand Down
1 change: 1 addition & 0 deletions packages/metro-file-map/src/__tests__/worker-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ describe('jest-worker interface', () => {

beforeEach(() => {
jest.resetModules();
// Re-require the worker module to reset its internal state.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what? get rid of you

workerModule = require('../worker');
});

Expand Down
15 changes: 7 additions & 8 deletions packages/metro-file-map/src/cache/DiskCacheManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ export class DiskCacheManager implements CacheManager {
#stopListening: ?() => void;

constructor(
{buildParameters}: CacheManagerFactoryOptions,
{autoSave = {}, cacheDirectory, cacheFilePrefix}: DiskCacheConfig,
factoryOptions: CacheManagerFactoryOptions,
config: DiskCacheConfig,
) {
const {buildParameters} = factoryOptions;
const {autoSave = {}, cacheDirectory, cacheFilePrefix} = config;
this.#cachePath = DiskCacheManager.getCacheFilePath(
buildParameters,
cacheFilePrefix,
Expand Down Expand Up @@ -103,12 +105,9 @@ export class DiskCacheManager implements CacheManager {

async write(
getSnapshot: () => CacheData,
{
changedSinceCacheRead,
eventSource,
onWriteError,
}: CacheManagerWriteOptions,
writeOptions: CacheManagerWriteOptions,
): Promise<void> {
const {changedSinceCacheRead, eventSource, onWriteError} = writeOptions;
// Initialise a writer function using a promise queue to ensure writes are
// sequenced.
const tryWrite = (this.#tryWrite = () => {
Expand Down Expand Up @@ -150,7 +149,7 @@ export class DiskCacheManager implements CacheManager {
}
}

async end() {
async end(): Promise<void> {
// Clear any timers
if (this.#debounceTimeout) {
clearTimeout(this.#debounceTimeout);
Expand Down
7 changes: 2 additions & 5 deletions packages/metro-file-map/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@
* This constant key map allows to keep the map smaller without having to build
* a custom serialization library.
*/

/*::
import type {HType} from './flow-types';
*/

'use strict';

const constants/*: HType */ = {
export const constants: HType = {
/* dependency serialization */
DEPENDENCY_DELIM: '\0',

Expand All @@ -51,4 +48,4 @@ const constants/*: HType */ = {
NATIVE_PLATFORM: 'native',
};

module.exports = constants;
export default constants;
25 changes: 13 additions & 12 deletions packages/metro-file-map/src/crawlers/watchman/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,23 @@ function makeWatchmanError(error: Error): Error {
return error;
}

export default async function watchmanCrawl({
abortSignal,
computeSha1,
extensions,
ignore,
includeSymlinks,
onStatus,
perfLogger,
previousState,
rootDir,
roots,
}: CrawlerOptions): Promise<{
export default async function watchmanCrawl(options: CrawlerOptions): Promise<{
changedFiles: FileData,
removedFiles: Set<CanonicalPath>,
clocks: WatchmanClocks,
}> {
const {
abortSignal,
computeSha1,
extensions,
ignore,
includeSymlinks,
onStatus,
perfLogger,
previousState,
rootDir,
roots,
} = options;
abortSignal?.throwIfAborted();

const client = new watchman.Client();
Expand Down
18 changes: 8 additions & 10 deletions packages/metro-file-map/src/crawlers/watchman/planQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,20 @@ import type {
WatchmanQuerySince,
} from 'fb-watchman';

export function planQuery({
since,
directoryFilters,
extensions,
includeSha1,
includeSymlinks,
}: Readonly<{
type PlanQueryArgs = Readonly<{
since: ?WatchmanQuerySince,
directoryFilters: $ReadOnlyArray<string>,
extensions: $ReadOnlyArray<string>,
directoryFilters: ReadonlyArray<string>,
extensions: ReadonlyArray<string>,
includeSha1: boolean,
includeSymlinks: boolean,
}>): {
}>;

export function planQuery(args: PlanQueryArgs): {
query: WatchmanQuery,
queryGenerator: string,
} {
const {since, directoryFilters, extensions, includeSha1, includeSymlinks} =
args;
const fields = ['name', 'exists', 'mtime_ms', 'size'];
if (includeSha1) {
fields.push('content.sha1hex');
Expand Down
10 changes: 6 additions & 4 deletions packages/metro-file-map/src/flow-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export type CacheManagerFactoryOptions = Readonly<{
export type CacheManagerWriteOptions = Readonly<{
changedSinceCacheRead: boolean,
eventSource: CacheManagerEventSource,
onWriteError: Error => void,
onWriteError: (error: Error) => void,
}>;

// A path that is
Expand Down Expand Up @@ -427,7 +427,9 @@ export type HasteMapData = Map<string, HasteMapItem>;

export type HasteMapItem = {
[platform: string]: HasteMapItemMetadata,
/*::
__proto__: null,
*/
};
export type HasteMapItemMetadata = [/* path */ string, /* type */ number];

Expand Down Expand Up @@ -465,8 +467,8 @@ export type ReadOnlyRawMockMap = Readonly<{

export interface WatcherBackend {
getPauseReason(): ?string;
onError((error: Error) => void): () => void;
onFileEvent((event: WatcherBackendChangeEvent) => void): () => void;
onError(listener: (error: Error) => void): () => void;
onFileEvent(listener: (event: WatcherBackendChangeEvent) => void): () => void;
startWatching(): Promise<void>;
stopWatching(): Promise<void>;
}
Expand Down Expand Up @@ -521,5 +523,5 @@ export type WorkerMetadata = Readonly<{
}>;

export type WorkerSetupArgs = Readonly<{
plugins?: $ReadOnlyArray<FileMapPluginWorker['worker']>,
plugins?: ReadonlyArray<FileMapPluginWorker['worker']>,
}>;
4 changes: 4 additions & 0 deletions packages/metro-file-map/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,19 @@ export default class FileMap extends EventEmitter {
_buildPromise: ?Promise<BuildResult>;
_canUseWatchmanPromise: Promise<boolean>;
_changeID: number;
/*::
_changeInterval: ?IntervalID;
*/
_fileProcessor: FileProcessor;
_console: Console;
_options: InternalOptions;
_pathUtils: RootPathUtils;
_watcher: ?Watcher;
_cacheManager: CacheManager;
_crawlerAbortController: AbortController;
/*::
_healthCheckInterval: ?IntervalID;
*/
_startupPerfLogger: ?PerfLogger;

#plugins: $ReadOnlyArray<IndexedPlugin>;
Expand Down
114 changes: 60 additions & 54 deletions packages/metro-file-map/src/lib/TreeFS.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,49 @@ type NormalizedSymlinkTarget = {
startOfBasenameIdx: number,
};

type DeserializedSnapshotInput = {
rootDir: string,
fileSystemData: DirectoryNode,
processFile: ProcessFileFunction,
};

type TreeFSOptions = {
rootDir: Path,
files?: FileData,
processFile: ProcessFileFunction,
};

type MatchFilesOptions = Readonly<{
/* Filter relative paths against a pattern. */
filter?: ?RegExp,
/* `filter` is applied against absolute paths, vs rootDir-relative. (default: false) */
filterCompareAbsolute?: boolean,
/* `filter` is applied against posix-delimited paths, even on Windows. (default: false) */
filterComparePosix?: boolean,
/* Follow symlinks when enumerating paths. (default: false) */
follow?: boolean,
/* Should search for files recursively. (default: true) */
recursive?: boolean,
/* Match files under a given root, or null for all files */
rootDir?: ?Path,
}>;

type MetadataIteratorOptions = Readonly<{
includeSymlinks: boolean,
includeNodeModules: boolean,
}>;

type PathIteratorOptions = Readonly<{
alwaysYieldPosix: boolean,
canonicalPathOfRoot: string,
follow: boolean,
recursive: boolean,
subtreeOnly: boolean,
}>;

type GetFileDataOptions = {
followLeaf: boolean,
};
/**
* OVERVIEW:
*
Expand Down Expand Up @@ -99,15 +142,8 @@ export default class TreeFS implements MutableFileSystem {
#pathUtils: RootPathUtils;
#processFile: ProcessFileFunction;

constructor({
rootDir,
files,
processFile,
}: {
rootDir: Path,
files?: FileData,
processFile: ProcessFileFunction,
}) {
constructor(opts: TreeFSOptions) {
const {rootDir, files, processFile} = opts;
this.#rootDir = rootDir;
this.#pathUtils = new RootPathUtils(rootDir);
this.#processFile = processFile;
Expand All @@ -120,15 +156,8 @@ export default class TreeFS implements MutableFileSystem {
return this._cloneTree(this.#rootNode);
}

static fromDeserializedSnapshot({
rootDir,
fileSystemData,
processFile,
}: {
rootDir: string,
fileSystemData: DirectoryNode,
processFile: ProcessFileFunction,
}): TreeFS {
static fromDeserializedSnapshot(args: DeserializedSnapshotInput): TreeFS {
const {rootDir, fileSystemData, processFile} = args;
const tfs = new TreeFS({processFile, rootDir});
tfs.#rootNode = fileSystemData;
return tfs;
Expand Down Expand Up @@ -302,27 +331,15 @@ export default class TreeFS implements MutableFileSystem {
* The query matches against normalized paths which start with `./`,
* for example: `a/b.js` -> `./a/b.js`
*/
*matchFiles({
filter = null,
filterCompareAbsolute = false,
filterComparePosix = false,
follow = false,
recursive = true,
rootDir = null,
}: Readonly<{
/* Filter relative paths against a pattern. */
filter?: ?RegExp,
/* `filter` is applied against absolute paths, vs rootDir-relative. (default: false) */
filterCompareAbsolute?: boolean,
/* `filter` is applied against posix-delimited paths, even on Windows. (default: false) */
filterComparePosix?: boolean,
/* Follow symlinks when enumerating paths. (default: false) */
follow?: boolean,
/* Should search for files recursively. (default: true) */
recursive?: boolean,
/* Match files under a given root, or null for all files */
rootDir?: ?Path,
}>): Iterable<Path> {
*matchFiles(opts: MatchFilesOptions): Iterable<Path> {
const {
filter = null,
filterCompareAbsolute = false,
filterComparePosix = false,
follow = false,
recursive = true,
rootDir = null,
} = opts;
const normalRoot = rootDir == null ? '' : this._normalizePath(rootDir);
const contextRootResult = this._lookupByNormalPath(normalRoot);
if (!contextRootResult.exists) {
Expand Down Expand Up @@ -993,12 +1010,7 @@ export default class TreeFS implements MutableFileSystem {
return null;
}

*metadataIterator(
opts: Readonly<{
includeSymlinks: boolean,
includeNodeModules: boolean,
}>,
): Iterator<{
*metadataIterator(opts: MetadataIteratorOptions): Iterator<{
baseName: string,
canonicalPath: string,
metadata: FileMetadata,
Expand All @@ -1008,7 +1020,7 @@ export default class TreeFS implements MutableFileSystem {

*_metadataIterator(
rootNode: DirectoryNode,
opts: Readonly<{includeSymlinks: boolean, includeNodeModules: boolean}>,
opts: MetadataIteratorOptions,
prefix: string = '',
): Iterable<{
baseName: string,
Expand Down Expand Up @@ -1060,13 +1072,7 @@ export default class TreeFS implements MutableFileSystem {
iterationRootNode: DirectoryNode,
iterationRootParentNode: ?DirectoryNode,
ancestorOfRootIdx: ?number,
opts: Readonly<{
alwaysYieldPosix: boolean,
canonicalPathOfRoot: string,
follow: boolean,
recursive: boolean,
subtreeOnly: boolean,
}>,
opts: PathIteratorOptions,
pathPrefix: string = '',
followedLinks: ReadonlySet<FileMetadata> = new Set(),
): Iterable<Path> {
Expand Down Expand Up @@ -1185,7 +1191,7 @@ export default class TreeFS implements MutableFileSystem {

_getFileData(
filePath: Path,
opts: {followLeaf: boolean} = {followLeaf: true},
opts: GetFileDataOptions = {followLeaf: true},
): ?FileMetadata {
const normalPath = this._normalizePath(filePath);
const result = this._lookupByNormalPath(normalPath, {
Expand Down
Loading
Loading