import fs from 'fs';
import path from 'path';
export class FileOperations {
/**
* Read file content safely
*/
readFile(filePath: string): string | null {
try {
return fs.readFileSync(filePath, 'utf8');
} catch (error) {
console.error(`Failed to read file ${filePath}:`, error);
return null;
}
}
/**
* Write file content safely
*/
writeFile(filePath: string, content: string): boolean {
try {
// Ensure directory exists
const dir = path.dirname(filePath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
fs.writeFileSync(filePath, content, 'utf8');
return true;
} catch (error) {
console.error(`Failed to write file ${filePath}:`, error);
return false;
}
}
/**
* Append to file
*/
appendFile(filePath: string, content: string): boolean {
try {
const dir = path.dirname(filePath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
fs.appendFileSync(filePath, content, 'utf8');
return true;
} catch (error) {
console.error(`Failed to append to file ${filePath}:`, error);
return false;
}
}
/**
* Check if file exists
*/
fileExists(filePath: string): boolean {
try {
return fs.existsSync(filePath);
} catch {
return false;
}
}
/**
* Delete file
*/
deleteFile(filePath: string): boolean {
try {
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
}
return true;
} catch (error) {
console.error(`Failed to delete file ${filePath}:`, error);
return false;
}
}
/**
* Create backup of file
*/
createBackup(filePath: string): string | null {
try {
if (!fs.existsSync(filePath)) {
return null;
}
const backupPath = `${filePath}.backup.${Date.now()}`;
fs.copyFileSync(filePath, backupPath);
return backupPath;
} catch (error) {
console.error(`Failed to create backup of ${filePath}:`, error);
return null;
}
}
/**
* Restore from backup
*/
restoreBackup(backupPath: string, originalPath: string): boolean {
try {
if (!fs.existsSync(backupPath)) {
return false;
}
fs.copyFileSync(backupPath, originalPath);
return true;
} catch (error) {
console.error(`Failed to restore from backup ${backupPath}:`, error);
return false;
}
}
/**
* Get file stats
*/
getFileStats(filePath: string): {
exists: boolean;
size: number;
modified: Date | null;
isFile: boolean;
} {
try {
const stats = fs.statSync(filePath);
return {
exists: true,
size: stats.size,
modified: stats.mtime,
isFile: stats.isFile()
};
} catch {
return {
exists: false,
size: 0,
modified: null,
isFile: false
};
}
}
/**
* Find files by pattern
*/
findFiles(basePath: string, pattern: RegExp): string[] {
const results: string[] = [];
try {
const entries = fs.readdirSync(basePath, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(basePath, entry.name);
if (entry.isDirectory()) {
// Skip common directories
if (!['node_modules', '.git', 'dist', 'build', '.next'].includes(entry.name)) {
results.push(...this.findFiles(fullPath, pattern));
}
} else if (entry.isFile()) {
if (pattern.test(entry.name)) {
results.push(fullPath);
}
}
}
} catch (error) {
// Skip directories we can't read
}
return results;
}
/**
* Get relative path from base
*/
getRelativePath(filePath: string, basePath: string): string {
return path.relative(basePath, filePath);
}
/**
* Normalize file path
*/
normalizePath(filePath: string): string {
return path.normalize(filePath).replace(/\\/g, '/');
}
}