import axios from 'axios'; import * as fs from 'fs'; import * as path from 'path'; import { buildDatasheetModel } from './pdf/model/build-datasheet-model'; import type { ProductData } from './pdf/model/types'; const WC_URL = process.env.WOOCOMMERCE_URL; const WC_KEY = process.env.WOOCOMMERCE_CONSUMER_KEY; const WC_SECRET = process.env.WOOCOMMERCE_CONSUMER_SECRET; if (!WC_URL || !WC_KEY || !WC_SECRET) { console.error('Missing WooCommerce credentials in .env'); process.exit(1); } async function fetchAllProducts() { let page = 1; let allProducts: any[] = []; while (true) { console.log(`Fetching page ${page}...`); try { const response = await axios.get(`${WC_URL}/wp-json/wc/v3/products`, { params: { consumer_key: WC_KEY, consumer_secret: WC_SECRET, per_page: 100, page: page } }); const products = response.data; if (products.length === 0) break; allProducts = allProducts.concat(products); page++; } catch (error) { console.error('Error fetching products:', error); break; } } return allProducts; } function mapWcProductToProductData(wcProduct: any, locale: 'en' | 'de'): ProductData { // This mapping needs to be adjusted based on actual WC response structure // and how translations are handled (e.g. if they are separate products or same product with different fields) // Assuming standard WC response return { id: wcProduct.id, name: wcProduct.name, shortDescriptionHtml: wcProduct.short_description, descriptionHtml: wcProduct.description, images: wcProduct.images.map((img: any) => img.src), featuredImage: wcProduct.images[0]?.src || null, sku: wcProduct.sku, slug: wcProduct.slug, categories: wcProduct.categories.map((cat: any) => ({ name: cat.name })), attributes: wcProduct.attributes.map((attr: any) => ({ name: attr.name, options: attr.options })), locale: locale // This might need to be derived }; } function generateMdxContent(product: ProductData, technicalData: any, locale: 'en' | 'de') { const frontmatter = { title: product.name, sku: product.sku, description: product.shortDescriptionHtml.replace(/<[^>]*>/g, ''), // Simple strip tags categories: product.categories.map(c => c.name), images: product.images, locale: locale }; const technicalDataJson = JSON.stringify(technicalData, null, 2); return `--- ${JSON.stringify(frontmatter, null, 2)} --- # ${product.name} ${product.descriptionHtml} ## Technical Data `; } async function run() { const products = await fetchAllProducts(); console.log(`Fetched ${products.length} products.`); for (const product of products) { // Determine locale. WC might return 'lang' property if using plugins like Polylang // Or we might have to infer it. // For now, let's assume we can detect it or default to 'en'. // If the site uses Polylang, usually there is a 'lang' field. const locale = product.lang || 'en'; // Default to en if not found // We need to handle both en and de. // If the API returns mixed, we process them. // If the API only returns default lang, we might need to fetch translations specifically. // Let's try to generate for the detected locale. const productData = mapWcProductToProductData(product, locale as 'en' | 'de'); // Build datasheet model to get technical data // We need to try both locales if we are not sure, or just the one we have. // But buildDatasheetModel takes a locale. const model = buildDatasheetModel({ product: productData, locale: locale as 'en' | 'de' }); if (model.voltageTables.length > 0 || model.technicalItems.length > 0) { console.log(`Generated technical data for ${product.name} (${locale})`); } else { console.warn(`No technical data found for ${product.name} (${locale})`); } const mdx = generateMdxContent(productData, { technicalItems: model.technicalItems, voltageTables: model.voltageTables }, locale as 'en' | 'de'); const outDir = path.join(process.cwd(), 'data', 'products', locale); if (!fs.existsSync(outDir)) { fs.mkdirSync(outDir, { recursive: true }); } const filename = `${product.slug}.mdx`; fs.writeFileSync(path.join(outDir, filename), mdx); console.log(`Saved ${filename}`); } } run().catch(console.error);