forked from linqiu919/augment-open-patch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch-once.ts
More file actions
183 lines (150 loc) · 4.83 KB
/
Copy pathpatch-once.ts
File metadata and controls
183 lines (150 loc) · 4.83 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
import dotenv from "dotenv";
import { patchExtension, getExtensionVersions } from "./augmentExt";
import { TelegramPusher, TelegramConfig } from "./telegram";
// 加载环境变量
dotenv.config();
interface PatchOnceConfig {
telegramBotToken: string;
telegramChatId: string;
publisher: string;
extension: string;
workDir: string;
}
class PatchOnceProcessor {
private config: PatchOnceConfig;
private telegramPusher: TelegramPusher;
constructor() {
this.config = this.loadConfig();
const telegramConfig: TelegramConfig = {
botToken: this.config.telegramBotToken,
chatId: this.config.telegramChatId
};
this.telegramPusher = new TelegramPusher(telegramConfig);
}
private loadConfig(): PatchOnceConfig {
const requiredEnvVars = ['TELEGRAM_BOT_TOKEN', 'TELEGRAM_CHAT_ID'];
for (const envVar of requiredEnvVars) {
if (!process.env[envVar]) {
throw new Error(`缺少必需的环境变量: ${envVar}`);
}
}
return {
telegramBotToken: process.env.TELEGRAM_BOT_TOKEN!,
telegramChatId: process.env.TELEGRAM_CHAT_ID!,
publisher: process.env.PUBLISHER || "augment",
extension: process.env.EXTENSION || "vscode-augment",
workDir: process.env.WORK_DIR || "./augment-plugins"
};
}
/**
* 处理指定版本
*/
async processVersion(version: string): Promise<boolean> {
try {
console.log(`🔧 开始处理指定版本: ${version}...`);
// 验证版本号格式
if (!/^\d+\.\d+\.\d+$/.test(version)) {
throw new Error(`无效的版本号格式: ${version},请使用 x.x.x 格式(如 0.688.0)`);
}
// 调用patch函数处理指定版本
const patchedFilePath = await patchExtension(version, true);
if (!patchedFilePath) {
throw new Error("Patch处理失败");
}
console.log(`✅ Patch完成: ${patchedFilePath}`);
// 推送文件到Telegram
const pushMessage = {
version: version,
filePath: patchedFilePath,
pushTime: new Date().toLocaleString('zh-CN'),
changelog: "手动指定版本处理"
};
console.log(`📤 正在推送到Telegram...`);
const pushSuccess = await this.telegramPusher.pushFile(pushMessage);
if (pushSuccess) {
console.log(`✅ 版本 ${version} 处理完成并推送成功`);
return true;
} else {
throw new Error("推送到Telegram失败");
}
} catch (error) {
console.error(`❌ 处理版本 ${version} 时出错:`, error);
await this.telegramPusher.sendErrorNotification(
error instanceof Error ? error.message : String(error),
version
);
return false;
}
}
/**
* 获取并处理最新版本(一次性)
*/
async processLatestVersion(): Promise<boolean> {
try {
console.log(`🔍 获取 ${this.config.publisher}.${this.config.extension} 的最新版本...`);
const versions = await getExtensionVersions(
this.config.publisher,
this.config.extension,
1
);
if (versions.length === 0) {
console.log("❌ 无法获取版本信息");
return false;
}
const latestVersion = versions[0].version;
console.log(`📦 最新版本: ${latestVersion}`);
return await this.processVersion(latestVersion);
} catch (error) {
console.error("❌ 获取最新版本时出错:", error);
return false;
}
}
/**
* 测试Telegram连接
*/
async testConnection(): Promise<boolean> {
return await this.telegramPusher.testConnection();
}
}
// 主函数
async function main() {
const args = process.argv.slice(2);
if (args.length === 0) {
console.log("用法:");
console.log(" 处理指定版本: npx tsx patch-once.ts <version>");
console.log(" 处理最新版本: npx tsx patch-once.ts --latest");
console.log("");
console.log("示例:");
console.log(" npx tsx patch-once.ts 0.688.0");
console.log(" npx tsx patch-once.ts --latest");
process.exit(1);
}
try {
const processor = new PatchOnceProcessor();
// 测试Telegram连接
const telegramOk = await processor.testConnection();
if (!telegramOk) {
throw new Error("Telegram机器人连接失败,请检查配置");
}
let success = false;
if (args[0] === "--latest") {
// 获取并处理最新版本
success = await processor.processLatestVersion();
} else {
// 处理指定版本
const version = args[0];
success = await processor.processVersion(version);
}
if (success) {
console.log("🎉 处理完成!");
process.exit(0);
} else {
console.log("❌ 处理失败");
process.exit(1);
}
} catch (error) {
console.error("❌ 执行失败:", error);
process.exit(1);
}
}
main();