({ filePaths, config })
| 26 | import { showTotal } from './lib/show-total.js'; |
| 27 | |
| 28 | export 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 | |
| 74 | async function processFile({ |
| 75 | filePath, |
nothing calls this directly
no test coverage detected