Mostra una progress bar con messaggio contestuale nelle 4 fasi: scansione sorgente, analisi cartella destinazione (con contatore cartelle), ricerca duplicati (con contatore file CAD), smistamento (N / totale). La barra è indeterminata nelle prime tre fasi e determinata durante la copia. Il canale IPC progress viene aperto all'inizio e chiuso al termine dell'operazione
60 lines
1.2 KiB
JavaScript
60 lines
1.2 KiB
JavaScript
const fs = require('fs-extra');
|
|
const path = require('path');
|
|
const { getCadInfo } = require('./router');
|
|
|
|
const MAX_SCAN_DEPTH = 8;
|
|
|
|
function toCadKey(cadInfo) {
|
|
return String(cadInfo?.key || '').toLowerCase();
|
|
}
|
|
|
|
async function buildExistingCadKeyIndex(destinationRoot, onProgress) {
|
|
const keys = new Set();
|
|
|
|
if (!destinationRoot || !(await fs.pathExists(destinationRoot))) {
|
|
return keys;
|
|
}
|
|
|
|
let scanned = 0;
|
|
|
|
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) {
|
|
const fullPath = path.join(currentDir, entry.name);
|
|
|
|
if (entry.isDirectory()) {
|
|
await walk(fullPath, depth + 1);
|
|
continue;
|
|
}
|
|
|
|
if (!entry.isFile()) {
|
|
continue;
|
|
}
|
|
|
|
const cadInfo = getCadInfo(entry.name);
|
|
if (!cadInfo) {
|
|
continue;
|
|
}
|
|
|
|
scanned += 1;
|
|
onProgress?.({ phase: 'index-dup', scanned, file: entry.name });
|
|
keys.add(toCadKey(cadInfo));
|
|
}
|
|
}
|
|
|
|
await walk(destinationRoot, 0);
|
|
return keys;
|
|
}
|
|
|
|
module.exports = { buildExistingCadKeyIndex, toCadKey };
|