File system crawling, watching and mapping library designed for Metro bundler ecosystem
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Main FileMap class that orchestrates file system crawling, watching, and caching operations. This is the primary entry point for all metro-file-map functionality.
The main class that extends EventEmitter to provide file system mapping capabilities.
/**
* Main FileMap class extending EventEmitter for file system operations
*/
class FileMap extends EventEmitter {
/**
* Factory method to create a new FileMap instance
* @param options - Configuration options for the FileMap
* @returns New FileMap instance
*/
static create(options: InputOptions): FileMap;
/**
* Create a new FileMap instance
* @param options - Configuration options for the FileMap
*/
constructor(options: InputOptions);
/**
* Build the file map by crawling and processing files
* @returns Promise resolving to build result with file system and maps
*/
build(): Promise<BuildResult>;
/**
* Read cached data if available
* @returns Promise resolving to cached data or null if not available
*/
read(): Promise<CacheData | null>;
/**
* Clean up resources, stop watching, and close workers
* @returns Promise that resolves when cleanup is complete
*/
end(): Promise<void>;
}Usage Examples:
import FileMap from "metro-file-map";
// Create FileMap with basic configuration
const fileMap = new FileMap({
extensions: ['.js', '.jsx', '.ts', '.tsx'],
platforms: ['ios', 'android', 'native', 'web'],
retainAllFiles: false,
rootDir: process.cwd(),
roots: ['./src', './lib'],
maxWorkers: require('os').cpus().length,
healthCheck: {
enabled: false,
interval: 30000,
timeout: 5000,
filePrefix: 'metro-file-map-health-check'
}
});
// Alternative factory method
const fileMap2 = FileMap.create(options);Orchestrates the complete file system analysis process.
/**
* Build result containing file system and module maps
*/
interface BuildResult {
/** Virtual file system with querying capabilities */
fileSystem: FileSystem;
/** Haste module resolution map */
hasteMap: HasteMap;
/** Mock module map (null if mocks disabled) */
mockMap: MockMap | null;
}Usage Examples:
// Perform complete file system build
const { fileSystem, hasteMap, mockMap } = await fileMap.build();
// Query results
const allFiles = fileSystem.getAllFiles();
console.log(`Found ${allFiles.length} files`);
// Find module by name
const componentPath = hasteMap.getModule('MyComponent', 'ios');
if (componentPath) {
console.log(`MyComponent found at: ${componentPath}`);
}FileMap extends EventEmitter and emits various events during operation.
/**
* Events emitted by FileMap instance
*/
interface FileMapEvents {
/** Emitted when files change during watch mode */
'change': (event: ChangeEvent) => void;
/** Emitted for watcher status updates */
'status': (status: WatcherStatus) => void;
/** Emitted for health check results (if enabled) */
'healthCheck': (result: HealthCheckResult) => void;
/** Emitted when metadata is updated */
'metadata': () => void;
}Usage Examples:
// Listen for file changes in watch mode
fileMap.on('change', (changeEvent) => {
console.log('Files changed:', changeEvent.eventsQueue.length);
changeEvent.eventsQueue.forEach(event => {
console.log(`${event.type}: ${event.filePath}`);
});
});
// Listen for watcher status
fileMap.on('status', (status) => {
if (status.type === 'watchman_slow_command') {
console.log(`Slow ${status.command} command: ${status.timeElapsed}ms`);
}
});
// Enable watch mode
const watchingFileMap = new FileMap({
...options,
watch: true,
useWatchman: true
});Static constants and utilities available on the FileMap class.
/**
* Constants for FileMetadata array indices
*/
static H: {
ID: 0,
MTIME: 1,
SIZE: 2,
VISITED: 3,
DEPENDENCIES: 4,
SHA1: 5,
SYMLINK: 6,
PATH: 0,
TYPE: 1,
MODULE: 0,
PACKAGE: 1,
GENERIC_PLATFORM: 'g',
NATIVE_PLATFORM: 'native',
DEPENDENCY_DELIM: '\0'
};Usage Examples:
// Access metadata constants
const H = FileMap.H;
// Use constants to read file metadata
function getModuleId(fileMetadata) {
return fileMetadata[H.ID];
}
function getModifiedTime(fileMetadata) {
return fileMetadata[H.MTIME];
}FileMap integrates with cache managers for persistent storage.
/**
* Cache data structure
*/
interface CacheData {
clocks: WatchmanClocks;
fileSystemData: unknown;
plugins: ReadonlyMap<string, unknown>;
}Usage Examples:
// Read existing cache
const cachedData = await fileMap.read();
if (cachedData) {
console.log('Using cached data');
} else {
console.log('No cache available, performing full crawl');
}
// Custom cache manager
import { DiskCacheManager } from "metro-file-map";
const customCacheManager = new DiskCacheManager({
buildParameters: options,
cacheDirectory: './custom-cache',
cacheFilePrefix: 'my-app'
});
const fileMapWithCustomCache = new FileMap({
...options,
cacheManagerFactory: () => customCacheManager
});type WatcherStatus =
| {
type: 'watchman_slow_command';
timeElapsed: number;
command: 'watch-project' | 'query';
}
| {
type: 'watchman_slow_command_complete';
timeElapsed: number;
command: 'watch-project' | 'query';
}
| {
type: 'watchman_warning';
warning: unknown;
command: 'watch-project' | 'query';
};
interface HealthCheckResult {
isHealthy: boolean;
reason?: string;
}
type WatchmanClocks = Map<string, WatchmanClockSpec>;
type WatchmanClockSpec = string | {
scm: {
'mergebase-with': string;
};
};