refactor: usa decisione di destinazione unificata per folder e zip
This commit is contained in:
46
services/destinationIndex.js
Normal file
46
services/destinationIndex.js
Normal file
@@ -0,0 +1,46 @@
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
|
||||
const CODE_FOLDER_REGEX = /^\d{3}$/;
|
||||
const MAX_SCAN_DEPTH = 6;
|
||||
|
||||
async function buildDestinationIndex(destinationRoot) {
|
||||
const index = new Map();
|
||||
|
||||
if (!destinationRoot || !(await fs.pathExists(destinationRoot))) {
|
||||
return index;
|
||||
}
|
||||
|
||||
async function walk(currentDir, depth) {
|
||||
if (depth > MAX_SCAN_DEPTH) {
|
||||
return;
|
||||
}
|
||||
|
||||
let entries;
|
||||
try {
|
||||
entries = await fs.readdir(currentDir, { withFileTypes: true });
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const entry of entries) {
|
||||
if (!entry.isDirectory()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const fullPath = path.join(currentDir, entry.name);
|
||||
if (CODE_FOLDER_REGEX.test(entry.name)) {
|
||||
const rows = index.get(entry.name) || [];
|
||||
rows.push(fullPath);
|
||||
index.set(entry.name, rows);
|
||||
}
|
||||
|
||||
await walk(fullPath, depth + 1);
|
||||
}
|
||||
}
|
||||
|
||||
await walk(destinationRoot, 0);
|
||||
return index;
|
||||
}
|
||||
|
||||
module.exports = { buildDestinationIndex };
|
||||
Reference in New Issue
Block a user