({ filePaths, config })
| 23 | import { showTotal } from './lib/show-total.js'; |
| 24 | |
| 25 | export 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, |
nothing calls this directly
no test coverage detected