const fs = require('fs');
const path = require('path');
/**
* Flushes file paths from a legacy project, handling errors gracefully.
* @param {string} rootDir - The root directory to search for files.
*/
async function flushFilePaths(rootDir) {
try {
const files = await findFiles(rootDir);
if (files.length > 0) {
console.log('Found files:');
files.forEach(file => console.log(file));
} else {
console.log('No files found.');
}
} catch (error) {
console.error('Error flushing file paths:', error.message);
}
}
/**
* Recursively finds all files within a directory.
* @param {string} dir - The directory to search.
* @returns {Promise<string[]>} - A promise that resolves to an array of file paths.
*/
async function findFiles(dir) {
return new Promise((resolve, reject) => {
fs.readdir(dir, (err, files) => {
if (err) {
reject(err);
return;
}
const filePaths = [];
for (const file of files) {
const fullPath = path.join(dir, file);
fs.stat(fullPath, (err, stat) => {
if (err) {
console.warn(`Error getting stats for ${fullPath}:`, err.message); // Log, don't reject.
return;
}
if (stat.isFile()) {
filePaths.push(fullPath);
} else if (stat.isDirectory()) {
findFiles(fullPath)
.then(subFiles => {
filePaths.push(...subFiles);
})
.catch(subError => {
console.warn(`Error in subdirectory ${fullPath}:`, subError.message); // Log, don't reject.
});
}
});
}
resolve(filePaths);
});
});
}
// Example usage (replace with your root directory)
flushFilePaths('./');
Add your comment