MCPcopy Create free account
hub / github.com/343dev/optimizt / optimize

Function optimize

optimize.js:28–72  ·  view source on GitHub ↗
({ filePaths, config })

Source from the content-addressed store, hash-verified

26import { showTotal } from './lib/show-total.js';
27
28export async function optimize({ filePaths, config }) {
29 const { isLossless } = programOptions;
30
31 const filePathsCount = filePaths.length;
32
33 if (filePathsCount <= 0) {
34 return;
35 }
36
37 log(`Optimizing ${filePathsCount} ${getPlural(filePathsCount, 'image', 'images')} (${isLossless ? 'lossless' : 'lossy'})...`);
38
39 const progressBarContainer = createProgressBarContainer(filePathsCount);
40 const progressBar = progressBarContainer.create(filePathsCount, 0);
41
42 const totalSize = { before: 0, after: 0 };
43
44 const cpuCount = os.cpus().length;
45 const tasksSimultaneousLimit = pLimit(cpuCount);
46 const guetzliTasksSimultaneousLimit = pLimit(1); // Guetzli uses a large amount of memory and a significant amount of CPU time. To reduce system load, we only allow one instance of guetzli to run at the same time.
47
48 await Promise.all(
49 filePaths.map((filePath) => {
50 const extension = path.extname(filePath.input).toLowerCase();
51 const isJpeg = extension === '.jpg' || extension === '.jpeg';
52
53 const limit = isJpeg && isLossless
54 ? guetzliTasksSimultaneousLimit
55 : tasksSimultaneousLimit;
56
57 return limit(() => processFile({
58 filePath,
59 config,
60 progressBarContainer,
61 progressBar,
62 totalSize,
63 isLossless,
64 }));
65 }),
66 );
67
68 progressBarContainer.update(); // Prevent logs lost. See: https://github.com/npkgz/cli-progress/issues/145#issuecomment-1859594159
69 progressBarContainer.stop();
70
71 showTotal(totalSize.before, totalSize.after);
72}
73
74async function processFile({
75 filePath,

Callers

nothing calls this directly

Calls 5

logFunction · 0.90
getPluralFunction · 0.90
showTotalFunction · 0.90
processFileFunction · 0.70

Tested by

no test coverage detected