ui5_easy_use
Version:
A utility package for UI5 projects
133 lines (101 loc) • 4.45 kB
JavaScript
// const fs = require('fs');
// const path = require('path');
// /**
// * Function to loop through the `ez5_core` folder and return paths with `content` folder.
// * @param {string} baseDir - The base directory to start searching.
// * @returns {Array} Array of paths to `content` folders inside `ez5_core`.
// */
// function getContentPaths(baseDir) {
// const result = [];
// function findContentFolders(currentDir) {
// // Read all items (files and directories) in the current directory
// const items = fs.readdirSync(currentDir, { withFileTypes: true });
// // Loop through each item
// items.forEach(item => {
// const itemPath = path.join(currentDir, item.name);
// if (item.isDirectory()) {
// const contentPath = path.join(itemPath, 'content');
// // Check if the `content` folder exists inside the current directory
// if (fs.existsSync(contentPath) && fs.statSync(contentPath).isDirectory()) {
// result.push({
// name: path.relative(baseDir, itemPath).replace(/\\/g, '/'),
// path: contentPath,
// });
// }
// // Recursively check subdirectories
// findContentFolders(itemPath);
// }
// });
// }
// // Start recursion from the base directory
// findContentFolders(baseDir);
// return result;
// }
// Example usage
// const myPackageDir = path.resolve(__dirname, 'ez5_core');
// const contentFolders = getContentPaths(myPackageDir);
// console.log('Content Folders:', contentFolders);
/**
* Output example:
* [
* { name: 'sharingFunctions', path: '/absolute/path/ez5_core/sharingFunctions/content' },
* { name: 'pagemt', path: '/absolute/path/ez5_core/pagemt/content' },
* { name: 'components', path: '/absolute/path/ez5_core/components/content' }
* ]
*/
// module.exports = { getContentPaths };
const path = require('path');
const fs = require('fs');
function getContentPaths(baseDir) {
const result = [];
function findContentFolders(currentDir) {
// Read all items (files and directories) in the current directory
const items = fs.readdirSync(currentDir, { withFileTypes: true });
// Loop through each item
items.forEach(item => {
const itemPath = path.join(currentDir, item.name);
if (item.isDirectory()) {
const contentPath = path.join(itemPath, 'content');
// Check if the `content` folder exists inside the current directory
if (fs.existsSync(contentPath) && fs.statSync(contentPath).isDirectory()) {
result.push({
name: path.relative(baseDir, itemPath).replace(/\\/g, '/'),
path: contentPath,
});
}
// Recursively check subdirectories
findContentFolders(itemPath);
}
});
}
// Start recursion from the base directory
findContentFolders(baseDir);
return result;
}
/**
* Filters out specific files and folders from contentPaths
* @param {Array} contentPaths Array from getContentPaths
* @param {Object} options
* @param {string[]} [options.excludeFiles] Relative paths from baseDir to exclude
* @param {string[]} [options.excludeFolders] Relative paths from baseDir to exclude
* @param {string} baseDir Base directory for relative paths
*/
function filterContentPaths(contentPaths, { excludeFiles = [], excludeFolders = [] }, baseDir) {
return contentPaths.filter(folder => {
const folderRel = folder.name; // relative name from baseDir
// Exclude if in excluded folders list
if (excludeFolders.some(exFolder => folderRel.startsWith(exFolder))) {
return false;
}
// Check if any excluded file exists inside this folder
const excludedInside = excludeFiles.some(exFile =>
path.resolve(baseDir, exFile).startsWith(folder.path)
);
return !excludedInside;
});
}
module.exports = {
getContentPaths,
filterContentPaths
};
module.exports = { getContentPaths, filterContentPaths };