cf-yoyo 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.json +28 -0
- package/.github/workflows/ci.yml +96 -0
- package/.prettierrc.json +10 -0
- package/CHANGELOG.md +55 -0
- package/README.md +138 -0
- package/__tests__/cli-e2e.test.ts +145 -0
- package/__tests__/config.test.ts +268 -0
- package/__tests__/filesystem.test.ts +453 -0
- package/__tests__/logger.test.ts +274 -0
- package/__tests__/template-engine.test.ts +450 -0
- package/__tests__/types.test.ts +25 -0
- package/deep_todos.md +766 -0
- package/dist/cli/commands/create.d.ts +26 -0
- package/dist/cli/commands/create.d.ts.map +1 -0
- package/dist/cli/commands/create.js +308 -0
- package/dist/cli/commands/create.js.map +1 -0
- package/dist/cli/commands/git.d.ts +10 -0
- package/dist/cli/commands/git.d.ts.map +1 -0
- package/dist/cli/commands/git.js +887 -0
- package/dist/cli/commands/git.js.map +1 -0
- package/dist/cli/commands/list.d.ts +10 -0
- package/dist/cli/commands/list.d.ts.map +1 -0
- package/dist/cli/commands/list.js +90 -0
- package/dist/cli/commands/list.js.map +1 -0
- package/dist/cli/index.d.ts +15 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +62 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/core/config.d.ts +35 -0
- package/dist/core/config.d.ts.map +1 -0
- package/dist/core/config.js +260 -0
- package/dist/core/config.js.map +1 -0
- package/dist/core/filesystem.d.ts +84 -0
- package/dist/core/filesystem.d.ts.map +1 -0
- package/dist/core/filesystem.js +417 -0
- package/dist/core/filesystem.js.map +1 -0
- package/dist/core/git-token.d.ts +81 -0
- package/dist/core/git-token.d.ts.map +1 -0
- package/dist/core/git-token.js +244 -0
- package/dist/core/git-token.js.map +1 -0
- package/dist/core/git.d.ts +70 -0
- package/dist/core/git.d.ts.map +1 -0
- package/dist/core/git.js +367 -0
- package/dist/core/git.js.map +1 -0
- package/dist/core/prompt.d.ts +28 -0
- package/dist/core/prompt.d.ts.map +1 -0
- package/dist/core/prompt.js +253 -0
- package/dist/core/prompt.js.map +1 -0
- package/dist/core/template-engine.d.ts +52 -0
- package/dist/core/template-engine.d.ts.map +1 -0
- package/dist/core/template-engine.js +308 -0
- package/dist/core/template-engine.js.map +1 -0
- package/dist/core/template-manager.d.ts +54 -0
- package/dist/core/template-manager.d.ts.map +1 -0
- package/dist/core/template-manager.js +330 -0
- package/dist/core/template-manager.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +19 -0
- package/dist/index.js.map +1 -0
- package/dist/types/index.d.ts +244 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +51 -0
- package/dist/types/index.js.map +1 -0
- package/dist/utils/logger.d.ts +68 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/logger.js +140 -0
- package/dist/utils/logger.js.map +1 -0
- package/memory.md +241 -0
- package/need-debug.md +395 -0
- package/package.json +42 -0
- package/src/cli/commands/create.ts +326 -0
- package/src/cli/commands/git.ts +1001 -0
- package/src/cli/commands/list.ts +97 -0
- package/src/cli/index.ts +71 -0
- package/src/core/config.ts +262 -0
- package/src/core/filesystem.ts +408 -0
- package/src/core/git-token.ts +248 -0
- package/src/core/git.ts +384 -0
- package/src/core/prompt.ts +345 -0
- package/src/core/template-engine.ts +324 -0
- package/src/core/template-manager.ts +338 -0
- package/src/index.ts +19 -0
- package/src/types/index.ts +259 -0
- package/src/utils/logger.ts +150 -0
- package/templates/pages/basic/README.md.mustache +63 -0
- package/templates/pages/basic/package.json.mustache +23 -0
- package/templates/pages/basic/public/css/style.css +199 -0
- package/templates/pages/basic/public/index.html.mustache +72 -0
- package/templates/pages/basic/public/js/main.js +103 -0
- package/templates/pages/basic/template.json +38 -0
- package/templates/pages/basic/tsconfig.json +21 -0
- package/templates/pages/basic/wrangler.toml.mustache +14 -0
- package/templates/pages/basic-js/README.md.mustache +62 -0
- package/templates/pages/basic-js/package.json.mustache +25 -0
- package/templates/pages/basic-js/public/css/style.css +212 -0
- package/templates/pages/basic-js/public/index.html.mustache +53 -0
- package/templates/pages/basic-js/public/js/main.js +134 -0
- package/templates/pages/basic-js/template.json +35 -0
- package/templates/pages/basic-js/wrangler.toml.mustache +14 -0
- package/templates/pages/react/README.md.mustache +97 -0
- package/templates/pages/react/index.html.mustache +14 -0
- package/templates/pages/react/package.json.mustache +34 -0
- package/templates/pages/react/src/App.css +168 -0
- package/templates/pages/react/src/App.tsx.mustache +62 -0
- package/templates/pages/react/src/index.css +53 -0
- package/templates/pages/react/src/main.tsx.mustache +10 -0
- package/templates/pages/react/src/vite-env.d.ts +1 -0
- package/templates/pages/react/template.json +54 -0
- package/templates/pages/react/tsconfig.json +21 -0
- package/templates/pages/react/tsconfig.node.json +10 -0
- package/templates/pages/react/vite.config.ts +16 -0
- package/templates/worker/basic/README.md.mustache +56 -0
- package/templates/worker/basic/package.json.mustache +29 -0
- package/templates/worker/basic/src/index.ts.mustache +125 -0
- package/templates/worker/basic/template.json +30 -0
- package/templates/worker/basic/tsconfig.json +24 -0
- package/templates/worker/basic/wrangler.toml.mustache +33 -0
- package/templates/worker/basic-js/README.md.mustache +55 -0
- package/templates/worker/basic-js/package.json.mustache +25 -0
- package/templates/worker/basic-js/src/index.js.mustache +146 -0
- package/templates/worker/basic-js/template.json +27 -0
- package/templates/worker/basic-js/wrangler.toml.mustache +33 -0
- package/templates/worker/hono/README.md.mustache +79 -0
- package/templates/worker/hono/package.json.mustache +33 -0
- package/templates/worker/hono/src/index.ts.mustache +64 -0
- package/templates/worker/hono/src/routes/index.ts.mustache +165 -0
- package/templates/worker/hono/template.json +34 -0
- package/templates/worker/hono/tsconfig.json +24 -0
- package/templates/worker/hono/wrangler.toml.mustache +36 -0
- package/templates/worker/hono-js/README.md.mustache +67 -0
- package/templates/worker/hono-js/package.json.mustache +29 -0
- package/templates/worker/hono-js/src/index.js.mustache +55 -0
- package/templates/worker/hono-js/src/routes/index.js.mustache +127 -0
- package/templates/worker/hono-js/template.json +31 -0
- package/templates/worker/hono-js/wrangler.toml.mustache +36 -0
- package/thoughts/ledgers/CONTINUITY_ses_287e.md +74 -0
- package/thoughts/ledgers/CONTINUITY_ses_28b5.md +85 -0
- package/tsconfig.json +30 -0
- package/vitest.config.ts +20 -0
- package//351/240/205/347/233/256/350/241/250.md +140 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# {{projectName}}
|
|
2
|
+
|
|
3
|
+
{{description}}
|
|
4
|
+
|
|
5
|
+
這是一個基於 Hono 框架的 Cloudflare Worker 專案,使用 create-cf-project 腳手架工具建立。
|
|
6
|
+
|
|
7
|
+
## 🚀 快速開始
|
|
8
|
+
|
|
9
|
+
### 安裝依賴
|
|
10
|
+
|
|
11
|
+
\`\`\`bash
|
|
12
|
+
npm install
|
|
13
|
+
\`\`\`
|
|
14
|
+
|
|
15
|
+
### 開發模式
|
|
16
|
+
|
|
17
|
+
\`\`\`bash
|
|
18
|
+
npm run dev
|
|
19
|
+
\`\`\`
|
|
20
|
+
|
|
21
|
+
這會啟動 wrangler dev 伺服器,你可以在 http://localhost:8787 測試 Worker。
|
|
22
|
+
|
|
23
|
+
### 部署
|
|
24
|
+
|
|
25
|
+
\`\`\`bash
|
|
26
|
+
npm run deploy
|
|
27
|
+
\`\`\`
|
|
28
|
+
|
|
29
|
+
## 📁 專案結構
|
|
30
|
+
|
|
31
|
+
\`\`\`
|
|
32
|
+
{{projectName}}/
|
|
33
|
+
├── src/
|
|
34
|
+
│ ├── index.ts # Worker 入口檔案
|
|
35
|
+
│ └── routes/
|
|
36
|
+
│ └── index.ts # 路由定義
|
|
37
|
+
├── package.json # 專案配置
|
|
38
|
+
├── tsconfig.json # TypeScript 配置
|
|
39
|
+
├── wrangler.toml # Cloudflare 配置
|
|
40
|
+
└── README.md # 本文件
|
|
41
|
+
\`\`\`
|
|
42
|
+
|
|
43
|
+
## 🛠️ 技術棧
|
|
44
|
+
|
|
45
|
+
- **框架**: [Hono](https://hono.dev) - 輕量級、快速的 Web 框架
|
|
46
|
+
- **平台**: Cloudflare Workers
|
|
47
|
+
- **語言**: TypeScript
|
|
48
|
+
- **執行環境**: Edge Runtime
|
|
49
|
+
|
|
50
|
+
## 📡 API 端點
|
|
51
|
+
|
|
52
|
+
| 方法 | 路徑 | 描述 |
|
|
53
|
+
|------|------|------|
|
|
54
|
+
| GET | `/` | 首頁 |
|
|
55
|
+
| GET | `/api/health` | 健康檢查 |
|
|
56
|
+
| GET | `/api/users` | 取得所有用戶 |
|
|
57
|
+
| GET | `/api/users/:id` | 取得指定用戶 |
|
|
58
|
+
| POST | `/api/users` | 創建新用戶 |
|
|
59
|
+
| GET | `/api/info` | 請求信息 |
|
|
60
|
+
|
|
61
|
+
## ✨ Hono 特性
|
|
62
|
+
|
|
63
|
+
- ⚡ **極快**: 基於 Web 標準 API
|
|
64
|
+
- 🪶 **輕量**: 零依賴
|
|
65
|
+
- 🔧 **中介層**: 內建中介層支援
|
|
66
|
+
- 📝 **類型安全**: 完整的 TypeScript 支援
|
|
67
|
+
- 🎯 **路由**: 直觀的路由系統
|
|
68
|
+
|
|
69
|
+
## 📚 相關資源
|
|
70
|
+
|
|
71
|
+
- [Hono 文檔](https://hono.dev)
|
|
72
|
+
- [Cloudflare Workers 文檔](https://developers.cloudflare.com/workers/)
|
|
73
|
+
- [Wrangler CLI 文檔](https://developers.cloudflare.com/workers/wrangler/)
|
|
74
|
+
|
|
75
|
+
## 📝 授權
|
|
76
|
+
|
|
77
|
+
{{#license}}{{license}}{{/license}}{{^license}}MIT{{/license}}
|
|
78
|
+
|
|
79
|
+
建立於 {{date}} | {{year}}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "{{projectName}}",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "{{description}}",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"dev": "wrangler dev",
|
|
8
|
+
"deploy": "wrangler deploy",
|
|
9
|
+
"build": "tsc",
|
|
10
|
+
"type-check": "tsc --noEmit",
|
|
11
|
+
"lint": "eslint src/**/*.ts"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"cloudflare",
|
|
15
|
+
"worker",
|
|
16
|
+
"hono",
|
|
17
|
+
"edge"
|
|
18
|
+
],
|
|
19
|
+
"author": "{{#author}}{{author}}{{/author}}{{^author}}Anonymous{{/author}}",
|
|
20
|
+
"license": "{{#license}}{{license}}{{/license}}{{^license}}MIT{{/license}}",
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"hono": "^4.2.0"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@cloudflare/workers-types": "^4.20240403.0",
|
|
26
|
+
"@types/node": "^20.0.0",
|
|
27
|
+
"eslint": "^8.57.0",
|
|
28
|
+
"@typescript-eslint/eslint-plugin": "^7.0.0",
|
|
29
|
+
"@typescript-eslint/parser": "^7.0.0",
|
|
30
|
+
"typescript": "^5.3.0",
|
|
31
|
+
"wrangler": "^3.39.0"
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hono Cloudflare Worker 入口檔案
|
|
3
|
+
* {{projectName}} - {{description}}
|
|
4
|
+
* 建立日期:{{date}}
|
|
5
|
+
*
|
|
6
|
+
* 使用 Hono 框架進行路由管理
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { Hono } from 'hono';
|
|
10
|
+
import { poweredBy } from 'hono/powered-by';
|
|
11
|
+
import { logger as honoLogger } from 'hono/logger';
|
|
12
|
+
import { cors } from 'hono/cors';
|
|
13
|
+
import { routes } from './routes';
|
|
14
|
+
|
|
15
|
+
// 定義環境變數類型
|
|
16
|
+
export interface Env {
|
|
17
|
+
{{#isKV}}
|
|
18
|
+
CACHE: KVNamespace;
|
|
19
|
+
{{/isKV}}
|
|
20
|
+
{{#isD1}}
|
|
21
|
+
DB: D1Database;
|
|
22
|
+
{{/isD1}}
|
|
23
|
+
{{#isR2}}
|
|
24
|
+
BUCKET: R2Bucket;
|
|
25
|
+
{{/isR2}}
|
|
26
|
+
ENVIRONMENT: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// 創建 Hono 應用
|
|
30
|
+
const app = new Hono<{ Bindings: Env }>();
|
|
31
|
+
|
|
32
|
+
// 中介層
|
|
33
|
+
app.use('*', poweredBy());
|
|
34
|
+
app.use('*', honoLogger());
|
|
35
|
+
app.use('*', cors({
|
|
36
|
+
origin: '*',
|
|
37
|
+
allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
|
|
38
|
+
allowHeaders: ['Content-Type', 'Authorization'],
|
|
39
|
+
}));
|
|
40
|
+
|
|
41
|
+
// 註冊路由
|
|
42
|
+
app.route('/', routes);
|
|
43
|
+
|
|
44
|
+
// 錯誤處理
|
|
45
|
+
app.onError((err, c) => {
|
|
46
|
+
console.error('應用錯誤:', err);
|
|
47
|
+
return c.json(
|
|
48
|
+
{
|
|
49
|
+
error: 'Internal Server Error',
|
|
50
|
+
message: err.message
|
|
51
|
+
},
|
|
52
|
+
500
|
|
53
|
+
);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// 404 處理
|
|
57
|
+
app.notFound((c) => {
|
|
58
|
+
return c.json(
|
|
59
|
+
{ error: 'Not Found' },
|
|
60
|
+
404
|
|
61
|
+
);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
export default app;
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hono 路由定義
|
|
3
|
+
* 集中管理所有 API 路由
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { Hono } from 'hono';
|
|
7
|
+
import type { Env } from '../index';
|
|
8
|
+
|
|
9
|
+
const routes = new Hono<{ Bindings: Env }>();
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 首頁
|
|
13
|
+
*/
|
|
14
|
+
routes.get('/', (c) => {
|
|
15
|
+
const html = `
|
|
16
|
+
<!DOCTYPE html>
|
|
17
|
+
<html lang="zh-TW">
|
|
18
|
+
<head>
|
|
19
|
+
<meta charset="UTF-8">
|
|
20
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
21
|
+
<title>{{projectName}}</title>
|
|
22
|
+
<style>
|
|
23
|
+
* { margin: 0; padding: 0; box-sizing: border-box; }
|
|
24
|
+
body {
|
|
25
|
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
|
26
|
+
min-height: 100vh;
|
|
27
|
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
28
|
+
display: flex;
|
|
29
|
+
align-items: center;
|
|
30
|
+
justify-content: center;
|
|
31
|
+
color: white;
|
|
32
|
+
}
|
|
33
|
+
.container {
|
|
34
|
+
text-align: center;
|
|
35
|
+
padding: 2rem;
|
|
36
|
+
max-width: 600px;
|
|
37
|
+
}
|
|
38
|
+
h1 {
|
|
39
|
+
font-size: 3rem;
|
|
40
|
+
margin-bottom: 1rem;
|
|
41
|
+
font-weight: 700;
|
|
42
|
+
}
|
|
43
|
+
p {
|
|
44
|
+
font-size: 1.2rem;
|
|
45
|
+
opacity: 0.9;
|
|
46
|
+
line-height: 1.6;
|
|
47
|
+
margin-bottom: 2rem;
|
|
48
|
+
}
|
|
49
|
+
.badges {
|
|
50
|
+
display: flex;
|
|
51
|
+
gap: 1rem;
|
|
52
|
+
justify-content: center;
|
|
53
|
+
flex-wrap: wrap;
|
|
54
|
+
}
|
|
55
|
+
.badge {
|
|
56
|
+
background: rgba(255, 255, 255, 0.2);
|
|
57
|
+
backdrop-filter: blur(10px);
|
|
58
|
+
padding: 0.5rem 1rem;
|
|
59
|
+
border-radius: 20px;
|
|
60
|
+
font-size: 0.9rem;
|
|
61
|
+
font-weight: 500;
|
|
62
|
+
}
|
|
63
|
+
.links {
|
|
64
|
+
margin-top: 2rem;
|
|
65
|
+
}
|
|
66
|
+
.links a {
|
|
67
|
+
color: white;
|
|
68
|
+
text-decoration: none;
|
|
69
|
+
padding: 0.5rem 1rem;
|
|
70
|
+
border: 1px solid rgba(255,255,255,0.3);
|
|
71
|
+
border-radius: 8px;
|
|
72
|
+
margin: 0 0.5rem;
|
|
73
|
+
transition: all 0.3s;
|
|
74
|
+
}
|
|
75
|
+
.links a:hover {
|
|
76
|
+
background: rgba(255,255,255,0.2);
|
|
77
|
+
}
|
|
78
|
+
</style>
|
|
79
|
+
</head>
|
|
80
|
+
<body>
|
|
81
|
+
<div class="container">
|
|
82
|
+
<h1>⚡ {{projectName}}</h1>
|
|
83
|
+
<p>{{description}}</p>
|
|
84
|
+
<div class="badges">
|
|
85
|
+
<span class="badge">Hono Framework</span>
|
|
86
|
+
<span class="badge">Cloudflare Worker</span>
|
|
87
|
+
</div>
|
|
88
|
+
<div class="links">
|
|
89
|
+
<a href="/api/health">Health Check</a>
|
|
90
|
+
<a href="/api/users">Users API</a>
|
|
91
|
+
</div>
|
|
92
|
+
</div>
|
|
93
|
+
</body>
|
|
94
|
+
</html>
|
|
95
|
+
`.trim();
|
|
96
|
+
|
|
97
|
+
return c.html(html);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* 健康檢查
|
|
102
|
+
*/
|
|
103
|
+
routes.get('/api/health', (c) => {
|
|
104
|
+
return c.json({
|
|
105
|
+
status: 'ok',
|
|
106
|
+
timestamp: new Date().toISOString(),
|
|
107
|
+
version: '1.0.0',
|
|
108
|
+
environment: c.env.ENVIRONMENT || 'development',
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* 用戶 API
|
|
114
|
+
*/
|
|
115
|
+
routes.get('/api/users', (c) => {
|
|
116
|
+
// 示例數據
|
|
117
|
+
const users = [
|
|
118
|
+
{ id: 1, name: 'Alice', email: 'alice@example.com' },
|
|
119
|
+
{ id: 2, name: 'Bob', email: 'bob@example.com' },
|
|
120
|
+
];
|
|
121
|
+
|
|
122
|
+
return c.json({ users });
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
routes.get('/api/users/:id', (c) => {
|
|
126
|
+
const id = c.req.param('id');
|
|
127
|
+
|
|
128
|
+
return c.json({
|
|
129
|
+
id: parseInt(id),
|
|
130
|
+
name: `User ${id}`,
|
|
131
|
+
email: `user${id}@example.com`,
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
routes.post('/api/users', async (c) => {
|
|
136
|
+
try {
|
|
137
|
+
const body = await c.req.json();
|
|
138
|
+
|
|
139
|
+
return c.json({
|
|
140
|
+
id: Date.now(),
|
|
141
|
+
...body,
|
|
142
|
+
created: new Date().toISOString(),
|
|
143
|
+
}, 201);
|
|
144
|
+
} catch (error) {
|
|
145
|
+
return c.json({ error: 'Invalid JSON' }, 400);
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* 示例 API - 取得請求信息
|
|
151
|
+
*/
|
|
152
|
+
routes.get('/api/info', (c) => {
|
|
153
|
+
const url = new URL(c.req.url);
|
|
154
|
+
|
|
155
|
+
return c.json({
|
|
156
|
+
method: c.req.method,
|
|
157
|
+
url: c.req.url,
|
|
158
|
+
path: url.pathname,
|
|
159
|
+
query: Object.fromEntries(url.searchParams),
|
|
160
|
+
headers: Object.fromEntries(c.req.headers),
|
|
161
|
+
timestamp: new Date().toISOString(),
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
export { routes };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "Hono Cloudflare Worker",
|
|
3
|
+
"description": "使用 Hono 框架的 Cloudflare Worker 專案,提供簡潔的路由和中介層支援",
|
|
4
|
+
"projectType": "worker",
|
|
5
|
+
"templateType": "hono",
|
|
6
|
+
"files": [
|
|
7
|
+
{
|
|
8
|
+
"sourcePath": "package.json.mustache",
|
|
9
|
+
"targetPath": "package.json"
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
"sourcePath": "wrangler.toml.mustache",
|
|
13
|
+
"targetPath": "wrangler.toml"
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
"sourcePath": "src/index.ts.mustache",
|
|
17
|
+
"targetPath": "src/index.ts"
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
"sourcePath": "src/routes/index.ts.mustache",
|
|
21
|
+
"targetPath": "src/routes/index.ts"
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
"sourcePath": "tsconfig.json",
|
|
25
|
+
"targetPath": "tsconfig.json"
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
"sourcePath": "README.md.mustache",
|
|
29
|
+
"targetPath": "README.md"
|
|
30
|
+
}
|
|
31
|
+
],
|
|
32
|
+
"requiredVars": ["projectName", "projectType", "template"],
|
|
33
|
+
"optionalVars": ["author", "email", "description", "license"]
|
|
34
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "bundler",
|
|
6
|
+
"lib": ["ES2022"],
|
|
7
|
+
"strict": true,
|
|
8
|
+
"noUnusedLocals": true,
|
|
9
|
+
"noUnusedParameters": true,
|
|
10
|
+
"noImplicitReturns": true,
|
|
11
|
+
"noFallthroughCasesInSwitch": true,
|
|
12
|
+
"esModuleInterop": true,
|
|
13
|
+
"skipLibCheck": true,
|
|
14
|
+
"forceConsistentCasingInFileNames": true,
|
|
15
|
+
"outDir": "./dist",
|
|
16
|
+
"rootDir": "./src",
|
|
17
|
+
"declaration": true,
|
|
18
|
+
"declarationMap": true,
|
|
19
|
+
"sourceMap": true,
|
|
20
|
+
"types": ["@cloudflare/workers-types", "node"]
|
|
21
|
+
},
|
|
22
|
+
"include": ["src/**/*"],
|
|
23
|
+
"exclude": ["node_modules", "dist"]
|
|
24
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
#:schema node_modules/wrangler/config-schema.json
|
|
2
|
+
name = "{{projectName}}"
|
|
3
|
+
main = "src/index.ts"
|
|
4
|
+
compatibility_date = "{{date}}"
|
|
5
|
+
compatibility_flags = ["nodejs_compat"]
|
|
6
|
+
|
|
7
|
+
# 環境變數(開發環境)
|
|
8
|
+
[env.development]
|
|
9
|
+
|
|
10
|
+
# 環境變數(生產環境)
|
|
11
|
+
[env.production]
|
|
12
|
+
|
|
13
|
+
{{#isKV}}
|
|
14
|
+
# KV 存儲綁定
|
|
15
|
+
[[kv_namespaces]]
|
|
16
|
+
binding = "CACHE"
|
|
17
|
+
id = "your-kv-namespace-id"
|
|
18
|
+
{{/isKV}}
|
|
19
|
+
|
|
20
|
+
{{#isD1}}
|
|
21
|
+
# D1 資料庫綁定
|
|
22
|
+
[[d1_databases]]
|
|
23
|
+
binding = "DB"
|
|
24
|
+
database_name = "{{projectName}}-db"
|
|
25
|
+
database_id = "your-d1-database-id"
|
|
26
|
+
{{/isD1}}
|
|
27
|
+
|
|
28
|
+
{{#isR2}}
|
|
29
|
+
# R2 存儲桶綁定
|
|
30
|
+
[[r2_buckets]]
|
|
31
|
+
binding = "BUCKET"
|
|
32
|
+
bucket_name = "{{projectName}}-bucket"
|
|
33
|
+
{{/isR2}}
|
|
34
|
+
|
|
35
|
+
[vars]
|
|
36
|
+
ENVIRONMENT = "production"
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# {{projectName}}
|
|
2
|
+
|
|
3
|
+
{{description}}
|
|
4
|
+
|
|
5
|
+
這是一個使用 Hono 框架的 Cloudflare Worker 專案,使用 create-cf-project 腳手架工具建立。
|
|
6
|
+
|
|
7
|
+
## 快速開始
|
|
8
|
+
|
|
9
|
+
### 安裝依賴
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
### 開發模式
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm run dev
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
這會啟動 wrangler dev 伺服器,你可以在本地測試 Worker。
|
|
22
|
+
|
|
23
|
+
### 部署
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm run deploy
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## 專案結構
|
|
30
|
+
|
|
31
|
+
```
|
|
32
|
+
{{projectName}}/
|
|
33
|
+
├── src/
|
|
34
|
+
│ ├── index.js # Worker 入口檔案
|
|
35
|
+
│ └── routes/
|
|
36
|
+
│ └── index.js # 路由定義
|
|
37
|
+
├── package.json # 專案配置
|
|
38
|
+
├── wrangler.toml # Cloudflare 配置
|
|
39
|
+
└── README.md # 本文件
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## API 端點
|
|
43
|
+
|
|
44
|
+
- `GET /` - 首頁
|
|
45
|
+
- `GET /api/health` - 健康檢查
|
|
46
|
+
- `GET /api/hello/:name` - 打招呼
|
|
47
|
+
- `POST /api/echo` - 回傳請求內容
|
|
48
|
+
|
|
49
|
+
## 技術細節
|
|
50
|
+
|
|
51
|
+
- **平台**: Cloudflare Workers
|
|
52
|
+
- **框架**: [Hono](https://hono.dev/)
|
|
53
|
+
- **語言**: JavaScript (ES Module)
|
|
54
|
+
- **執行環境**: Edge Runtime
|
|
55
|
+
|
|
56
|
+
## Hono 特性
|
|
57
|
+
|
|
58
|
+
- 🚀 超快 - 使用 RegExpRouter 進行路由匹配
|
|
59
|
+
- 🎯 簡潔 - 內建中介層、驗證等功能
|
|
60
|
+
- 🌍 標準 - 基於 Web 標準
|
|
61
|
+
- 💾 多運行時支援 - Cloudflare Workers、Deno、Bun、Node.js 等
|
|
62
|
+
|
|
63
|
+
## 授權
|
|
64
|
+
|
|
65
|
+
{{#license}}{{license}}{{/license}}{{^license}}MIT{{/license}}
|
|
66
|
+
|
|
67
|
+
建立於 {{date}} | {{year}}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "{{projectName}}",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "{{description}}",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"dev": "wrangler dev",
|
|
9
|
+
"deploy": "wrangler deploy",
|
|
10
|
+
"lint": "eslint src/**/*.js"
|
|
11
|
+
},
|
|
12
|
+
"keywords": [
|
|
13
|
+
"cloudflare",
|
|
14
|
+
"worker",
|
|
15
|
+
"hono",
|
|
16
|
+
"edge",
|
|
17
|
+
"javascript"
|
|
18
|
+
],
|
|
19
|
+
"author": "{{#author}}{{author}}{{/author}}{{^author}}Anonymous{{/author}}",
|
|
20
|
+
"license": "{{#license}}{{license}}{{/license}}{{^license}}MIT{{/license}}",
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"hono": "^4.2.0"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@cloudflare/workers-types": "^4.20240403.0",
|
|
26
|
+
"eslint": "^8.57.0",
|
|
27
|
+
"wrangler": "^3.39.0"
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hono Cloudflare Worker 入口檔案(JavaScript 版本)
|
|
3
|
+
* {{projectName}} - {{description}}
|
|
4
|
+
* 建立日期:{{date}}
|
|
5
|
+
*
|
|
6
|
+
* 使用 Hono 框架進行路由管理
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { Hono } from 'hono';
|
|
10
|
+
import { poweredBy } from 'hono/powered-by';
|
|
11
|
+
import { logger as honoLogger } from 'hono/logger';
|
|
12
|
+
import { cors } from 'hono/cors';
|
|
13
|
+
import { routes } from './routes/index.js';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 創建 Hono 應用
|
|
17
|
+
* @type {Hono}
|
|
18
|
+
*/
|
|
19
|
+
const app = new Hono();
|
|
20
|
+
|
|
21
|
+
// 中介層
|
|
22
|
+
app.use('*', poweredBy());
|
|
23
|
+
app.use('*', honoLogger());
|
|
24
|
+
app.use('*', cors({
|
|
25
|
+
origin: '*',
|
|
26
|
+
allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
|
|
27
|
+
allowHeaders: ['Content-Type', 'Authorization'],
|
|
28
|
+
}));
|
|
29
|
+
|
|
30
|
+
// 註冊路由
|
|
31
|
+
app.route('/', routes);
|
|
32
|
+
|
|
33
|
+
// 錯誤處理
|
|
34
|
+
app.onError((err, c) => {
|
|
35
|
+
console.error('應用錯誤:', err);
|
|
36
|
+
return c.json(
|
|
37
|
+
{
|
|
38
|
+
error: 'Internal Server Error',
|
|
39
|
+
message: err.message,
|
|
40
|
+
},
|
|
41
|
+
500
|
|
42
|
+
);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// 404 處理
|
|
46
|
+
app.notFound((c) => {
|
|
47
|
+
return c.json(
|
|
48
|
+
{
|
|
49
|
+
error: 'Not Found',
|
|
50
|
+
},
|
|
51
|
+
404
|
|
52
|
+
);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
export default app;
|