feat(ui): aggiunge barra di progresso durante lo smistamento
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
This commit is contained in:
@@ -4,13 +4,15 @@ const path = require('path');
|
||||
const CODE_FOLDER_REGEX = /^\d{3}$/;
|
||||
const MAX_SCAN_DEPTH = 6;
|
||||
|
||||
async function buildDestinationIndex(destinationRoot) {
|
||||
async function buildDestinationIndex(destinationRoot, onProgress) {
|
||||
const index = new Map();
|
||||
|
||||
if (!destinationRoot || !(await fs.pathExists(destinationRoot))) {
|
||||
return index;
|
||||
}
|
||||
|
||||
let scanned = 0;
|
||||
|
||||
async function walk(currentDir, depth) {
|
||||
if (depth > MAX_SCAN_DEPTH) {
|
||||
return;
|
||||
@@ -29,6 +31,9 @@ async function buildDestinationIndex(destinationRoot) {
|
||||
}
|
||||
|
||||
const fullPath = path.join(currentDir, entry.name);
|
||||
scanned += 1;
|
||||
onProgress?.({ phase: 'index-dest', scanned, folder: entry.name });
|
||||
|
||||
if (CODE_FOLDER_REGEX.test(entry.name)) {
|
||||
const rows = index.get(entry.name) || [];
|
||||
rows.push(fullPath);
|
||||
|
||||
@@ -8,13 +8,15 @@ function toCadKey(cadInfo) {
|
||||
return String(cadInfo?.key || '').toLowerCase();
|
||||
}
|
||||
|
||||
async function buildExistingCadKeyIndex(destinationRoot) {
|
||||
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;
|
||||
@@ -44,6 +46,8 @@ async function buildExistingCadKeyIndex(destinationRoot) {
|
||||
continue;
|
||||
}
|
||||
|
||||
scanned += 1;
|
||||
onProgress?.({ phase: 'index-dup', scanned, file: entry.name });
|
||||
keys.add(toCadKey(cadInfo));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,10 +66,12 @@ async function collectFilesRecursively(rootDir) {
|
||||
return files;
|
||||
}
|
||||
|
||||
async function processFolder(folder, config) {
|
||||
async function processFolder(folder, config, onProgress) {
|
||||
onProgress?.({ phase: 'scan' });
|
||||
const files = await collectFilesRecursively(folder);
|
||||
const destinationIndex = await buildDestinationIndex(config?.destination);
|
||||
const existingCadKeys = await buildExistingCadKeyIndex(config?.destination);
|
||||
|
||||
const destinationIndex = await buildDestinationIndex(config?.destination, onProgress);
|
||||
const existingCadKeys = await buildExistingCadKeyIndex(config?.destination, onProgress);
|
||||
const sourceMaxVersions = buildHighestNumericVersionByCadKey(files);
|
||||
const result = {
|
||||
scanned: 0,
|
||||
@@ -80,9 +82,13 @@ async function processFolder(folder, config) {
|
||||
details: [],
|
||||
};
|
||||
|
||||
for (const { fullPath, fileName } of files) {
|
||||
const total = files.length;
|
||||
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
const { fullPath, fileName } = files[i];
|
||||
const file = fileName;
|
||||
result.scanned += 1;
|
||||
onProgress?.({ phase: 'copy', current: i + 1, total, file });
|
||||
|
||||
const cadInfo = getCadInfo(file);
|
||||
if (!cadInfo) {
|
||||
|
||||
@@ -27,7 +27,7 @@ async function buildZipHighestNumericVersionByCadKey(zipPath) {
|
||||
try {
|
||||
directory = await unzipper.Open.file(zipPath);
|
||||
} catch {
|
||||
return index;
|
||||
return { index, total: 0 };
|
||||
}
|
||||
|
||||
for (const row of directory.files || []) {
|
||||
@@ -53,14 +53,17 @@ async function buildZipHighestNumericVersionByCadKey(zipPath) {
|
||||
}
|
||||
}
|
||||
|
||||
return index;
|
||||
return { index, total: (directory.files || []).filter((f) => f.type === 'File').length };
|
||||
}
|
||||
|
||||
async function processZip(zipPath, config) {
|
||||
async function processZip(zipPath, config, onProgress) {
|
||||
onProgress?.({ phase: 'scan' });
|
||||
const { index: sourceMaxVersions, total } = await buildZipHighestNumericVersionByCadKey(zipPath);
|
||||
|
||||
const destinationIndex = await buildDestinationIndex(config?.destination, onProgress);
|
||||
const existingCadKeys = await buildExistingCadKeyIndex(config?.destination, onProgress);
|
||||
|
||||
const stream = fs.createReadStream(zipPath).pipe(unzipper.Parse({ forceStream: true }));
|
||||
const destinationIndex = await buildDestinationIndex(config?.destination);
|
||||
const existingCadKeys = await buildExistingCadKeyIndex(config?.destination);
|
||||
const sourceMaxVersions = await buildZipHighestNumericVersionByCadKey(zipPath);
|
||||
const result = {
|
||||
scanned: 0,
|
||||
copied: 0,
|
||||
@@ -70,6 +73,8 @@ async function processZip(zipPath, config) {
|
||||
details: [],
|
||||
};
|
||||
|
||||
let current = 0;
|
||||
|
||||
for await (const entry of stream) {
|
||||
if (entry.type !== 'File') {
|
||||
entry.autodrain();
|
||||
@@ -78,7 +83,9 @@ async function processZip(zipPath, config) {
|
||||
|
||||
const file = entry.path;
|
||||
const baseName = path.basename(file);
|
||||
current += 1;
|
||||
result.scanned += 1;
|
||||
onProgress?.({ phase: 'copy', current, total, file: baseName });
|
||||
|
||||
const cadInfo = getCadInfo(baseName);
|
||||
if (!cadInfo) {
|
||||
|
||||
Reference in New Issue
Block a user