Skip to content

Commit

Permalink
minor improvements
Browse files Browse the repository at this point in the history
- png dithering enabled
- png quality preset updated
- stats improved
  • Loading branch information
Aslan Baryshnikov committed Apr 26, 2022
1 parent 59b0d5b commit 07f7788
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
6 changes: 5 additions & 1 deletion helpers/convertBytes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ const convertBytes = (bytes) => {

if (bytes === 0) return 'n/a';

const isNegative = bytes < 0;
// eslint-disable-next-line no-param-reassign
if (isNegative) bytes *= -1;

const i = parseInt(String(Math.floor(Math.log(bytes) / Math.log(1024))), 10);

if (i === 0) return `${bytes} ${sizes[i]}`;

return `${(bytes / 1024 ** i).toFixed(1)} ${sizes[i]}`;
return `${isNegative ? '-' : ''}${(bytes / 1024 ** i).toFixed(1)} ${sizes[i]}`;
};

module.exports = convertBytes;
12 changes: 9 additions & 3 deletions helpers/moveFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ const fs = require('fs');
const path = require('path');

const moveFile = (from, to) => {
const dir = path.dirname(to);
fs.mkdirSync(dir, { recursive: true });
fs.renameSync(from, to);
if (path.extname(to)) {
const dir = path.dirname(to);
fs.mkdirSync(dir, { recursive: true });
fs.renameSync(from, to);
} else {
const basename = path.basename(from);
fs.mkdirSync(to, { recursive: true });
fs.renameSync(from, path.join(to, basename));
}
};

module.exports = moveFile;
25 changes: 16 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ const imageOptimist = async (config) => {
imageminPngquant({
speed: 4,
strip: true,
quality: [0.8, 1],
dithering: false,
verbose: true,
quality: [0.9, 1],
dithering: 1,
}),
imageminSvgo(),
imageminGifsicle({
Expand All @@ -62,9 +61,10 @@ const imageOptimist = async (config) => {
return;
}

const initialDirSize = getTotalSize(c.sourcePath);

let tmpDir;
let initialDirSize = c.smartMode ? 0 : getTotalSize(c.sourcePath);
let finalDirSize = 0;
let conflictCount = 0;
try {
const inputDirectories = getAllDirectories(c.sourcePath);

Expand Down Expand Up @@ -125,12 +125,15 @@ const imageOptimist = async (config) => {
// eslint-disable-next-line no-nested-ternary
if (candidateIsExists ? tmpFileSize < candidateSize : true) {
moveFile(tmpFile, candidate);
if (sourceFileIsExists) initialDirSize += sourceFileSize;
finalDirSize += tmpFileSize;
if (candidateIsExists) console.log(`Updated: ${candidateRelative}`);
else console.log(`Created: ${candidateRelative}`);
} else {
console.log(`Not updated (already has optimal size): ${candidateRelative}`);
}
} else {
conflictCount += 1;
console.warn(`Conflict: ${sourceFile} and ${candidate}\nThe source file and the file in the`
+ ' destination folder are not the same. You should replace an'
+ ' obsolete file with a more recent one to avoid erroneous overwriting.'
Expand All @@ -143,12 +146,16 @@ const imageOptimist = async (config) => {
}
}

const finalDirSize = getTotalSize(c.destinationPath);
if (!c.smartMode) finalDirSize = getTotalSize(c.destinationPath);
const diffDirSize = initialDirSize - finalDirSize;

console.log(`Initial: ${convertBytes(initialDirSize)}`);
console.log(`Final${c.webp ? ' + WebP' : ''}: ${convertBytes(finalDirSize)}`);
console.log(`Was available for processing: ${convertBytes(initialDirSize)}`);
console.log(`After processing: ${convertBytes(finalDirSize)}`);
console.log(`Diff: ${convertBytes(diffDirSize)}`);
if (c.smartMode) {
console.log(`Source size: ${convertBytes(getTotalSize(c.sourcePath))}`);
console.log(`Destination size: ${convertBytes(getTotalSize(c.destinationPath))}`);
}
if (conflictCount > 0) console.log(`Conflicts: ${conflictCount}`);
console.timeEnd(PROCESSING_TIME);
console.log(`${PACKAGE_NAME} finished`);
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "image-optimist",
"version": "1.0.15",
"version": "1.0.16",
"description": "A utility for processing and converting images to the requirements of the modern web.",
"private": false,
"main": "index.js",
Expand Down

0 comments on commit 07f7788

Please sign in to comment.