import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; import { select } from '@evershop/postgres-query-builder'; import { info, success, error } from '../../lib/log/logger.js'; import { pool } from '../../lib/postgres/connection.js'; import createCategory from '../../modules/catalog/services/category/createCategory.js'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); /** * Seed categories from JSON file */ export async function seedCategories(): Promise { info('Seeding categories...'); const dataPath = path.join(__dirname, 'data', 'categories.json'); const categoriesData = JSON.parse(fs.readFileSync(dataPath, 'utf-8')); for (const categoryData of categoriesData) { try { // Check if category already exists const existingCategory = await select() .from('category_description') .where('url_key', '=', categoryData.url_key) .load(pool); if (existingCategory) { info(`Category "${categoryData.name}" already exists, skipping...`); continue; } await createCategory(categoryData, {}); success(`✓ Created category: ${categoryData.name}`); } catch (e: any) { error(`Failed to create category ${categoryData.name}: ${e.message}`); } } }