refactor: usa decisione di destinazione unificata per folder e zip

This commit is contained in:
2026-03-05 17:35:12 +01:00
parent 5b65a9bd04
commit 0612cb0d8a
4 changed files with 115 additions and 32 deletions

View File

@@ -1,39 +1,72 @@
const path = require('path');
function getCadType(filename) {
const baseName = path.basename(filename).toLowerCase();
const match = baseName.match(/\.(prt|asm|drw|dwr)(?:\.\d+)?$/);
return match ? match[1] : null;
function normalizeCadType(ext) {
const lowerExt = String(ext || '').toLowerCase();
return lowerExt === 'drw' ? 'dwr' : lowerExt;
}
function findDestination(filename, config) {
const cadType = getCadType(filename);
const globalDestination = typeof config?.destination === 'string' ? config.destination.trim() : '';
if (globalDestination && cadType) {
return globalDestination;
function getCadInfo(filename) {
const baseName = path.basename(filename);
const match = baseName.match(/^(.*)\.(prt|asm|drw|dwr)(?:\.([^.]+))?$/i);
if (!match) {
return null;
}
for (const rule of config.rules || []) {
if ((rule.ext || '').toLowerCase() !== cadType) {
continue;
}
const rawCode = match[1];
const type = normalizeCadType(match[2]);
const version = match[3] || '';
const digits = rawCode.replace(/\D/g, '');
const routingGroup = digits.length >= 5 ? digits.slice(2, 5) : null;
if (!rule.pattern) {
return rule.destination;
}
return {
code: rawCode,
digits,
type,
version,
key: `${rawCode}.${type}`,
routingGroup,
};
}
const regex = new RegExp(rule.pattern);
if (regex.test(path.basename(filename))) {
return rule.destination;
}
function getDestinationDecision(filename, config, destinationIndex) {
const cadInfo = getCadInfo(filename);
if (!cadInfo) {
return { destination: null, reason: 'Nessuna regola trovata' };
}
return null;
if (!cadInfo.routingGroup) {
return {
destination: null,
reason: 'Nome file non conforme: servono almeno 5 cifre nel nome per ricavare la 3a-4a-5a cifra',
};
}
const group = cadInfo.routingGroup;
const candidates = destinationIndex?.get(group) || [];
if (!candidates.length) {
return {
destination: null,
reason: `Sottocartella ${group} non trovata nella destinazione`,
};
}
if (candidates.length > 1) {
return {
destination: null,
reason: `Sottocartella ${group} trovata in ${candidates.length} percorsi diversi`,
};
}
return { destination: candidates[0], reason: null };
}
function findDestination(filename, config, destinationIndex) {
return getDestinationDecision(filename, config, destinationIndex).destination;
}
function isCadFile(filename) {
return Boolean(getCadType(filename));
return Boolean(getCadInfo(filename));
}
module.exports = { findDestination, isCadFile };
module.exports = { findDestination, getDestinationDecision, isCadFile, getCadInfo };