1. /**
  2. * Syncs resources based on CLI arguments.
  3. *
  4. * @param {object} args - An object containing CLI arguments.
  5. * @returns {object} - An object containing the synced resources.
  6. */
  7. function syncResources(args) {
  8. const {
  9. sourceDir,
  10. targetDir,
  11. includePattern,
  12. excludePattern,
  13. dryRun,
  14. } = args;
  15. const resources = [];
  16. //Handle source directory
  17. if (sourceDir) {
  18. try {
  19. const files = require('fs').readdirSync(sourceDir);
  20. for (const file of files) {
  21. if (!excludePattern || !includePattern(file, excludePattern)) {
  22. resources.push({ source: `${sourceDir}/${file}`, target: `${targetDir}/${file}` });
  23. }
  24. }
  25. } catch (err) {
  26. console.error(`Error reading source directory: ${err.message}`);
  27. return {}; // or throw the error, depending on desired behavior
  28. }
  29. }
  30. // Apply dry run if requested
  31. if (dryRun) {
  32. console.log("Dry run: Would sync the following resources:");
  33. console.log(resources);
  34. return resources; //Return resources to show what *would* be synced
  35. }
  36. // Perform the actual sync
  37. for (const resource of resources) {
  38. try {
  39. //Implement logic to copy/sync file
  40. console.log(`Syncing ${resource.source} to ${resource.target}`);
  41. //Example using fs.copyFileSync (replace with your preferred syncing method)
  42. require('fs').copyFileSync(resource.source, resource.target);
  43. } catch (err) {
  44. console.error(`Error syncing ${resource.source}: ${err.message}`);
  45. }
  46. }
  47. return resources; //Return the synced resources
  48. }
  49. //Helper function for pattern matching
  50. function includePattern(filename, excludePattern) {
  51. if (!excludePattern) {
  52. return true; //include if no exclude pattern
  53. }
  54. return !excludePattern.test(filename);
  55. }

Add your comment