Skip to content

Commit 2e8109e

Browse files
author
Rodrigo Bermudez Schettino
committed
Improve logging after optimization
1 parent f41befc commit 2e8109e

File tree

4 files changed

+28
-17
lines changed

4 files changed

+28
-17
lines changed

changelog.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616

1717
## [Unreleased]
1818

19+
## [1.1.0] - 2019-04-24
20+
21+
### Added
22+
23+
- Log percentage difference instead of file size in bytes.
24+
25+
### Fixed
26+
27+
- Made error logging more robust.
28+
1929
## [1.0.1] - 2019-04-23
2030

2131
### Fixed
@@ -29,6 +39,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2939

3040
- Optimize file size of PDFs
3141

32-
[unreleased]: https://github.com/rodrigobdz/pdfoptim/compare/v1.0.1...HEAD
42+
[unreleased]: https://github.com/rodrigobdz/pdfoptim/compare/v1.1.0...HEAD
43+
[v1.0.1]: https://github.com/rodrigobdz/pdfoptim/compare/v1.0.1...v1.1.0
3344
[v1.0.1]: https://github.com/rodrigobdz/pdfoptim/compare/v1.0.0...v1.0.1
3445
[v1.0.0]: https://github.com/rodrigobdz/pdfoptim/compare/b30480643d7a74ff1f383674a0f48c735fe73faa...v1.0.0

index.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22
const fs = require('fs');
33
const path = require('path');
44
const {execSync} = require('child_process');
5+
const percentageDiff = require('percentage-diff');
56

67
/**
7-
* Calculate file size and return human readable representation
8+
* Get filesize in bytes
89
* @param {string} file Path to file
910
*
10-
* @returns {string} '<size> bytes'
11+
* @returns {string} Size in bytes
1112
*/
12-
function humanFileSize(file) {
13-
return `${fs.statSync(file).size} bytes`;
13+
function fileSizeInBytes(file) {
14+
return Number(fs.statSync(file).size);
1415
}
1516

1617
function logOptimizationResults(oldSize, file, outputFile) {
17-
const newSize = humanFileSize(outputFile);
18+
const newSize = fileSizeInBytes(outputFile);
19+
const diff = percentageDiff(oldSize, newSize);
1820

19-
console.log('File Size Comparison');
20-
console.log(`Original: ${oldSize}`);
21-
console.log(`Optimized: ${newSize}\n`);
21+
console.log(`Filesize difference ${percentageDiff.toPercentage(diff)} ✂️`);
2222
console.log(`Optimized version of ${path.basename(file)} saved as ${outputFile} 🎉`);
2323
}
2424

@@ -41,13 +41,15 @@ module.exports = (file, options = {}) => {
4141
return false;
4242
}
4343

44-
const oldSize = humanFileSize(file);
44+
const oldSize = fileSizeInBytes(file);
4545

4646
try {
4747
execSync(`\\gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=${options.outputFile} ${file}`);
4848
logOptimizationResults(oldSize, file, options.outputFile);
4949
} catch (error) {
50-
console.log(error.stdout.toString());
50+
const errMsg = error.stdout ? error.stdout : error;
51+
console.log(errMsg.toString());
52+
5153
return false;
5254
}
5355

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
],
2828
"dependencies": {
2929
"meow": "^5.0.0",
30+
"percentage-diff": "^1.1.0",
3031
"update-notifier": "^2.5.0"
3132
},
3233
"devDependencies": {

readme.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,8 @@ $ npm install pdfoptim
1414
const pdfoptim = require('pdfoptim');
1515

1616
pdfoptim('printer-manual.pdf');
17-
// File Size Comparison
18-
// Original: 3595210 bytes
19-
// Optimized: 738584 bytes
20-
21-
// Optimized version of printer-manual.pdf
22-
// saved as optimized-1556032400471.pdf 🎉
17+
// Filesize difference -79.46% ✂️
18+
// Optimized version of printer-manual.pdf saved as optimized-1556039930343.pdf 🎉
2319
```
2420

2521
## API
@@ -67,6 +63,7 @@ $ pdfoptim --help
6763
6864
## Credits
6965
66+
* [percentage-diff](https://github.com/rodrigobdz/percentage-diff) - Calculate percentage difference between two numbers
7067
* [generator-lnm](https://github.com/rodrigobdz/generator-lnm) - Awesome node module generator
7168
7269
## License

0 commit comments

Comments
 (0)