/**
* Provides a wrapper around file path manipulation logic, allowing for manual overrides.
*/
const pathManager = {
/**
* Resolves a file path, allowing for manual override of specific parts.
* @param {string} basePath - The base directory for the path.
* @param {string} relativePath - The relative path from the base.
* @param {object} overrides - An object to override parts of the path (e.g., { 'folder1': 'newFolder' }).
* @returns {string} The resolved file path.
*/
resolvePath: (basePath, relativePath, overrides = {}) => {
let resolvedPath = path.resolve(basePath, relativePath);
// Apply overrides
for (const part in overrides) {
if (resolvedPath.includes(part)) {
resolvedPath = resolvedPath.replace(part, overrides[part]);
}
}
return resolvedPath;
},
/**
* Creates a directory path, allowing for manual override.
* @param {string} baseDir - The base directory.
* @param {string} dirName - The name of the directory to create.
* @param {object} overrides - An object to override parts of the path.
* @returns {string} The full directory path.
*/
createDirPath: (baseDir, dirName, overrides = {}) => {
let pathSegments = baseDir.split(path.sep);
for (let i = 0; i < pathSegments.length; i++) {
if (overrides[i]) {
pathSegments[i] = overrides[i];
}
}
return path.join(...pathSegments, dirName);
},
/**
* Gets the file extension from a path.
* @param {string} filePath The file path.
* @returns {string} The file extension, or an empty string if no extension.
*/
getFileExtension: (filePath) => {
const lastDotIndex = filePath.lastIndexOf('.');
return lastDotIndex === -1 ? '' : filePath.substring(lastDotIndex + 1);
}
};
const path = require('path'); //Import path module
Add your comment