systems-obscure/scripts/process-blog-imgs.js

56 lines
1.2 KiB
JavaScript

import fs from "fs"
import path from "path"
import sharp from "sharp"
const processBlogImages = async () => {
const srcDir = "posts/img"
const destDir = "public/posts/img"
if (!fs.existsSync(srcDir)) {
console.error("⚠️ No posts/img directory found")
return
}
if (!fs.existsSync(destDir)) {
fs.mkdirSync(destDir, { recursive: true })
}
const files = fs.readdirSync(srcDir)
const imageExtensions = [
".jpg",
".jpeg",
".png",
".gif",
".webp",
".svg",
".webm",
]
for (const file of files) {
const ext = path.extname(file).toLowerCase()
if (imageExtensions.includes(ext)) {
const inputPath = path.join(srcDir, file) // Define inputPath and outputPath
const outputPath = path.join(destDir, file)
if (ext === ".svg" || ext === ".webm") {
fs.copyFileSync(inputPath, outputPath)
console.info(`📸 Copied ${file}`)
} else {
await sharp(inputPath)
.resize(1200, 800, {
fit: "inside",
withoutEnlargement: true,
})
.jpeg({ quality: 85, progressive: true })
.png({ compressionLevel: 8 })
.webp({ quality: 85 })
.toFile(outputPath)
console.info(`🖼️ Processed ${file}`)
}
}
}
console.log(`✅ Processed images from ${srcDir}`)
}
export { processBlogImages }