1. /**
  2. * Loads resources from text files for development with a dry-run mode.
  3. *
  4. * @param {string[]} filePaths An array of file paths to load.
  5. * @param {boolean} dryRun Whether to only log the loading process without actually loading the resources. Defaults to true.
  6. */
  7. async function loadTextFiles(filePaths, dryRun = true) {
  8. for (const filePath of filePaths) {
  9. try {
  10. // Check if the file exists
  11. if (!fs.existsSync(filePath)) {
  12. console.warn(`File not found: ${filePath}`);
  13. continue; // Skip to the next file
  14. }
  15. //Read the file content
  16. const fileContent = fs.readFileSync(filePath, 'utf8');
  17. if (dryRun) {
  18. console.log(`[DRY-RUN] Loading: ${filePath}`);
  19. console.log(`Content Preview: ${fileContent.substring(0, 100)}...`); //Show first 100 characters
  20. } else {
  21. // Actual loading logic (replace with your resource loading)
  22. console.log(`Loading: ${filePath}`);
  23. //Implement your resource loading here, e.g., parse JSON, etc.
  24. //return await parseFileContent(fileContent); //Example, uncomment if needed
  25. }
  26. } catch (error) {
  27. console.error(`Error loading ${filePath}: ${error}`);
  28. }
  29. }
  30. }
  31. // Import the 'fs' module (Node.js file system)
  32. const fs = require('fs');
  33. //Example Usage:
  34. // const filesToLoad = ['data/config.txt', 'data/messages.txt'];
  35. // loadTextFiles(filesToLoad);

Add your comment