File System
The File System module provides a comprehensive set of APIs for file and directory operations. It follows a singleton pattern and is accessible through the fs instance.
Overview
The File System module offers a unified interface for:
- File and directory management
- File content operations
- Compression and decompression
- File system event monitoring
Features
- File and directory management (create, delete, rename, move, copy)
- File content operations (read, write, append)
- Compression and decompression (zip, tar)
- File system event monitoring
- Cross-platform path handling
- Asynchronous operations with Promise support
Examples
Listing Files and Directories
// List all items in a directory
const items = await fs.ls('/path/to/directory');
// List only directories
const directories = await fs.ls('/path/to/directory', true);
File and Directory Management
// Check if a path exists
const exists = await fs.exists('/path/to/file');
// Check if a path is a directory
const isDir = await fs.isDirectory('/path/to/directory');
// Check if a path is a file
const isFile = await fs.isFile('/path/to/file');
// Create a directory
await fs.mkdir('/path/to/new/directory', true); // true for creating parent directories
// Remove a file
await fs.rm('/path/to/file');
// Remove a directory
await fs.rmdir('/path/to/directory', true); // true for recursive removal
// Rename a file or directory
await fs.ren('/path/to/old', 'new-name');
File Content Operations
// Read file content
const content = await fs.read('/path/to/file');
// Write content to a file
await fs.write('/path/to/file', 'content', 'truncate'); // truncate, create, or append
// Create an empty file
await fs.touch('/path/to/file');
File Copy and Move
// Copy a file or directory
await fs.cp('/path/to/source', '/path/to/target');
// Move a file or directory
await fs.mv('/path/to/source', '/path/to/target');
Compression and Decompression
// Compress files to ZIP
await fs.zip(['/path/to/file1', '/path/to/file2'], '/path/to/archive.zip');
// Extract ZIP archive
await fs.unzip('/path/to/archive.zip', '/path/to/extract/to');
// Create TAR archive
await fs.tar(['/path/to/file1', '/path/to/file2'], '/path/to/archive.tar', 'gzip');
// Extract TAR archive
await fs.untar('/path/to/archive.tar', '/path/to/extract/to', 'gzip');
Best Practices
-
Path Handling
- Use absolute paths when possible
- Handle path separators correctly for cross-platform compatibility
- Validate paths before operations
-
Resource Management
- Clean up temporary files
- Use appropriate write modes for file operations
- Handle large files with care
-
Error Handling
- Implement proper error handling for all file operations
- Check file existence before operations
- Handle permission errors appropriately
- Use try-catch blocks for error handling:
try {
await fs.write('/path/to/file', 'content');
} catch (error) {
console.error('Error writing file:', error);
} -
Performance
- Use streaming for large files transfer
- Implement proper caching mechanisms
- Batch operations when possible
- Choose appropriate compression modes for archives