[ PROMPT_NODE_24022 ]
C3 设计模式
[ SKILL_DOCUMENTATION ]
# C3 使用模式
## 快速工作流
bash
# TypeScript API Worker
npm create cloudflare@latest my-api -- --type=hello-world --lang=ts --deploy
# Next.js on Pages
npm create cloudflare@latest my-app -- --type=web-app --framework=next --platform=pages --ts --deploy
# Astro 静态网站
npm create cloudflare@latest my-blog -- --type=web-app --framework=astro --platform=pages --ts
## CI/CD (GitHub Actions)
yaml
- name: Deploy
run: npm run deploy
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
**非交互式模式要求:**
bash
--type= # 必需
--no-git # 推荐 (CI 已在 git 中)
--no-deploy # 使用密钥单独部署
--framework= # 用于 web-app
--ts / --no-ts # 必需
## Monorepo
C3 会检测工作区配置 (`package.json` 工作区或 `pnpm-workspace.yaml`)。
bash
cd packages/
npm create cloudflare@latest my-worker -- --type=hello-world --lang=ts --no-deploy
## 自定义模板
bash
# GitHub 仓库
npm create cloudflare@latest -- --template=username/repo
npm create cloudflare@latest -- --template=cloudflare/templates/worker-openapi
# 本地路径
npm create cloudflare@latest my-app -- --template=../my-template
**模板需要 `c3.config.json`:**
{
"name": "my-template",
"category": "hello-world",
"copies": [{ "path": "src/" }, { "path": "wrangler.jsonc" }],
"transforms": [{ "path": "package.json", "jsonc": { "name": "{{projectName}}" }}]
}
## 现有项目
bash
# 将 Cloudflare 添加到现有 Worker
npm create cloudflare@latest . -- --type=pre-existing --existing-script=./dist/index.js
# 添加到现有框架应用
npm create cloudflare@latest . -- --type=web-app --framework=next --platform=pages --ts
## 创建后检查清单
1. 检查 `wrangler.jsonc` - 设置 `compatibility_date`,验证 `name`
2. 创建绑定:`wrangler kv namespace create`, `wrangler d1 create`, `wrangler r2 bucket create`
3. 生成类型:`npm run cf-typegen`
4. 测试:`npm run dev`
5. 部署:`npm run deploy`
6. 设置密钥:`wrangler secret put SECRET_NAME`