|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Development Commands |
| 6 | + |
| 7 | +### Build and Development |
| 8 | +- `npm run build` - Compiles TypeScript using `tsc --project tsconfig.build.json` |
| 9 | +- `npm run dev` - Runs TypeScript compiler in watch mode |
| 10 | +- `npm run clean` - Removes the `dist/` directory |
| 11 | + |
| 12 | +### Testing |
| 13 | +- `npm test` - Runs the full test suite (unit, integration, and e2e tests) |
| 14 | +- `npm run test:unit` - Runs unit tests only with `vitest run tests/unit/` |
| 15 | +- `npm run test:integration` - Runs integration tests with `vitest run --retry=3 tests/integration/` |
| 16 | +- `npm run test:e2e` - Runs end-to-end tests with `vitest run --config vitest.e2e.config.ts` |
| 17 | +- **Single test file**: `npm exec vitest -- run tests/unit/lib/account.test.ts` |
| 18 | +- **Single test by name**: `npm exec vitest -- run tests/unit/lib/account.test.ts -t 'test name'` |
| 19 | +- **Debug tests**: `DEBUG_TESTS=true npm exec vitest -- run [test-file] -t 'test name'` |
| 20 | +- `npm run test:init` - Sets up test dependencies for various fixtures (Hugo, Next.js, monorepo) |
| 21 | + |
| 22 | +### Code Quality |
| 23 | +- `npm run lint` - Runs ESLint with cache |
| 24 | +- `npm run lint:fix` - Runs ESLint and automatically fixes issues |
| 25 | +- `npm run format` - Formats code with Prettier |
| 26 | +- `npm run format:check` - Checks code formatting without modifying files |
| 27 | +- `npm run typecheck` - Runs TypeScript type checking |
| 28 | +- `npm run typecheck:watch` - Runs TypeScript type checking in watch mode |
| 29 | + |
| 30 | +### Running the CLI Locally |
| 31 | +- `./bin/run.js [command]` - Runs the CLI locally |
| 32 | +- `DEBUG=true ./bin/run.js [command]` - Runs with stack traces enabled for debugging |
| 33 | +- `npm run start -- [command]` - Alternative way to run CLI locally |
| 34 | + |
| 35 | +## Architecture |
| 36 | + |
| 37 | +### Core Structure |
| 38 | +The Netlify CLI is built with **Commander.js** for CLI interface, **@netlify/js-client** for API interactions, and **TypeScript** with modular architecture. The system uses a registry pattern for managing Functions and Edge Functions, with sophisticated local development server capabilities. |
| 39 | + |
| 40 | +### Key Architectural Patterns |
| 41 | + |
| 42 | +#### Command Architecture |
| 43 | +- All commands extend `BaseCommand` class (`src/commands/base-command.ts`) which provides: |
| 44 | + - Consistent config loading and API client setup |
| 45 | + - Site information management and linking |
| 46 | + - Authentication and token handling |
| 47 | + - Analytics and telemetry integration |
| 48 | +- Commands follow a modular structure with separate `index.ts` files for exports |
| 49 | +- Each command supports both interactive prompts and non-interactive flag-based operation |
| 50 | + |
| 51 | +#### Registry Pattern for Runtime Management |
| 52 | +- **FunctionsRegistry** (`src/lib/functions/registry.ts`): Manages Netlify Functions lifecycle |
| 53 | + - Supports multiple runtimes (JavaScript/TypeScript, Go, Rust) |
| 54 | + - Handles hot reloading and file watching |
| 55 | + - Manages function builds and serving |
| 56 | +- **EdgeFunctionsRegistry** (`src/lib/edge-functions/registry.ts`): Manages Edge Functions |
| 57 | + - Uses `@netlify/edge-bundler` for Deno-based edge runtime |
| 58 | + - Handles Edge Function deployment and local serving |
| 59 | + |
| 60 | +#### Development Server Architecture (`netlify dev`) |
| 61 | +The dev server (`src/commands/dev/dev.ts`) orchestrates multiple subsystems: |
| 62 | +- **Proxy Server**: Routes requests between static files, functions, and edge functions |
| 63 | +- **Functions Server**: Executes Netlify Functions locally with runtime-specific handlers |
| 64 | +- **Edge Functions Proxy**: Serves Edge Functions via Deno runtime |
| 65 | +- **Framework Detection**: Auto-detects and integrates with various web frameworks |
| 66 | +- **Live Tunneling**: Provides public URLs for local development via Netlify's tunnel service |
| 67 | + |
| 68 | +#### Config System |
| 69 | +- Uses `@netlify/config` for configuration resolution and normalization |
| 70 | +- Supports `netlify.toml` files with environment-specific overrides |
| 71 | +- Integrates with Netlify's build plugins system |
| 72 | +- Handles environment variable injection from multiple sources (process, .env files, Netlify site settings) |
| 73 | + |
| 74 | +### Key Libraries and Their Roles |
| 75 | + |
| 76 | +#### Core Infrastructure |
| 77 | +- `src/lib/api.ts` - Netlify API client wrapper with authentication |
| 78 | +- `src/lib/build.ts` - Build system integration and config caching |
| 79 | +- `src/lib/settings.ts` - Project and global settings management |
| 80 | +- `src/utils/command-helpers.ts` - Shared utilities for CLI commands (logging, error handling, prompts) |
| 81 | + |
| 82 | +#### Function Runtime System |
| 83 | +- `src/lib/functions/runtimes/` - Runtime-specific builders and executors |
| 84 | + - `js/` - JavaScript/TypeScript function handling with `zip-it-and-ship-it` |
| 85 | + - `go/` - Go function compilation and execution |
| 86 | + - `rust/` - Rust function compilation via Cargo |
| 87 | +- `src/lib/functions/server.ts` - Local function server with request/response handling |
| 88 | + |
| 89 | +#### Development Tools |
| 90 | +- `src/utils/dev.ts` - Development server utilities and environment setup |
| 91 | +- `src/utils/proxy-server.ts` - HTTP proxy for routing dev server requests |
| 92 | +- `src/utils/detect-server-settings.ts` - Framework detection and port management |
| 93 | + |
| 94 | +#### Deployment System |
| 95 | +- `src/utils/deploy/` - Site deployment orchestration |
| 96 | + - File hashing, diffing, and upload optimization |
| 97 | + - Build artifact management and caching |
| 98 | + - Progress tracking and status reporting |
| 99 | + |
| 100 | +### Testing Architecture |
| 101 | +- **Unit Tests** (`tests/unit/`): Test individual modules and utilities |
| 102 | +- **Integration Tests** (`tests/integration/`): Test full command workflows using fixtures |
| 103 | +- **E2E Tests**: Test complete user scenarios with real Netlify API interactions |
| 104 | +- **Fixtures** (`tests/integration/__fixtures__/`): Sample projects for testing different frameworks and configurations |
| 105 | + |
| 106 | +### Important Implementation Details |
| 107 | + |
| 108 | +#### Environment Variable Handling |
| 109 | +Environment variables are loaded from multiple sources with specific precedence: |
| 110 | +1. Process environment |
| 111 | +2. `.env` files (multiple variants supported) |
| 112 | +3. Netlify site settings (shared, project-specific) |
| 113 | +4. Addon-provided variables |
| 114 | +5. Build-time configuration |
| 115 | + |
| 116 | +#### Function URL Routing |
| 117 | +Functions are accessible via standardized URL patterns: |
| 118 | +- `/.netlify/functions/[function-name]` - Standard functions |
| 119 | +- `/.netlify/builders/[function-name]` - On-demand builders |
| 120 | +- Custom paths supported via function configuration |
| 121 | + |
| 122 | +#### Build Plugin Integration |
| 123 | +The CLI integrates with Netlify's build plugin system, allowing plugins to: |
| 124 | +- Modify build configuration |
| 125 | +- Add custom build steps |
| 126 | +- Integrate with the development server |
| 127 | +- Provide additional CLI commands |
| 128 | + |
| 129 | +### Development Setup Requirements |
| 130 | +- Node.js 20.12.2+ required |
| 131 | +- Git LFS must be installed for full test suite |
| 132 | +- Some integration tests require Netlify Auth Token (`NETLIFY_AUTH_TOKEN`) or login via `./bin/run.js login` |
| 133 | +- Live tests can be disabled with `NETLIFY_TEST_DISABLE_LIVE=true` |
| 134 | + |
| 135 | +### Coding Style: |
| 136 | +- Never write comments on what the code does, make the code clean and self explanatory instead |
0 commit comments