-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdeploy-service.js
135 lines (117 loc) · 3.69 KB
/
deploy-service.js
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
import fs from 'fs/promises'
import { exec } from 'child_process'
import { promisify } from 'util'
import readline from 'readline'
import path from 'path'
const execAsync = promisify(exec)
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
})
const question = (query) => new Promise((resolve) => rl.question(query, resolve))
async function checkNodeModules() {
try {
await fs.access('./service/node_modules')
return true
} catch {
return false
}
}
async function execCommand(command, ignoreErrors = false) {
try {
const { stdout, stderr } = await execAsync(command, { encoding: 'utf8' })
if (stdout) console.log(stdout)
if (stderr) {
console.log(stderr)
}
} catch (error) {
if (!ignoreErrors) {
throw new Error(`命令执行失败: ${error.message}`)
} else {
console.log('⚠️ ', error.message)
}
}
}
async function checkWrangler() {
try {
await execAsync('npx wrangler --version')
return true
} catch {
return false
}
}
async function deployService() {
try {
// 检查并安装 wrangler
const hasWrangler = await checkWrangler()
if (!hasWrangler) {
console.log('📦 正在安装 wrangler...')
await execCommand('npm install -D wrangler')
console.log('✅ wrangler 安装完成')
}
// 检查并安装依赖
const hasNodeModules = await checkNodeModules()
if (!hasNodeModules) {
console.log('📦 正在安装 service 依赖...')
await execCommand('cd service && npm install')
console.log('✅ 依赖安装完成')
}
// 获取用户输入
const databaseName = await question('请输入 D1 数据库名称: ')
const databaseId = await question('请输入 D1 数据库 ID: ')
const kvNamespaceId = await question('请输入 KV 命名空间 ID: ')
const jwtSecret = await question('请输入 JWT Secret: ')
// 读取 wrangler.toml.example 文件
let wranglerContent = await fs.readFile('./service/wrangler.toml.example', 'utf8')
// 替换配置值
wranglerContent = wranglerContent
// 替换数据库配置
.replace(
/database_name = ".*?"/m,
`database_name = "${databaseName}"`
)
.replace(
/database_id = ".*?"/m,
`database_id = "${databaseId}"`
)
// 替换 KV 配置
.replace(
/binding = "CYI_IMGKV"\s*\nid = ".*?"/m,
`binding = "CYI_IMGKV"\nid = "${kvNamespaceId}"`
)
// 替换 JWT 配置
.replace(
/JWT_SECRET = ".*?"/m,
`JWT_SECRET = "${jwtSecret}"`
)
// 打印配置内容以便验证
// console.log('配置文件内容:', wranglerContent)
// 创建 wrangler.toml 文件
await fs.writeFile('./service/wrangler.toml', wranglerContent)
console.log('✅ 已创建 wrangler.toml 文件')
// 部署 Worker
console.log('🚀 开始部署 Worker...')
try {
// 切换到 service 目录
process.chdir('./service')
console.log('📍 当前目录:', process.cwd())
// 使用 ignoreErrors = true 执行部署命令
await execCommand('npx wrangler deploy', true)
// 检查部署是否成功(可以通过检查输出中是否包含特定字符串)
console.log('✅ Worker 部署完成')
} catch (error) {
if (error.message.includes('fetch failed')) {
console.error('❌ 部署失败:请确保已经运行 "npx wrangler login" 并完成 Cloudflare 登录')
} else {
throw error
}
} finally {
process.chdir('..') // 切换回原目录
}
} catch (error) {
console.error('❌ 部署过程中出现错误:', error)
} finally {
rl.close()
}
}
deployService()