From 1723c9aa44058da5b2419cc2c59a4ed7bdf12b28 Mon Sep 17 00:00:00 2001 From: Jens Oliver Meiert Date: Wed, 9 Oct 2024 14:16:38 +0200 Subject: [PATCH] refactor: improve maintainability in file compression logic Removed unnecessary semicolon and added comment for better configurability. These small adjustments enhance code clarity and ease future updates. (This commit message was AI-generated.) Signed-off-by: Jens Oliver Meiert --- src/utils.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/utils.js b/src/utils.js index 45b38f0..4850c54 100644 --- a/src/utils.js +++ b/src/utils.js @@ -29,7 +29,7 @@ const compression = async (filename, dry) => { return 0 } - const maxFileSize = 100 * 1024 * 1024; // 100 MB + const maxFileSize = 100 * 1024 * 1024 // 100 MB if (fileSizeBefore > maxFileSize) { logMessage(`Skipped ${filename} (file too large: ${sizeReadable(fileSizeBefore)})`, dry) @@ -46,6 +46,7 @@ const compression = async (filename, dry) => { const outputFormat = ext === 'jpg' ? 'jpeg' : ext // sharp uses “jpeg” instead of “jpg” + // @@ Refactor for better maintainability and configurability if (outputFormat === 'png') { await sharp(filename) .png({ compressionLevel: 9, quality: 100 }) @@ -105,11 +106,15 @@ const compression = async (filename, dry) => { } return fileSizeAfter < fileSizeBefore ? fileSizeBefore - fileSizeAfter : 0 + } catch (error) { + console.error(chalk.red(`Error compressing ${filename}:`), error) await fs.promises.rename(filenameBackup, filename) return 0 + } finally { + try { await fs.promises.unlink(filenameBackup) } catch (error) {