const fs = require("fs/promises"); const path = require("path"); const fileURLToPath = require("url"); const {compressImages} = require("./compress-jimp.cjs"); const {watermarkAllImages} = require("./watermark-canvas.cjs"); const {generateDocxFromTemplate} = require("./template.cjs"); const exiftool = require("exiftool-vendored"); // import fs from "fs/promises"; // import path from "path"; // import { fileURLToPath } from "url"; // import { bufferImages, compressImages } from "./compress.js"; // import { watermarkAllImages } from "./watermark.js"; // import { generateDocxFromTemplate } from "./template.js"; // import { exiftool } from "exiftool-vendored"; // Get __dirname in ES module scope // const __filename = fileURLToPath(import.meta.url); // const __dirname = path.dirname(__filename); (e) => { const mod_path = require.resolve(e, { paths: [ process.cwd(), `${process.cwd()}/node_modules` ] }); return require(e); } async function main() { try { const workFolderPath = path.join(__dirname, "Work"); const folders = await fs.readdir(workFolderPath, { withFileTypes: true }); const templateFolders = folders .filter((dirent) => dirent.isDirectory()) .map((dirent) => dirent.name); console.log("Found template folders:", templateFolders); for (const templateFolderName of templateFolders) { console.log(`Processing: ${templateFolderName}`); const orderNo = templateFolderName.split('_')[1]; const casePath = path.join(workFolderPath, templateFolderName); const configPath = path.join(casePath, "config.txt"); // Step 1: Compress images inside this folder const compressedBuffers = await compressImages(casePath); // Step 2: Apply watermark const watermarkedBuffers = await watermarkAllImages( compressedBuffers, casePath, ); // Step 3: Generate DOCX await generateDocxFromTemplate( configPath, watermarkedBuffers, templateFolderName, orderNo ); console.log(`✅ Finished processing ${templateFolderName}\n`); } await exiftool.end(); } catch (err) { console.error("Error:", err); } } main();