196 lines
6.5 KiB
JavaScript
196 lines
6.5 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
class FileSystemManager {
|
|
/**
|
|
* @param {string} jsonPath - Pfad zur JSON-Datei
|
|
* @param {boolean} watch - ob die JSON-Datei überwacht werden soll (Live-Update)
|
|
*/
|
|
constructor() { }
|
|
|
|
loadAllFiles(path, fileextension = null) {
|
|
let files = {};
|
|
fs.readdirSync(path).forEach(file => {
|
|
if (fileextension == null || file.endsWith(fileextension)) {
|
|
const filefound = require(`${path}/${file}`);
|
|
files = { ...files, ...filefound }; // zusammenführen
|
|
}
|
|
});
|
|
return files;
|
|
}
|
|
|
|
getAllFiles(path, fileextension = null) {
|
|
let files = {};
|
|
fs.readdirSync(path).forEach(file => {
|
|
if (fileextension == null || file.endsWith(fileextension)) {
|
|
const filefound = `${path}/${file}`;
|
|
files = { ...files, ...filefound }; // zusammenführen
|
|
}
|
|
});
|
|
return files;
|
|
}
|
|
|
|
|
|
/**
|
|
* Liest rekursiv Dateien und gibt nur die gewünschten Attribute zurück.
|
|
*
|
|
* @param {string} dirPath - Startverzeichnis
|
|
* @param {string[]} attributes - gewünschte Attribute:
|
|
* ['name','fullPath','size','lastModified','isDirectory','extension', ...]
|
|
* @param {string|null} sortBy - Attribut zum Sortieren (z.B. 'lastModified' oder 'name')
|
|
* @param {string} order - 'asc' oder 'desc'
|
|
*/
|
|
readFiles(dirPath, attributes = ['name', 'fullPath'], sortBy = null, order = 'asc') {
|
|
let results = [];
|
|
|
|
const items = fs.readdirSync(dirPath);
|
|
|
|
for (const item of items) {
|
|
const fullPath = path.join(dirPath, item);
|
|
const stats = fs.statSync(fullPath);
|
|
const isDir = stats.isDirectory();
|
|
|
|
// Objekt mit ALLEN möglichen Infos
|
|
const allInfo = {
|
|
name: item,
|
|
nameWithoutExt: item.substring(0, item.indexOf('.')),
|
|
fullPath: fullPath,
|
|
size: stats.size,
|
|
lastModified: stats.mtime,
|
|
created: stats.birthtime,
|
|
isDirectory: isDir,
|
|
extension: isDir ? null : path.extname(item)
|
|
};
|
|
|
|
if (isDir) {
|
|
// rekursiv weitermachen
|
|
results = results.concat(this.readFiles(fullPath, attributes, null, order));
|
|
} else {
|
|
// nur gewünschte Attribute ausgeben
|
|
const filtered = {};
|
|
for (const attr of attributes) {
|
|
if (allInfo[attr] !== undefined) {
|
|
filtered[attr] = allInfo[attr];
|
|
}
|
|
}
|
|
results.push(filtered);
|
|
}
|
|
}
|
|
|
|
// Sortieren, falls gewünscht
|
|
if (sortBy) {
|
|
results.sort((a, b) => {
|
|
if (a[sortBy] < b[sortBy]) return order === 'asc' ? -1 : 1;
|
|
if (a[sortBy] > b[sortBy]) return order === 'asc' ? 1 : -1;
|
|
return 0;
|
|
});
|
|
}
|
|
return results;
|
|
}
|
|
|
|
|
|
/**
|
|
* Sammelt verschiedene Pattern-Ergebnisse aus Dateien mehrerer Ordner.
|
|
*
|
|
* @param {Object} options
|
|
* @param {string|string[]} options.folderPaths - Pfad oder Array von Pfaden zu Ordnern
|
|
* @param {string} [options.extension='.js'] - Dateiendung
|
|
* @param {Array<{ name: string, pattern: RegExp, mapFn?: Function }>} options.patterns - Liste von Pattern-Definitionen
|
|
* @param {boolean} [options.recursive=true] - Unterordner durchsuchen?
|
|
* @returns {Array} - kombinierte Ergebnisse aller Pattern
|
|
*/
|
|
collectFromFiles({
|
|
folderPaths,
|
|
extension = '.js',
|
|
patterns,
|
|
recursive = true
|
|
}) {
|
|
const results = [];
|
|
|
|
// if only one path selected
|
|
const paths = Array.isArray(folderPaths) ? folderPaths : [folderPaths];
|
|
|
|
function readDir(dir) {
|
|
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
|
|
for (const entry of entries) {
|
|
const fullPath = path.join(dir, entry.name);
|
|
|
|
if (entry.isDirectory() && recursive) {
|
|
readDir(fullPath);
|
|
} else if (entry.isFile() && entry.name.endsWith(extension)) {
|
|
const content = fs.readFileSync(fullPath, 'utf8');
|
|
|
|
// 👉 NEU: fallback wenn keine patterns
|
|
if (!patterns || patterns.length === 0) {
|
|
results.push({
|
|
file: entry.name,
|
|
fullPath
|
|
});
|
|
continue;
|
|
}
|
|
|
|
for (const { name, pattern, mapFn } of patterns) {
|
|
let match;
|
|
while ((match = pattern.exec(content)) !== null) {
|
|
const mapped = mapFn
|
|
? mapFn(match, entry.name, fullPath, name)
|
|
: { file: entry.name, type: name, match: match[0] };
|
|
results.push(mapped);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Run through multiple paths
|
|
for (const dir of paths) {
|
|
if (fs.existsSync(dir)) {
|
|
readDir(path.resolve(dir));
|
|
} else {
|
|
console.warn(`Ordner nicht gefunden: ${dir}`);
|
|
}
|
|
}
|
|
|
|
return results;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// JSON-Datei laden
|
|
loadJSON(path) {
|
|
try {
|
|
const rawData = fs.readFileSync(path, 'utf8');
|
|
return JSON.parse(rawData);
|
|
} catch (err) {
|
|
console.log(err)
|
|
return err
|
|
}
|
|
}
|
|
|
|
loadFile(path) {
|
|
try {
|
|
const rawData = fs.readFileSync(path, 'utf8');
|
|
return rawData;
|
|
} catch (err) {
|
|
console.log(err)
|
|
return err
|
|
}
|
|
}
|
|
|
|
|
|
// Check file-/path
|
|
exists(path) {
|
|
try {
|
|
const info = fs.statSync(path);
|
|
if (!info.isFile() && !info.isDirectory()) return { status: false, levelId: 4, message: `${info.isFile() ? 'Datei' : 'Pfad'} ${path} existiert nicht` };
|
|
return { status: true, levelId: 0, message: `${info.isFile() ? 'Datei' : 'Pfad'} existiert: ${path}` };
|
|
} catch (err) {
|
|
return ;
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = FileSystemManager; |