-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMakefile
More file actions
315 lines (260 loc) · 14.6 KB
/
Makefile
File metadata and controls
315 lines (260 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
.PHONY: install dev dev-web dev-api-cf dev-full build test lint clean format setup db-web-local check-tools help bundle remotion-bundle remotion-render deploy deploy-api deploy-web deploy-loro-sync deploy-all predeploy-check wrangler-whoami deploy-staging deploy-api-staging deploy-web-staging
# Use interactive shell to load .zshrc environment
#==============================================================================
# Configuration
#==============================================================================
# Proxy settings (disabled by default; set via environment or CLI if needed)
HTTP_PROXY ?=
HTTPS_PROXY ?=
NO_PROXY ?=
# Service ports (single source of truth)
# apps/web (Vite + RR7 + CF Vite plugin) is the user-facing entry point on :3000.
# It absorbed the gateway's proxy logic; there is no separate gateway worker
# anymore. /sync/*, /agents/*, /assets/*, /thumbnails/*, /upload/*, /api/v1/*,
# /api/tasks/*, /api/describe, /api/generate/* are proxied to api-cf via the
# service binding (or API_CF_URL fallback).
WEB_PORT ?= 3000
API_CF_PORT ?= 8789
RENDER_PORT ?= 8080
# Color output
BLUE := \033[0;34m
GREEN := \033[0;32m
YELLOW := \033[1;33m
RED := \033[0;31m
NC := \033[0m # No Color
#==============================================================================
# Help
#==============================================================================
help: ## Show this help message
@echo "$(BLUE)Master Clash - Development Commands$(NC)"
@echo ""
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf " $(GREEN)%-20s$(NC) %s\n", $$1, $$2}'
@echo ""
@echo "$(YELLOW)Environment Variables:$(NC)"
@echo " HTTP_PROXY - Proxy for HTTP requests (default: $(HTTP_PROXY))"
@echo " HTTPS_PROXY - Proxy for HTTPS requests (default: $(HTTPS_PROXY))"
@echo " NO_PROXY - Comma-separated list of bypassed hosts (default: $(NO_PROXY))"
#==============================================================================
# Prerequisites
#==============================================================================
check-tools: ## Verify required tools are installed
@echo "$(BLUE)Checking required tools...$(NC)"
@command -v pnpm >/dev/null 2>&1 || { echo "$(RED)Error: pnpm not found.$(NC)"; echo "$(YELLOW)Install: brew install pnpm$(NC)"; exit 1; }
@command -v turbo >/dev/null 2>&1 || { echo "$(YELLOW)Warning: turbo not found. Run 'pnpm install' first$(NC)"; }
@echo "$(GREEN)✓ All required tools are installed$(NC)"
#==============================================================================
# Installation
#==============================================================================
install: check-tools ## Install all dependencies
@echo "$(BLUE)Installing TypeScript dependencies...$(NC)"
@pnpm install
@echo "$(GREEN)✓ Installation complete$(NC)"
#==============================================================================
# Database
#==============================================================================
db-web-local: ## Setup/migrate local D1 database for web app
@echo "$(BLUE)Setting up local D1 database for web...$(NC)"
@cd apps/web && pnpm db:migrate:local
db-local: db-web-local ## Setup all local D1 databases
#==============================================================================
# Development Servers
#==============================================================================
dev-web: ## Start web app (Vite + RR7 + Cloudflare Vite plugin) on :3000
@echo "$(BLUE)Starting web on http://localhost:$(WEB_PORT)...$(NC)"
@cd apps/web && \
HTTP_PROXY=$(HTTP_PROXY) HTTPS_PROXY=$(HTTPS_PROXY) NO_PROXY=$(NO_PROXY) \
pnpm dev
dev-api-cf: ## (Deprecated) api-cf is started by vite as an auxiliary worker
@echo "$(YELLOW)api-cf is spawned by apps/web's vite config (auxiliaryWorkers).$(NC)"
@echo "$(YELLOW)Running it separately will collide on ports/DO state.$(NC)"
@echo "$(YELLOW)Use 'make dev-web' (or 'make dev') only.$(NC)"
dev-api-cf-standalone: ## Rare: run api-cf on its own for tests/debugging
@echo "$(BLUE)Starting api-cf on http://localhost:$(API_CF_PORT)...$(NC)"
@cd apps/api-cf && HTTP_PROXY=$(HTTP_PROXY) HTTPS_PROXY=$(HTTPS_PROXY) NO_PROXY=$(NO_PROXY) pnpm dev
dev-render: ## Start render server (runs outside wrangler — ffmpeg/remotion)
@echo "$(BLUE)Starting render server on http://localhost:$(RENDER_PORT)...$(NC)"
@cd apps/render-server && HTTP_PROXY=$(HTTP_PROXY) HTTPS_PROXY=$(HTTPS_PROXY) NO_PROXY=$(NO_PROXY) PORT=$(RENDER_PORT) pnpm dev
#==============================================================================
# Combined Development
#==============================================================================
dev: ## Start web (with api-cf as aux worker) + render-server in parallel
@echo "$(BLUE)Starting development environment...$(NC)"
@echo ""
@echo " ┌─────────────────────────────────────────────┐"
@echo " │ $(GREEN)Web:$(NC) http://localhost:$(WEB_PORT) │"
@echo " │ (Vite + RR7 + @cloudflare/vite-plugin) │"
@echo " │ Vite also spawns api-cf as aux worker; │"
@echo " │ service binding env.API_CF is wired in. │"
@echo " │ ├─ / → RR7 SPA │"
@echo " │ ├─ /sync/* → api-cf ProjectRoom │"
@echo " │ ├─ /agents/* → api-cf SupervisorAgent │"
@echo " │ ├─ /assets/* → api-cf R2 Assets │"
@echo " │ └─ /api/v1/* → api-cf REST (auth-gated) │"
@echo " │ │"
@echo " │ $(GREEN)Render:$(NC) http://localhost:$(RENDER_PORT) │"
@echo " │ (ffmpeg: thumbnails + timeline render) │"
@echo " └─────────────────────────────────────────────┘"
@echo ""
@$(MAKE) -j2 dev-web dev-render
dev-full: dev ## Alias for dev
#==============================================================================
# Build & Test
#==============================================================================
build: check-tools ## Build all packages
@echo "$(BLUE)Building TypeScript packages...$(NC)"
@pnpm turbo run build
test: check-tools ## Run all tests
@echo "$(BLUE)Running TypeScript tests...$(NC)"
@pnpm turbo run test
test-web: ## Run frontend tests only
@echo "$(BLUE)Running frontend tests...$(NC)"
@cd apps/web && pnpm test
#==============================================================================
# Remotion Bundle & Render
#==============================================================================
remotion-bundle: ## Build Remotion bundle for server-side rendering
@echo "$(BLUE)Building Remotion bundle...$(NC)"
@cd packages/remotion-components && npx remotion bundle src/Root.tsx
@echo "$(GREEN)✓ Bundle created at packages/remotion-components/build$(NC)"
remotion-render: ## Render video using Remotion CLI (for local testing)
@echo "$(BLUE)Rendering video...$(NC)"
@echo "$(YELLOW)Usage: make remotion-render PROPS='{\"tracks\":[...]}' OUTPUT=output.mp4$(NC)"
@[ -n "$(PROPS)" ] || { echo "$(RED)Error: PROPS is required$(NC)"; exit 1; }
@[ -n "$(OUTPUT)" ] || { echo "$(RED)Error: OUTPUT is required$(NC)"; exit 1; }
@cd packages/remotion-components && npx remotion render src/Root.tsx VideoComposition --props='$(PROPS)' --output="$(OUTPUT)"
@echo "$(GREEN)✓ Video rendered to $(OUTPUT)$(NC)"
bundle: remotion-bundle ## Alias for remotion-bundle
#==============================================================================
# Code Quality
#==============================================================================
lint: check-tools ## Lint all code
@echo "$(BLUE)Linting TypeScript...$(NC)"
@pnpm turbo run lint
lint-web: ## Lint frontend only
@echo "$(BLUE)Linting frontend...$(NC)"
@cd apps/web && pnpm lint
format: check-tools ## Format all code
@echo "$(BLUE)Formatting TypeScript...$(NC)"
@pnpm prettier --write "**/*.{ts,tsx,json,md}"
format-check: ## Check if code is formatted (CI use)
@echo "$(BLUE)Checking TypeScript formatting...$(NC)"
@pnpm prettier --check "**/*.{ts,tsx,json,md}"
#==============================================================================
# Deployment (Cloudflare Workers / Pages)
#==============================================================================
#
# What deploys where (per apps/*/wrangler.toml):
# - api-cf : Cloudflare Worker `clash-api`. Includes the
# RenderContainer DO whose image is built from
# apps/render-server/Dockerfile.cf during deploy. So
# deploying api-cf also updates the render container.
# render-server itself is NOT deployed standalone.
# - web : Cloudflare Pages/Worker `clash-web` (built by Vite +
# @cloudflare/vite-plugin). Has a `pnpm build` step
# before wrangler deploy.
# - loro-sync : Legacy worker. Functionality merged into api-cf —
# only deploy on explicit request.
#
# Order matters: deploy api-cf BEFORE web so web's runtime sees the new
# bindings/DO classes. The combined `make deploy` enforces this.
#
# Pre-deploy gates:
# - `make lint` runs first (unless SKIP_CHECKS=1)
# - wrangler must be authenticated; we surface `wrangler whoami` early
# so failures don't happen mid-deploy.
WRANGLER ?= pnpm --silent dlx wrangler
wrangler-whoami: ## Confirm wrangler is logged in (warns, doesn't fail)
@echo "$(BLUE)Checking wrangler auth...$(NC)"
@cd apps/api-cf && pnpm exec wrangler whoami 2>&1 | tail -3 || \
echo "$(YELLOW)Not logged in. Run: pnpm exec wrangler login$(NC)"
predeploy-check: ## Run lint before any deploy (skip with SKIP_CHECKS=1)
@if [ "$(SKIP_CHECKS)" = "1" ]; then \
echo "$(YELLOW)⚠ SKIP_CHECKS=1 — skipping pre-deploy lint$(NC)"; \
else \
echo "$(BLUE)Pre-deploy lint...$(NC)"; \
$(MAKE) lint || { echo "$(RED)Pre-deploy lint failed. Fix or set SKIP_CHECKS=1 to override.$(NC)"; exit 1; }; \
fi
deploy-api: predeploy-check ## Deploy api-cf (Workers + RenderContainer image)
@echo "$(BLUE)Deploying clash-api → Cloudflare Workers...$(NC)"
@# `pnpm run deploy` (not `pnpm deploy`) — the latter is pnpm's built-in
@# workspace-deployment command and does NOT execute package.json scripts.
@cd apps/api-cf && pnpm run deploy
@echo "$(GREEN)✓ api-cf deployed$(NC)"
deploy-web: predeploy-check ## Build + deploy web (Pages/Worker)
@echo "$(BLUE)Deploying clash-web → Cloudflare...$(NC)"
@cd apps/web && pnpm run deploy
@echo "$(GREEN)✓ web deployed$(NC)"
deploy-loro-sync: predeploy-check ## Deploy legacy loro-sync-server (rare)
@echo "$(YELLOW)⚠ loro-sync-server is legacy — verify you really want to deploy it.$(NC)"
@cd apps/loro-sync-server && pnpm run deploy
@echo "$(GREEN)✓ loro-sync-server deployed$(NC)"
deploy: predeploy-check ## Deploy api-cf then web (the standard production path)
@echo "$(BLUE)Deploying api-cf + web (in order)...$(NC)"
@$(MAKE) deploy-api SKIP_CHECKS=1
@$(MAKE) deploy-web SKIP_CHECKS=1
@echo ""
@echo "$(GREEN)✓ Standard deploy complete (api-cf + web)$(NC)"
@echo "$(YELLOW)Note: loro-sync-server is legacy and was not deployed.$(NC)"
@echo "$(YELLOW) Run 'make deploy-loro-sync' explicitly if needed.$(NC)"
deploy-all: predeploy-check ## Deploy everything including legacy loro-sync-server
@$(MAKE) deploy-api SKIP_CHECKS=1
@$(MAKE) deploy-web SKIP_CHECKS=1
@$(MAKE) deploy-loro-sync SKIP_CHECKS=1
@echo "$(GREEN)✓ All workers deployed$(NC)"
# ─── Staging ──────────────────────────────────────────────────────────
# `[env.staging]` in each app's wrangler.toml binds staging workers to
# PRODUCTION data (real D1 / R2). Worker names get a `-staging` suffix so
# they don't collide with the prod workers. The wrangler.toml staging
# sections must NOT be committed (they contain real resource IDs).
deploy-api-staging: predeploy-check ## Deploy clash-api-staging (uses prod data)
@echo "$(BLUE)Deploying clash-api-staging → Cloudflare Workers...$(NC)"
@cd apps/api-cf && pnpm exec wrangler deploy --env staging
@echo "$(GREEN)✓ api-cf staging deployed$(NC)"
deploy-web-staging: predeploy-check ## Build + deploy clash-web-staging (uses prod data)
@echo "$(BLUE)Deploying clash-web-staging → Cloudflare...$(NC)"
@cd apps/web && pnpm build && pnpm exec wrangler deploy --env staging
@echo "$(GREEN)✓ web staging deployed$(NC)"
deploy-staging: predeploy-check ## Deploy api-cf + web staging (in order)
@echo "$(BLUE)Deploying staging (api-cf → web)...$(NC)"
@$(MAKE) deploy-api-staging SKIP_CHECKS=1
@$(MAKE) deploy-web-staging SKIP_CHECKS=1
@echo ""
@echo "$(GREEN)✓ Staging deploy complete$(NC)"
@echo "$(YELLOW)Reminder: set MEDIA_GATEWAY_URL on api-cf-staging if rendering.$(NC)"
@echo "$(YELLOW) → wrangler secret put MEDIA_GATEWAY_URL --env staging$(NC)"
#==============================================================================
# Cleanup
#==============================================================================
clean: ## Clean all build artifacts and dependencies
@echo "$(BLUE)Cleaning TypeScript artifacts...$(NC)"
@pnpm clean || true
@rm -rf node_modules .turbo
@rm -f apps/web/local.db*
@echo "$(GREEN)✓ Cleanup complete$(NC)"
clean-all: clean ## Clean everything including all .wrangler directories
@echo "$(BLUE)Cleaning Wrangler caches...$(NC)"
@find . -type d -name ".wrangler" -exec rm -rf {} + 2>/dev/null || true
@echo "$(GREEN)✓ Deep cleanup complete$(NC)"
#==============================================================================
# Utilities
#==============================================================================
deps-tree: ## Show dependency tree for all packages
@echo "$(BLUE)TypeScript dependencies:$(NC)"
@pnpm list --depth 0
update-deps: ## Update all dependencies
@echo "$(BLUE)Updating TypeScript dependencies...$(NC)"
@pnpm update --latest
info: ## Show project information
@echo "$(BLUE)Master Clash - Project Information$(NC)"
@echo ""
@echo "Project Root: $(shell pwd)"
@echo "Git Branch: $$(git branch --show-current 2>/dev/null || echo 'Not a git repo')"
@echo "Git Status: $$(git status --short 2>/dev/null | wc -l | tr -d ' ') files modified"
@echo ""
@echo "$(BLUE)Node Version:$(NC) $$(node --version 2>/dev/null || echo 'Not installed')"
@echo "$(BLUE)PNPM Version:$(NC) $$(pnpm --version 2>/dev/null || echo 'Not installed')"
@echo ""
@echo "$(BLUE)Environment:$(NC)"
@echo " HTTP_PROXY=$(HTTP_PROXY)"
@echo " HTTPS_PROXY=$(HTTPS_PROXY)"
@echo " NO_PROXY=$(NO_PROXY)"