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

65 lines
2.0 KiB
JavaScript

import fs from "fs/promises";
import path from "path";
import { fileURLToPath } from "url";
import { 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-backup");
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 templateFolderPath = path.join(workFolderPath, templateFolderName);
const folders = await fs.readdir(templateFolderPath, {
withFileTypes: true,
});
const caseFolders = folders
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name);
for (const folderName of caseFolders) {
const casePath = path.join(templateFolderPath, folderName);
const configPath = path.join(casePath, "config.json");
console.log(`Processing: ${folderName}`);
// 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,
folderName,
);
console.log(`✅ Finished processing ${folderName}\n`);
}
}
await exiftool.end();
} catch (err) {
console.error("Error:", err);
}
}
main();