1. const fs = require('fs');
  2. const path = require('path');
  3. /**
  4. * Flushes file paths from a legacy project, handling errors gracefully.
  5. * @param {string} rootDir - The root directory to search for files.
  6. */
  7. async function flushFilePaths(rootDir) {
  8. try {
  9. const files = await findFiles(rootDir);
  10. if (files.length > 0) {
  11. console.log('Found files:');
  12. files.forEach(file => console.log(file));
  13. } else {
  14. console.log('No files found.');
  15. }
  16. } catch (error) {
  17. console.error('Error flushing file paths:', error.message);
  18. }
  19. }
  20. /**
  21. * Recursively finds all files within a directory.
  22. * @param {string} dir - The directory to search.
  23. * @returns {Promise<string[]>} - A promise that resolves to an array of file paths.
  24. */
  25. async function findFiles(dir) {
  26. return new Promise((resolve, reject) => {
  27. fs.readdir(dir, (err, files) => {
  28. if (err) {
  29. reject(err);
  30. return;
  31. }
  32. const filePaths = [];
  33. for (const file of files) {
  34. const fullPath = path.join(dir, file);
  35. fs.stat(fullPath, (err, stat) => {
  36. if (err) {
  37. console.warn(`Error getting stats for ${fullPath}:`, err.message); // Log, don't reject.
  38. return;
  39. }
  40. if (stat.isFile()) {
  41. filePaths.push(fullPath);
  42. } else if (stat.isDirectory()) {
  43. findFiles(fullPath)
  44. .then(subFiles => {
  45. filePaths.push(...subFiles);
  46. })
  47. .catch(subError => {
  48. console.warn(`Error in subdirectory ${fullPath}:`, subError.message); // Log, don't reject.
  49. });
  50. }
  51. });
  52. }
  53. resolve(filePaths);
  54. });
  55. });
  56. }
  57. // Example usage (replace with your root directory)
  58. flushFilePaths('./');

Add your comment