Rocky_Mountain_Vending/scripts/sync-thumbnails-to-public.js

82 lines
2.6 KiB
JavaScript

/**
* Script to sync thumbnails from /thumbnails to /public/thumbnails
* This allows static file serving for GHL hosting or static deployments
*/
import { readdir, copyFile, mkdir, stat } from "fs/promises"
import { join } from "path"
import { existsSync } from "fs"
const PROJECT_ROOT = join(process.cwd(), "..")
const THUMBNAILS_SOURCE = join(PROJECT_ROOT, "thumbnails")
const THUMBNAILS_PUBLIC = join(process.cwd(), "public", "thumbnails")
async function syncThumbnails() {
try {
// Check if source directory exists
if (!existsSync(THUMBNAILS_SOURCE)) {
console.log("⚠️ Thumbnails source directory not found, skipping...")
return
}
// Create public/thumbnails directory structure
if (!existsSync(THUMBNAILS_PUBLIC)) {
await mkdir(THUMBNAILS_PUBLIC, { recursive: true })
}
/**
* Recursively copy thumbnails, preserving directory structure
*/
async function copyDirectory(sourceDir, destDir, relativePath = "") {
const entries = await readdir(sourceDir, { withFileTypes: true })
for (const entry of entries) {
const sourcePath = join(sourceDir, entry.name)
const destPath = join(destDir, entry.name)
const entryRelativePath = relativePath
? join(relativePath, entry.name)
: entry.name
if (entry.isDirectory()) {
// Create destination directory
if (!existsSync(destPath)) {
await mkdir(destPath, { recursive: true })
}
// Recursively copy subdirectories
await copyDirectory(sourcePath, destPath, entryRelativePath)
} else if (entry.isFile()) {
// Copy image files (jpg, jpeg, png, webp)
const ext = entry.name.toLowerCase()
if (
ext.endsWith(".jpg") ||
ext.endsWith(".jpeg") ||
ext.endsWith(".png") ||
ext.endsWith(".webp")
) {
// Check if file needs updating (compare modification times)
let shouldCopy = true
if (existsSync(destPath)) {
const sourceStats = await stat(sourcePath)
const destStats = await stat(destPath)
shouldCopy = sourceStats.mtime > destStats.mtime
}
if (shouldCopy) {
await copyFile(sourcePath, destPath)
console.log(`Copied: ${entryRelativePath}`)
}
}
}
}
}
await copyDirectory(THUMBNAILS_SOURCE, THUMBNAILS_PUBLIC)
console.log("✅ Thumbnails synced to public folder successfully!")
} catch (error) {
console.error("❌ Error syncing thumbnails:", error)
process.exit(1)
}
}
syncThumbnails()