template-project/generate.js
2025-05-30 18:13:30 +08:00

54 lines
1.7 KiB
JavaScript

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);
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();