#!/usr/bin/env python3 """ Create frontend JavaScript file for base configuration. """ import sys import os import argparse import re from pathlib import Path # Add parent directory to path sys.path.insert(0, str(Path(__file__).parent.parent)) def create_base_frontend_js(tenant_id: str, api_port: int = 6002, output_file: str = "frontend/static/js/app_base.js"): """ Create frontend JavaScript file for base configuration. Args: tenant_id: Tenant ID api_port: API port output_file: Output file path """ # Read original app.js original_file = Path(__file__).parent.parent / "frontend/static/js/app.js" if not original_file.exists(): print(f"ERROR: Original frontend file not found: {original_file}") return 1 with open(original_file, 'r', encoding='utf-8') as f: content = f.read() # Replace API_BASE_URL api_url = f"http://localhost:{api_port}" content = content.replace( "const API_BASE_URL = 'http://120.76.41.98:6002';", f"const API_BASE_URL = '{api_url}';" ) # Add tenant_id constant at the beginning content = content.replace( "const API_BASE_URL =", f"const TENANT_ID = '{tenant_id}';\nconst API_BASE_URL =" ) # Update facets for base configuration base_facets = ''' const facets = [ { "field": "category_keyword", "size": 15, "type": "terms" }, { "field": "vendor_keyword", "size": 15, "type": "terms" }, { "field": "tags_keyword", "size": 10, "type": "terms" }, { "field": "min_price", "type": "range", "ranges": [ {"key": "0-50", "to": 50}, {"key": "50-100", "from": 50, "to": 100}, {"key": "100-200", "from": 100, "to": 200}, {"key": "200+", "from": 200} ] } ];''' # Find and replace facets definition (multiline match) facets_pattern = r'const facets = \[.*?\];' content = re.sub(facets_pattern, base_facets, content, flags=re.DOTALL) # Update fetch to include tenant_id header content = content.replace( "headers: {\n 'Content-Type': 'application/json',\n },", f"headers: {{\n 'Content-Type': 'application/json',\n 'X-Tenant-ID': TENANT_ID,\n }}," ) # Replace hits with results throughout content = re.sub(r'\bdata\.hits\b', 'data.results', content) content = re.sub(r'!data\.hits', '!data.results', content) # Replace hit loop with product loop content = re.sub( r'data\.hits\.forEach\(\(hit\) => \{', 'data.results.forEach((product) => {', content ) # Remove source extraction lines content = re.sub(r'const source = hit\._source;\s*\n', '', content) content = re.sub(r'const score = hit\._custom_score \|\| hit\._score;\s*\n', 'const score = product.relevance_score;\n', content) # Replace all source. references with product. content = re.sub(r'\bsource\.', 'product.', content) # Replace specific field names for base configuration # imageUrl -> image_url content = re.sub(r'product\.imageUrl', 'product.image_url', content) # name -> title content = re.sub(r'product\.name', 'product.title', content) content = re.sub(r'product\.enSpuName', 'product.title', content) # categoryName -> category content = re.sub(r'product\.categoryName', 'product.category', content) # brandName -> vendor content = re.sub(r'product\.brandName', 'product.vendor', content) # price -> price (already correct) # Remove moq and quantity fields (not in base config) content = re.sub(r'