1. /**
  2. * Provides a wrapper around file path manipulation logic, allowing for manual overrides.
  3. */
  4. const pathManager = {
  5. /**
  6. * Resolves a file path, allowing for manual override of specific parts.
  7. * @param {string} basePath - The base directory for the path.
  8. * @param {string} relativePath - The relative path from the base.
  9. * @param {object} overrides - An object to override parts of the path (e.g., { 'folder1': 'newFolder' }).
  10. * @returns {string} The resolved file path.
  11. */
  12. resolvePath: (basePath, relativePath, overrides = {}) => {
  13. let resolvedPath = path.resolve(basePath, relativePath);
  14. // Apply overrides
  15. for (const part in overrides) {
  16. if (resolvedPath.includes(part)) {
  17. resolvedPath = resolvedPath.replace(part, overrides[part]);
  18. }
  19. }
  20. return resolvedPath;
  21. },
  22. /**
  23. * Creates a directory path, allowing for manual override.
  24. * @param {string} baseDir - The base directory.
  25. * @param {string} dirName - The name of the directory to create.
  26. * @param {object} overrides - An object to override parts of the path.
  27. * @returns {string} The full directory path.
  28. */
  29. createDirPath: (baseDir, dirName, overrides = {}) => {
  30. let pathSegments = baseDir.split(path.sep);
  31. for (let i = 0; i < pathSegments.length; i++) {
  32. if (overrides[i]) {
  33. pathSegments[i] = overrides[i];
  34. }
  35. }
  36. return path.join(...pathSegments, dirName);
  37. },
  38. /**
  39. * Gets the file extension from a path.
  40. * @param {string} filePath The file path.
  41. * @returns {string} The file extension, or an empty string if no extension.
  42. */
  43. getFileExtension: (filePath) => {
  44. const lastDotIndex = filePath.lastIndexOf('.');
  45. return lastDotIndex === -1 ? '' : filePath.substring(lastDotIndex + 1);
  46. }
  47. };
  48. const path = require('path'); //Import path module

Add your comment