50 lines
1.2 KiB
JavaScript
50 lines
1.2 KiB
JavaScript
const fs = require('fs-extra');
|
|
const path = require('path');
|
|
const { getDestinationDecision, isCadFile } = require('./router');
|
|
const { buildDestinationIndex } = require('./destinationIndex');
|
|
|
|
async function processFolder(folder, config) {
|
|
const entries = await fs.readdir(folder, { withFileTypes: true });
|
|
const destinationIndex = await buildDestinationIndex(config?.destination);
|
|
const result = {
|
|
scanned: 0,
|
|
copied: 0,
|
|
skipped: 0,
|
|
details: [],
|
|
};
|
|
|
|
for (const entry of entries) {
|
|
if (!entry.isFile()) {
|
|
continue;
|
|
}
|
|
|
|
const file = entry.name;
|
|
result.scanned += 1;
|
|
|
|
if (!isCadFile(file)) {
|
|
result.skipped += 1;
|
|
continue;
|
|
}
|
|
|
|
const src = path.join(folder, file);
|
|
const decision = getDestinationDecision(file, config, destinationIndex);
|
|
const destDir = decision.destination;
|
|
|
|
if (!destDir) {
|
|
result.skipped += 1;
|
|
result.details.push({ file, reason: decision.reason || 'Nessuna regola trovata' });
|
|
continue;
|
|
}
|
|
|
|
const dest = path.join(destDir, file);
|
|
await fs.copy(src, dest, { overwrite: true });
|
|
|
|
result.copied += 1;
|
|
result.details.push({ file, destination: destDir });
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
module.exports = { processFolder };
|