|
| 1 | +"""Build logic for frontend assets.""" |
| 2 | + |
| 3 | +import subprocess |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | + |
| 7 | +class BuildError(Exception): |
| 8 | + """Raised when build fails.""" |
| 9 | + |
| 10 | + |
| 11 | +def ensure_npm_dependencies_installed(timeout: int = 300) -> None: |
| 12 | + """ |
| 13 | + Ensure npm dependencies are installed. |
| 14 | +
|
| 15 | + Args: |
| 16 | + timeout: Timeout in seconds for npm install |
| 17 | +
|
| 18 | + Raises: |
| 19 | + BuildError: If npm install fails or times out |
| 20 | + """ |
| 21 | + node_modules = Path("node_modules") |
| 22 | + if node_modules.exists(): |
| 23 | + return |
| 24 | + |
| 25 | + try: |
| 26 | + subprocess.run( |
| 27 | + ["npm", "install"], |
| 28 | + check=True, |
| 29 | + capture_output=True, |
| 30 | + text=True, |
| 31 | + timeout=timeout, |
| 32 | + ) |
| 33 | + except subprocess.TimeoutExpired as e: |
| 34 | + raise BuildError( |
| 35 | + f"npm install timed out after {timeout} seconds. " |
| 36 | + "Please check your network connection and try again." |
| 37 | + ) from e |
| 38 | + except subprocess.CalledProcessError as e: |
| 39 | + raise BuildError(f"Failed to install dependencies: {e.stderr}") from e |
| 40 | + except FileNotFoundError as e: |
| 41 | + raise BuildError("npm not found. Please install Node.js to build the frontend.") from e |
| 42 | + |
| 43 | + |
| 44 | +def build_frontend(timeout: int = 300) -> str: |
| 45 | + """ |
| 46 | + Build frontend assets for production. |
| 47 | +
|
| 48 | + Runs npm run build to execute Vite build, which: |
| 49 | + - Compiles JavaScript and CSS with Vite |
| 50 | + - Generates unique hashes for each asset (e.g., main-abc123.js) |
| 51 | + - Creates manifest.json mapping source files to hashed versions |
| 52 | + - Outputs to frontend/static/dist/ |
| 53 | +
|
| 54 | + Args: |
| 55 | + timeout: Timeout in seconds for the build process |
| 56 | +
|
| 57 | + Returns: |
| 58 | + Build output as string |
| 59 | +
|
| 60 | + Raises: |
| 61 | + BuildError: If package.json is missing, build fails, or times out |
| 62 | + """ |
| 63 | + # Check if package.json exists |
| 64 | + package_json = Path("package.json") |
| 65 | + if not package_json.exists(): |
| 66 | + raise BuildError( |
| 67 | + "No package.json found. " |
| 68 | + "Please run 'myfy frontend init' first to initialize the frontend." |
| 69 | + ) |
| 70 | + |
| 71 | + # Ensure dependencies are installed |
| 72 | + ensure_npm_dependencies_installed(timeout=timeout) |
| 73 | + |
| 74 | + # Run the build |
| 75 | + try: |
| 76 | + result = subprocess.run( |
| 77 | + ["npm", "run", "build"], |
| 78 | + check=True, |
| 79 | + capture_output=True, |
| 80 | + text=True, |
| 81 | + timeout=timeout, |
| 82 | + ) |
| 83 | + return result.stdout |
| 84 | + except subprocess.TimeoutExpired as e: |
| 85 | + raise BuildError( |
| 86 | + f"Build timed out after {timeout} seconds. " |
| 87 | + "The build process is taking too long. Please check for issues." |
| 88 | + ) from e |
| 89 | + except subprocess.CalledProcessError as e: |
| 90 | + error_msg = "Build failed" |
| 91 | + if e.stderr: |
| 92 | + error_msg += f": {e.stderr}" |
| 93 | + if e.stdout: |
| 94 | + error_msg += f"\n{e.stdout}" |
| 95 | + raise BuildError(error_msg) from e |
| 96 | + except FileNotFoundError as e: |
| 97 | + raise BuildError("npm not found. Please install Node.js to build the frontend.") from e |
0 commit comments