import { defineNuxtModule, useLogger } from "@nuxt/kit"; import { copyFile, mkdir, readdir, readFile } from "fs/promises"; import { join } from "path"; import { createHash } from "crypto"; import { existsSync, watch } from "fs"; export default defineNuxtModule({ meta: { name: "content-assets", configKey: "contentAssets", }, defaults: {}, async setup(_, nuxt) { const logger = useLogger("content-assets"); const contentDir = join(nuxt.options.rootDir, "content"); const publicDir = join(nuxt.options.rootDir, "public/images/projects"); const getFileChecksum = async (filePath: string): Promise => { const content = await readFile(filePath); return createHash("sha256").update(content).digest("hex"); }; const copyWebpFiles = async () => { try { const projectsDir = join(contentDir, "projects"); if (!existsSync(projectsDir)) { logger.info("No projects directory found"); return; } const entries = await readdir(projectsDir, { withFileTypes: true }); for (const entry of entries) { if (!entry.isDirectory()) continue; const projectName = entry.name; const sourceDir = join(projectsDir, projectName); const destDir = join(publicDir, projectName); if (!existsSync(destDir)) { await mkdir(destDir, { recursive: true }); } const files = await readdir(sourceDir); for (const file of files) { if (!file.endsWith(".webp")) continue; const sourcePath = join(sourceDir, file); const destPath = join(destDir, file); // only copy if destination doesn't exist, or if not the same as source let shouldCopy = true; if (existsSync(destPath)) { const sourceChecksum = await getFileChecksum(sourcePath); const destChecksum = await getFileChecksum(destPath); if (sourceChecksum === destChecksum) { shouldCopy = false; } } if (shouldCopy) { await copyFile(sourcePath, destPath); logger.success(`Copied: ${projectName}/${file}`); } } } } catch (error) { logger.error("Error copying files:", error); } }; nuxt.hook("build:before", async () => { await copyWebpFiles(); }); if (nuxt.options.dev) { nuxt.hook("ready", () => { watch(contentDir, { recursive: true }, async (_, filename) => { if (filename?.endsWith(".webp")) { logger.info(`Detected change: ${filename}`); await copyWebpFiles(); } }); }); } }, });