Skip to content

Commit

Permalink
Improve logging after optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodrigo Bermudez Schettino committed Apr 23, 2019
1 parent f41befc commit 2e8109e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
13 changes: 12 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
22 changes: 12 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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} '<size> 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} 🎉`);
}

Expand All @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
],
"dependencies": {
"meow": "^5.0.0",
"percentage-diff": "^1.1.0",
"update-notifier": "^2.5.0"
},
"devDependencies": {
Expand Down
9 changes: 3 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2e8109e

Please sign in to comment.