Skip to content

Commit

Permalink
Fix progress bar in convert mode
Browse files Browse the repository at this point in the history
  • Loading branch information
343dev committed Sep 26, 2024
1 parent 1bb84e4 commit b02519b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
21 changes: 10 additions & 11 deletions lib/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 28,32 @@ export default async function convert({ inputFilePaths, outputDirectoryPath, isL

log(`Converting ${inputFilePathsCount} ${getPlural(inputFilePathsCount, 'image', 'images')} (${isLossless ? 'lossless' : 'lossy'})...`);

const progressBarTotal = shouldConvertToAvif && shouldConvertToWebp
? inputFilePathsCount * 2
: inputFilePathsCount;
const progressBar = new CliProgress.SingleBar({
format: `{bar} {percentage}% | Processed {value} of {total} ${getPlural(inputFilePathsCount, 'image', 'images')}`,
format: `{bar} {percentage}% | Processed {value} of {total} ${getPlural(progressBarTotal, 'image', 'images')}`,
clearOnComplete: true,
stopOnComplete: true,
}, CliProgress.Presets.shades_classic);

progressBar.start(inputFilePathsCount, 0);

const simultaneousTasksLimit = pLimit(os.cpus().length);
const totalSize = { before: 0, after: 0 };

const avifConfig = isLossless
? config?.avif?.lossless
: config?.avif?.lossy;

const webpConfig = isLossless
? config?.webp?.lossless
: config?.webp?.lossy;

const webpGifConfig = isLossless
? config?.webpGif?.lossless
: config?.webpGif?.lossy;

const totalSize = { before: 0, after: 0 };

const tasksLogs = [];
const tasksSimultaneousLimit = pLimit(os.cpus().length);
const tasksPromises = inputFilePaths.reduce((accumulator, filePath) => {
if (shouldConvertToAvif) {
accumulator.push(
simultaneousTasksLimit(
tasksSimultaneousLimit(
() => processFile({
filePath,
outputDirectoryPath,
Expand All @@ -74,7 71,7 @@ export default async function convert({ inputFilePaths, outputDirectoryPath, isL

if (shouldConvertToWebp) {
accumulator.push(
simultaneousTasksLimit(
tasksSimultaneousLimit(
() => processFile({
filePath,
outputDirectoryPath,
Expand All @@ -96,7 93,9 @@ export default async function convert({ inputFilePaths, outputDirectoryPath, isL
return accumulator;
}, []);

progressBar.start(progressBarTotal, 0);
await Promise.all(tasksPromises);
progressBar.stop();

for (const message of tasksLogs) {
log(...message);
Expand Down
15 changes: 7 additions & 8 deletions lib/optimize.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 31,23 @@ export default async function optimize({ inputFilePaths, outputDirectoryPath, is
const progressBar = new CliProgress.SingleBar({
format: `{bar} {percentage}% | Processed {value} of {total} ${getPlural(inputFilePathsCount, 'image', 'images')}`,
clearOnComplete: true,
stopOnComplete: true,
}, CliProgress.Presets.shades_classic);

progressBar.start(inputFilePathsCount, 0);
const totalSize = { before: 0, after: 0 };

const cpuCount = os.cpus().length;
const simultaneousTasksLimit = pLimit(

const tasksLogs = [];
const tasksSimultaneousLimit = pLimit(
/*
Guetzli uses a large amount of memory and a significant amount of CPU time.
To reduce the processor load in lossless mode, we reduce the number
of simultaneous tasks by half.
*/
isLossless ? Math.round(cpuCount / 2) : cpuCount,
);

const totalSize = { before: 0, after: 0 };

const tasksLogs = [];
const tasksPromises = inputFilePaths.map(
filePath => simultaneousTasksLimit(
filePath => tasksSimultaneousLimit(
() => processFile({
filePath,
outputDirectoryPath,
Expand All @@ -63,7 60,9 @@ export default async function optimize({ inputFilePaths, outputDirectoryPath, is
),
);

progressBar.start(inputFilePathsCount, 0);
await Promise.all(tasksPromises);
progressBar.stop();

for (const message of tasksLogs) {
log(...message);
Expand Down

0 comments on commit b02519b

Please sign in to comment.