-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
57 lines (47 loc) · 1.53 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'use strict';
const fs = require('fs');
const path = require('path');
const {execSync} = require('child_process');
const percentageDiff = require('percentage-diff');
/**
* Get filesize in bytes
* @param {string} file Path to file
*
* @returns {string} Size in bytes
*/
function fileSizeInBytes(file) {
return Number(fs.statSync(file).size);
}
function logOptimizationResults(oldSize, file, outputFile) {
const newSize = fileSizeInBytes(outputFile);
const diff = percentageDiff(oldSize, newSize);
console.log(`Filesize difference ${percentageDiff.toPercentage(diff)} ✂️`);
console.log(`Optimized version of ${path.basename(file)} saved as ${outputFile} 🎉`);
}
module.exports = (file, options = {}) => {
if (typeof options.outputFile !== 'string') {
throw new TypeError(`Expected a string, got ${typeof options.outputFile}`);
}
const ext = path.extname(file).toLowerCase();
if (ext !== '.pdf') {
throw new TypeError(`Expected a PDF, got ${ext || undefined}`);
}
try {
if (!fs.existsSync(file)) {
throw new ReferenceError(`File ${file} does not exist.`);
}
} catch (error) {
console.error(error);
return false;
}
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) {
const errMsg = error.stdout ? error.stdout : error;
console.log(errMsg.toString());
return false;
}
return true;
};