-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
223 lines (190 loc) · 7.44 KB
/
Copy pathindex.js
File metadata and controls
223 lines (190 loc) · 7.44 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
#!/usr/bin/env node
const fs = require('fs-extra');
const path = require('path');
const chalk = require('chalk');
const ora = require('ora');
const inquirer = require('inquirer');
const os = require('os');
const { execSync } = require('child_process');
// 汉化资源路径 (随项目分发)
const SOURCE_UI_PATH_V1 = path.join(__dirname, 'resources', 'control-ui');
const SOURCE_UI_PATH_V2 = path.join(__dirname, 'resources', 'control-ui-openclaw');
async function main() {
console.log(chalk.cyan('🚀 Moltbot/Clawdbot 中文 WebUI 离线安装工具'));
console.log(chalk.gray('无需网络下载,直接替换本地文件'));
console.log();
// 0. 检查资源文件
const hasV1 = fs.existsSync(SOURCE_UI_PATH_V1);
const hasV2 = fs.existsSync(SOURCE_UI_PATH_V2);
if (!hasV1 && !hasV2) {
console.error(chalk.red('❌ 严重错误: 未找到内置的汉化资源文件。'));
console.error(chalk.yellow('请确保您下载了完整的安装包,并且 resources 文件夹存在。'));
process.exit(1);
}
// 选择版本
let selectedSourcePath = SOURCE_UI_PATH_V1;
if (hasV1 && hasV2) {
const { version } = await inquirer.prompt([
{
type: 'list',
name: 'version',
message: '请选择汉化版本:',
choices: [
{ name: 'Moltbot-cn (旧版)', value: 'v1' },
{ name: 'OpenClaw-cn (推荐, 汉化更全)', value: 'v2' }
],
default: 'v2'
}
]);
selectedSourcePath = version === 'v2' ? SOURCE_UI_PATH_V2 : SOURCE_UI_PATH_V1;
} else if (hasV2) {
selectedSourcePath = SOURCE_UI_PATH_V2;
}
try {
// 1. 查找安装目录
let targetDir = await findMoltbotInstallation();
// 如果自动查找失败,或者用户想要确认/修改
if (!targetDir) {
console.log(chalk.yellow('⚠️ 自动检测未找到 clawdbot 或 moltbot 的安装位置。'));
const answer = await inquirer.prompt([
{
type: 'input',
name: 'manualPath',
message: '请输入安装目录路径 (包含 dist/control-ui 的父目录):',
validate: (input) => {
if (!input) return '路径不能为空';
// 允许用户输入根目录,我们自动检查子目录
if (fs.existsSync(path.join(input, 'dist', 'control-ui')) ||
fs.existsSync(path.join(input, 'control-ui')) ||
fs.existsSync(input)) {
return true;
}
return '路径无效或未找到 control-ui 目录';
}
}
]);
targetDir = answer.manualPath;
}
// 规范化路径:确保指向包含 dist/control-ui 的根目录
// 比如用户输入了 /opt/clawdbot/dist/control-ui,我们要定位到 /opt/clawdbot
if (targetDir.endsWith('control-ui')) {
if (targetDir.endsWith('dist/control-ui') || targetDir.endsWith('dist\\control-ui')) {
targetDir = path.resolve(targetDir, '../..');
} else {
targetDir = path.resolve(targetDir, '..'); // 假设是直接的 control-ui
}
}
const targetUiPath = path.join(targetDir, 'dist', 'control-ui');
// 二次验证
if (!fs.existsSync(targetUiPath)) {
// 尝试创建一个 mock 结构用于测试?不,生产环境直接报错
// 除非文件夹不存在但 dist 存在
if (!fs.existsSync(path.join(targetDir, 'dist'))) {
console.log(chalk.red(`❌ 路径验证失败: ${targetUiPath} 不存在。`));
return;
}
}
console.log(chalk.blue(`📍 目标安装路径: ${targetDir}`));
console.log(chalk.blue(`📂 UI 目标路径: ${targetUiPath}`));
// 2. 确认安装
const { confirm } = await inquirer.prompt([
{
type: 'confirm',
name: 'confirm',
message: '确认开始安装汉化包?(将备份原文件)',
default: true
}
]);
if (!confirm) {
console.log(chalk.yellow('操作已取消'));
return;
}
// 3. 执行替换
const spinner = ora('正在安装...').start();
try {
// 备份
const backupPath = path.join(targetDir, 'dist', `control-ui-backup-${Date.now()}`);
if (fs.existsSync(targetUiPath)) {
spinner.text = '正在备份原文件...';
await fs.move(targetUiPath, backupPath);
} else {
// 如果目标不存在,确保父目录存在
await fs.ensureDir(path.join(targetDir, 'dist'));
}
// 复制
spinner.text = '正在部署汉化文件...';
await fs.copy(selectedSourcePath, targetUiPath);
spinner.succeed('安装完成!');
console.log();
console.log(chalk.green('✅ 汉化包已成功应用。'));
if (fs.existsSync(backupPath)) {
console.log(chalk.gray(`💾 原文件已备份至: ${backupPath}`));
}
console.log();
console.log(chalk.yellow('请重启服务以生效:'));
console.log(chalk.white(' clawdbot gateway restart'));
console.log();
} catch (err) {
spinner.fail('安装失败');
console.error(err);
// 尝试回滚
// if (fs.existsSync(backupPath)) ...
}
} catch (error) {
console.error(chalk.red('\n发生意外错误:'), error);
}
}
async function findMoltbotInstallation() {
const candidates = [];
// 1. 检查环境变量
if (process.env.CLAWDBOT_DIR) candidates.push(process.env.CLAWDBOT_DIR);
// 2. 检查 npm 全局安装路径
try {
const npmRoot = execSync('npm root -g', { encoding: 'utf8' }).trim();
if (npmRoot) {
candidates.push(path.join(npmRoot, 'clawdbot'));
candidates.push(path.join(npmRoot, 'moltbot'));
}
} catch (e) {}
// 3. 常见系统路径
const commonPaths = [
'/usr/local/lib/node_modules/clawdbot',
'/usr/lib/node_modules/clawdbot',
'/opt/clawdbot',
'/usr/local/lib/node_modules/moltbot',
'/usr/lib/node_modules/moltbot',
'/opt/moltbot',
path.join(os.homedir(), '.clawdbot'),
path.join(os.homedir(), '.moltbot'),
// Windows paths
process.env.APPDATA ? path.join(process.env.APPDATA, 'npm', 'node_modules', 'clawdbot') : null,
process.env.APPDATA ? path.join(process.env.APPDATA, 'npm', 'node_modules', 'moltbot') : null
].filter(Boolean);
candidates.push(...commonPaths);
// 4. 检查所有候选路径
for (const p of candidates) {
if (await isValidInstall(p)) {
return p;
}
}
// 5. 如果还没找到,尝试搜索(深度有限)
// 这里为了性能暂时不开启全盘搜索,但在 Windows 下可以尝试搜索 npm 目录
return null;
}
async function isValidInstall(dir) {
try {
// 只要 dist 目录存在,我们就认为它是有效的安装目录(即使 control-ui 不存在,我们可以创建)
// 或者如果有 package.json 且包含相关关键字
const hasDist = await fs.pathExists(path.join(dir, 'dist'));
if (hasDist) return true;
const pkgPath = path.join(dir, 'package.json');
if (await fs.pathExists(pkgPath)) {
const pkg = await fs.readJson(pkgPath);
return pkg.name === 'clawdbot' || pkg.name === 'moltbot';
}
return false;
} catch (e) {
return false;
}
}
main();