feat(router): gestisce versioni duplicate mantenendo solo la più alta nello smistamento
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
const os = require('os');
|
||||
const { getCadInfo } = require('./router');
|
||||
|
||||
const PRIMARY_UNROUTED_DIR = '/cadroute/__NON_SMISTATI';
|
||||
const HOME_UNROUTED_DIR = path.join(os.homedir(), '.cadroute', '__NON_SMISTATI');
|
||||
@@ -83,6 +84,100 @@ async function getDuplicateTarget(fileName) {
|
||||
return getTarget('duplicates', fileName);
|
||||
}
|
||||
|
||||
function parseNumericVersion(version) {
|
||||
const rawVersion = String(version || '').trim();
|
||||
if (!rawVersion || !/^\d+$/.test(rawVersion)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return BigInt(rawVersion);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeCadKey(cadInfo) {
|
||||
return String(cadInfo?.key || '').toLowerCase();
|
||||
}
|
||||
|
||||
async function findComparableVersionFiles(destinationDir, incomingCadInfo) {
|
||||
const incomingKey = normalizeCadKey(incomingCadInfo);
|
||||
const entries = await fs.readdir(destinationDir, { withFileTypes: true }).catch(() => []);
|
||||
const comparable = [];
|
||||
|
||||
for (const entry of entries) {
|
||||
if (!entry.isFile()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const cadInfo = getCadInfo(entry.name);
|
||||
if (!cadInfo || normalizeCadKey(cadInfo) !== incomingKey) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const numericVersion = parseNumericVersion(cadInfo.version);
|
||||
if (numericVersion === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
comparable.push({
|
||||
path: path.join(destinationDir, entry.name),
|
||||
version: numericVersion,
|
||||
name: entry.name,
|
||||
});
|
||||
}
|
||||
|
||||
return comparable;
|
||||
}
|
||||
|
||||
async function prepareSpecialTarget(kind, fileName) {
|
||||
const destinationDir = await resolveTargetDir(kind);
|
||||
const incomingCadInfo = getCadInfo(fileName);
|
||||
if (!incomingCadInfo) {
|
||||
const destinationPath = await getUniquePath(destinationDir, fileName);
|
||||
return { shouldCopy: true, destinationDir, destinationPath };
|
||||
}
|
||||
|
||||
const incomingVersion = parseNumericVersion(incomingCadInfo.version);
|
||||
if (incomingVersion === null) {
|
||||
const destinationPath = await getUniquePath(destinationDir, fileName);
|
||||
return { shouldCopy: true, destinationDir, destinationPath };
|
||||
}
|
||||
|
||||
const comparable = await findComparableVersionFiles(destinationDir, incomingCadInfo);
|
||||
if (!comparable.length) {
|
||||
const destinationPath = await getUniquePath(destinationDir, fileName);
|
||||
return { shouldCopy: true, destinationDir, destinationPath };
|
||||
}
|
||||
|
||||
const highest = comparable.reduce((max, row) => (row.version > max.version ? row : max));
|
||||
if (incomingVersion <= highest.version) {
|
||||
return {
|
||||
shouldCopy: false,
|
||||
destinationDir,
|
||||
reason: `Versione piu alta gia presente (${highest.name})`,
|
||||
};
|
||||
}
|
||||
|
||||
await Promise.all(comparable.map((row) => fs.remove(row.path)));
|
||||
const destinationPath = await getUniquePath(destinationDir, fileName);
|
||||
return {
|
||||
shouldCopy: true,
|
||||
destinationDir,
|
||||
destinationPath,
|
||||
cleaned: comparable.length,
|
||||
};
|
||||
}
|
||||
|
||||
async function prepareUnroutedTarget(fileName) {
|
||||
return prepareSpecialTarget('unrouted', fileName);
|
||||
}
|
||||
|
||||
async function prepareDuplicateTarget(fileName) {
|
||||
return prepareSpecialTarget('duplicates', fileName);
|
||||
}
|
||||
|
||||
async function listFilesRecursively(rootDir) {
|
||||
const files = [];
|
||||
|
||||
@@ -156,6 +251,8 @@ async function clearDuplicateFiles() {
|
||||
module.exports = {
|
||||
getUnroutedTarget,
|
||||
getDuplicateTarget,
|
||||
prepareUnroutedTarget,
|
||||
prepareDuplicateTarget,
|
||||
resolveUnroutedDir,
|
||||
resolveDuplicatesDir,
|
||||
listUnroutedFiles,
|
||||
|
||||
Reference in New Issue
Block a user