From 6c0c45e92bfa14e23bffb4fac7abdaebede46079 Mon Sep 17 00:00:00 2001 From: Salah Benmoussati <51402489+sbenmoussati@users.noreply.github.com> Date: Wed, 14 Feb 2024 11:50:00 +0100 Subject: [PATCH] SDA-4474 Logs zip file creation should open file explorer on completion (#2091) --- src/app/reports-handler.ts | 77 +++++++++++++++----------------------- 1 file changed, 31 insertions(+), 46 deletions(-) diff --git a/src/app/reports-handler.ts b/src/app/reports-handler.ts index f9ef1f59..2119134f 100644 --- a/src/app/reports-handler.ts +++ b/src/app/reports-handler.ts @@ -24,56 +24,41 @@ const generateArchiveForDirectory = ( fileExtensions: string[], retrievedLogs: ILogs[], ): Promise => { - return new Promise((resolve, _reject) => { - logger.info(`reports-handler: generating archive for directory ${source}`); - const output = fs.createWriteStream(destination); - const archive = new zip(); - const filesForCleanup: string[] = []; - - output.on('close', () => { - for (const file of filesForCleanup) { - if (fs.existsSync(file)) { - fs.unlinkSync(file); - } + logger.info(`reports-handler: generating archive for directory ${source}`); + const archive = new zip(); + const filesForCleanup: string[] = []; + const files = fs.readdirSync(source); + files + .filter((file) => fileExtensions.indexOf(path.extname(file)) !== -1) + .forEach((file) => { + switch (path.extname(file)) { + case '.log': + archive.addLocalFile(source + '/' + file, 'logs'); + break; + case '.dmp': + case '.txt': // on Windows .txt files will be created as part of crash dump + archive.addLocalFile(source + '/' + file, 'crashes'); + break; + default: + break; } - logger.info(`reports-handler: generated archive for directory ${source}`); - return resolve(); }); - const files = fs.readdirSync(source); - files - .filter((file) => fileExtensions.indexOf(path.extname(file)) !== -1) - .forEach((file) => { - switch (path.extname(file)) { - case '.log': - archive.addLocalFile(source + '/' + file, 'logs'); - break; - case '.dmp': - case '.txt': // on Windows .txt files will be created as part of crash dump - archive.addLocalFile(source + '/' + file, 'crashes'); - break; - default: - break; - } - }); - - for (const logs of retrievedLogs) { - for (const logFile of logs.logFiles) { - const file = path.join(source, logFile.filename); - archive.addLocalFile(file, 'logs'); - filesForCleanup.push(file); - } + for (const logs of retrievedLogs) { + for (const logFile of logs.logFiles) { + const file = path.join(source, logFile.filename); + archive.addLocalFile(file, 'logs'); + filesForCleanup.push(file); } - archive - .writeZipPromise(destination) - .then(() => { - logger.info('reports-handler: successfully created archive'); - }) - .catch((error) => { - logger.error('reports-handler: error while archiving ', error); - }); - archive.writeZip(destination); - }); + } + return archive + .writeZipPromise(destination) + .then(() => { + logger.info('reports-handler: successfully created archive'); + }) + .catch((error) => { + logger.error('reports-handler: error while archiving ', error); + }); }; let logWebContents: WebContents;