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

Function convert

convert.js:25–99  ·  view source on GitHub ↗
({ filePaths, config })

Source from the content-addressed store, hash-verified

23import { showTotal } from './lib/show-total.js';
24
25export async function convert({ filePaths, config }) {
26 const {
27 isForced,
28 isLossless,
29 shouldConvertToAvif,
30 shouldConvertToWebp,
31 } = programOptions;
32
33 const filePathsCount = filePaths.length;
34
35 if (!filePathsCount) {
36 return;
37 }
38
39 log(`Converting ${filePathsCount} ${getPlural(filePathsCount, 'image', 'images')} (${isLossless ? 'lossless' : 'lossy'})...`);
40
41 const progressBarTotal = shouldConvertToAvif && shouldConvertToWebp
42 ? filePathsCount * 2
43 : filePathsCount;
44 const progressBarContainer = createProgressBarContainer(progressBarTotal);
45 const progressBar = progressBarContainer.create(progressBarTotal, 0);
46
47 const totalSize = { before: 0, after: 0 };
48
49 const getConfig = format => config?.[format]?.[isLossless ? 'lossless' : 'lossy'];
50
51 const avifConfig = getConfig('avif');
52 const webpConfig = getConfig('webp');
53
54 const cpuCount = os.cpus().length;
55 const tasksSimultaneousLimit = pLimit(cpuCount);
56
57 const tasks = [];
58 for (const filePath of filePaths) {
59 if (shouldConvertToAvif) {
60 tasks.push(
61 tasksSimultaneousLimit(
62 () => processFile({
63 filePath,
64 config: avifConfig || {},
65 progressBarContainer,
66 progressBar,
67 totalSize,
68 isForced,
69 format: 'AVIF',
70 processFunction: processAvif,
71 }),
72 ),
73 );
74 }
75
76 if (shouldConvertToWebp) {
77 tasks.push(
78 tasksSimultaneousLimit(
79 () => processFile({
80 filePath,
81 config: webpConfig || {},
82 progressBarContainer,

Callers

nothing calls this directly

Calls 6

logFunction · 0.90
getPluralFunction · 0.90
showTotalFunction · 0.90
getConfigFunction · 0.85
processFileFunction · 0.70

Tested by

no test coverage detected