format
This commit is contained in:
@@ -17,59 +17,54 @@
|
||||
* Usage: tsx website/scripts/generate-placeholders.ts
|
||||
*/
|
||||
|
||||
import sharp from "sharp";
|
||||
import path from "node:path";
|
||||
import fs from "node:fs";
|
||||
import sharp from 'sharp'
|
||||
import path from 'node:path'
|
||||
import fs from 'node:fs'
|
||||
|
||||
const PUBLIC_DIR = path.resolve(import.meta.dirname, "../public");
|
||||
const OUTPUT_DIR = path.resolve(import.meta.dirname, "../src/assets/placeholders");
|
||||
const PLACEHOLDER_PREFIX = "placeholder-";
|
||||
const PLACEHOLDER_WIDTH = 64;
|
||||
const IMAGE_EXTENSIONS = new Set([".png", ".jpg", ".jpeg", ".webp"]);
|
||||
const PUBLIC_DIR = path.resolve(import.meta.dirname, '../public')
|
||||
const OUTPUT_DIR = path.resolve(import.meta.dirname, '../src/assets/placeholders')
|
||||
const PLACEHOLDER_PREFIX = 'placeholder-'
|
||||
const PLACEHOLDER_WIDTH = 64
|
||||
const IMAGE_EXTENSIONS = new Set(['.png', '.jpg', '.jpeg', '.webp'])
|
||||
|
||||
async function main() {
|
||||
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
|
||||
fs.mkdirSync(OUTPUT_DIR, { recursive: true })
|
||||
|
||||
const entries = fs.readdirSync(PUBLIC_DIR);
|
||||
const entries = fs.readdirSync(PUBLIC_DIR)
|
||||
|
||||
const images = entries.filter((name) => {
|
||||
if (name.startsWith(PLACEHOLDER_PREFIX)) {
|
||||
return false;
|
||||
return false
|
||||
}
|
||||
const ext = path.extname(name).toLowerCase();
|
||||
return IMAGE_EXTENSIONS.has(ext);
|
||||
});
|
||||
const ext = path.extname(name).toLowerCase()
|
||||
return IMAGE_EXTENSIONS.has(ext)
|
||||
})
|
||||
|
||||
if (images.length === 0) {
|
||||
console.error("No images found in public/");
|
||||
return;
|
||||
console.error('No images found in public/')
|
||||
return
|
||||
}
|
||||
|
||||
for (const name of images) {
|
||||
const inputPath = path.join(PUBLIC_DIR, name);
|
||||
const ext = path.extname(name);
|
||||
const base = path.basename(name, ext);
|
||||
const outputPath = path.join(OUTPUT_DIR, `${PLACEHOLDER_PREFIX}${base}${ext}`);
|
||||
const inputPath = path.join(PUBLIC_DIR, name)
|
||||
const ext = path.extname(name)
|
||||
const base = path.basename(name, ext)
|
||||
const outputPath = path.join(OUTPUT_DIR, `${PLACEHOLDER_PREFIX}${base}${ext}`)
|
||||
|
||||
// Skip if placeholder already exists and is newer than source
|
||||
if (fs.existsSync(outputPath)) {
|
||||
const srcMtime = fs.statSync(inputPath).mtimeMs;
|
||||
const outMtime = fs.statSync(outputPath).mtimeMs;
|
||||
const srcMtime = fs.statSync(inputPath).mtimeMs
|
||||
const outMtime = fs.statSync(outputPath).mtimeMs
|
||||
if (outMtime > srcMtime) {
|
||||
continue;
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
await sharp(inputPath)
|
||||
.resize(PLACEHOLDER_WIDTH)
|
||||
.png({ compressionLevel: 9 })
|
||||
.toFile(outputPath);
|
||||
await sharp(inputPath).resize(PLACEHOLDER_WIDTH).png({ compressionLevel: 9 }).toFile(outputPath)
|
||||
|
||||
const stats = fs.statSync(outputPath);
|
||||
console.error(
|
||||
`Generated ${PLACEHOLDER_PREFIX}${base}${ext} (${PLACEHOLDER_WIDTH}px wide, ${stats.size} bytes)`,
|
||||
);
|
||||
const stats = fs.statSync(outputPath)
|
||||
console.error(`Generated ${PLACEHOLDER_PREFIX}${base}${ext} (${PLACEHOLDER_WIDTH}px wide, ${stats.size} bytes)`)
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user