From 2e8109ef904a2f00e3696ca0d639f2c152e9324d Mon Sep 17 00:00:00 2001 From: Rodrigo Bermudez Schettino Date: Wed, 24 Apr 2019 01:23:25 +0800 Subject: [PATCH] Improve logging after optimization --- changelog.md | 13 ++++++++++++- index.js | 22 ++++++++++++---------- package.json | 1 + readme.md | 9 +++------ 4 files changed, 28 insertions(+), 17 deletions(-) diff --git a/changelog.md b/changelog.md index bb3576c..898918c 100644 --- a/changelog.md +++ b/changelog.md @@ -16,6 +16,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [1.1.0] - 2019-04-24 + +### Added + +- Log percentage difference instead of file size in bytes. + +### Fixed + +- Made error logging more robust. + ## [1.0.1] - 2019-04-23 ### Fixed @@ -29,6 +39,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Optimize file size of PDFs -[unreleased]: https://github.com/rodrigobdz/pdfoptim/compare/v1.0.1...HEAD +[unreleased]: https://github.com/rodrigobdz/pdfoptim/compare/v1.1.0...HEAD +[v1.0.1]: https://github.com/rodrigobdz/pdfoptim/compare/v1.0.1...v1.1.0 [v1.0.1]: https://github.com/rodrigobdz/pdfoptim/compare/v1.0.0...v1.0.1 [v1.0.0]: https://github.com/rodrigobdz/pdfoptim/compare/b30480643d7a74ff1f383674a0f48c735fe73faa...v1.0.0 diff --git a/index.js b/index.js index b8b5eaf..86028e5 100644 --- a/index.js +++ b/index.js @@ -2,23 +2,23 @@ const fs = require('fs'); const path = require('path'); const {execSync} = require('child_process'); +const percentageDiff = require('percentage-diff'); /** - * Calculate file size and return human readable representation + * Get filesize in bytes * @param {string} file Path to file * - * @returns {string} ' bytes' + * @returns {string} Size in bytes */ -function humanFileSize(file) { - return `${fs.statSync(file).size} bytes`; +function fileSizeInBytes(file) { + return Number(fs.statSync(file).size); } function logOptimizationResults(oldSize, file, outputFile) { - const newSize = humanFileSize(outputFile); + const newSize = fileSizeInBytes(outputFile); + const diff = percentageDiff(oldSize, newSize); - console.log('File Size Comparison'); - console.log(`Original: ${oldSize}`); - console.log(`Optimized: ${newSize}\n`); + console.log(`Filesize difference ${percentageDiff.toPercentage(diff)} ✂️`); console.log(`Optimized version of ${path.basename(file)} saved as ${outputFile} 🎉`); } @@ -41,13 +41,15 @@ module.exports = (file, options = {}) => { return false; } - const oldSize = humanFileSize(file); + const oldSize = fileSizeInBytes(file); try { execSync(`\\gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=${options.outputFile} ${file}`); logOptimizationResults(oldSize, file, options.outputFile); } catch (error) { - console.log(error.stdout.toString()); + const errMsg = error.stdout ? error.stdout : error; + console.log(errMsg.toString()); + return false; } diff --git a/package.json b/package.json index b1e3e82..be72f09 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ ], "dependencies": { "meow": "^5.0.0", + "percentage-diff": "^1.1.0", "update-notifier": "^2.5.0" }, "devDependencies": { diff --git a/readme.md b/readme.md index 535bd67..4b44a95 100644 --- a/readme.md +++ b/readme.md @@ -14,12 +14,8 @@ $ npm install pdfoptim const pdfoptim = require('pdfoptim'); pdfoptim('printer-manual.pdf'); -// File Size Comparison -// Original: 3595210 bytes -// Optimized: 738584 bytes - -// Optimized version of printer-manual.pdf -// saved as optimized-1556032400471.pdf 🎉 +// Filesize difference -79.46% ✂️ +// Optimized version of printer-manual.pdf saved as optimized-1556039930343.pdf 🎉 ``` ## API @@ -67,6 +63,7 @@ $ pdfoptim --help ## Credits +* [percentage-diff](https://github.com/rodrigobdz/percentage-diff) - Calculate percentage difference between two numbers * [generator-lnm](https://github.com/rodrigobdz/generator-lnm) - Awesome node module generator ## License