-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert-images.cjs
31 lines (24 loc) · 951 Bytes
/
convert-images.cjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const fs = require('fs').promises;
const path = require('path');
const sharp = require('sharp');
const publicDir = path.join(__dirname, 'public');
async function convertJpgToWebp(directory) {
try {
const entries = await fs.readdir(directory, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(directory, entry.name);
if (entry.isDirectory()) {
await convertJpgToWebp(fullPath);
} else if (entry.isFile() && path.extname(entry.name).toLowerCase() === '.jpg') {
const webpName = path.join(directory, `${path.parse(entry.name).name}.webp`);
await sharp(fullPath).webp().toFile(webpName);
console.log(`Converted: ${fullPath} -> ${webpName}`);
}
}
} catch (error) {
console.error(`Error processing directory ${directory}:`, error);
}
}
convertJpgToWebp(publicDir)
.then(() => console.log('Conversion complete'))
.catch((error) => console.error('Conversion failed:', error));