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

73 lines
2.4 KiB
JavaScript

/**
* Script to sync manuals from /manuals to /public/manuals
* This allows static file serving as a backup to the API route
* Useful 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 MANUALS_DATA_ROOT =
process.env.MANUALS_DATA_ROOT ||
(existsSync(join(PROJECT_ROOT, "manuals-data"))
? join(PROJECT_ROOT, "manuals-data")
: "/Users/matthewcall/Documents/VS Code Projects/Rocky Mountain Vending/manuals-data")
const MANUALS_SOURCE = join(MANUALS_DATA_ROOT, "manuals")
const MANUALS_PUBLIC = join(process.cwd(), "public", "manuals")
async function syncManuals() {
try {
// Create public/manuals directory structure
if (!existsSync(MANUALS_PUBLIC)) {
await mkdir(MANUALS_PUBLIC, { recursive: true })
}
// Get all manufacturer directories
const manufacturers = await readdir(MANUALS_SOURCE, { withFileTypes: true })
for (const manufacturerDir of manufacturers) {
if (!manufacturerDir.isDirectory()) continue
const manufacturer = manufacturerDir.name
const sourcePath = join(MANUALS_SOURCE, manufacturer)
const destPath = join(MANUALS_PUBLIC, manufacturer)
// Create manufacturer directory in public
if (!existsSync(destPath)) {
await mkdir(destPath, { recursive: true })
}
// Get all PDF files
const files = await readdir(sourcePath, { withFileTypes: true })
for (const file of files) {
if (file.isFile() && file.name.toLowerCase().endsWith(".pdf")) {
const sourceFile = join(sourcePath, file.name)
const destFile = join(destPath, file.name)
// Check if file needs updating (compare modification times)
let shouldCopy = true
if (existsSync(destFile)) {
const sourceStats = await stat(sourceFile)
const destStats = await stat(destFile)
shouldCopy = sourceStats.mtime > destStats.mtime
}
if (shouldCopy) {
await copyFile(sourceFile, destFile)
console.log(`Copied: ${manufacturer}/${file.name}`)
}
}
}
}
console.log("✅ Manuals synced to public folder successfully!")
} catch (error) {
console.error("❌ Error syncing manuals:", error)
process.exit(1)
}
}
syncManuals()