koishi-plugin-minecraft-notifier 1.2.2 → 1.2.3

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.
@@ -1,17 +1,16 @@
1
1
  import { AxiosResponse } from 'axios';
2
2
  /**
3
- * 更新 Gitee 仓库中的文本文件
3
+ * 上传或更新 Gitee 仓库中的文本文件(如果不存在则创建,如果存在则更新)
4
4
  * @param owner - 仓库所有者用户名
5
5
  * @param repo - 仓库名称
6
6
  * @param path - 文件路径(例如 'docs/update.txt')
7
7
  * @param content - 文件内容(纯文本字符串,会自动 Base64 编码)
8
- * @param sha - 文件的当前 SHA 值(通过 GET contents API 获取,用于更新)
9
8
  * @param message - Commit 消息
10
9
  * @param token - Gitee Personal Access Token
11
10
  * @param branch - 分支名(默认 'master')
12
11
  * @returns Promise<{ success: boolean; data?: AxiosResponse; error?: string }>
13
12
  */
14
- export declare function updateFileOnGitee(owner: string, repo: string, path: string, content: string, sha: string, message: string, token: string, branch?: string): Promise<{
13
+ export declare function upsertFileToGitee(owner: string, repo: string, path: string, content: string, message: string, token: string, branch?: string): Promise<{
15
14
  success: boolean;
16
15
  data?: AxiosResponse;
17
16
  error?: string;
package/lib/index.cjs CHANGED
@@ -123,36 +123,92 @@ var import_node_path = __toESM(require("node:path"), 1);
123
123
 
124
124
  // src/gitee-helper.ts
125
125
  var import_axios = __toESM(require("axios"), 1);
126
- async function updateFileOnGitee(owner, repo, path3, content, sha, message, token, branch = "master") {
126
+ async function upsertFileToGitee(owner, repo, path3, content, message, token, branch = "master") {
127
127
  const base64Content = Buffer.from(content, "utf-8").toString("base64");
128
- const url = `https://gitee.com/api/v5/repos/${owner}/${repo}/contents/${path3}`;
128
+ const checkUrl = `https://gitee.com/api/v5/repos/${owner}/${repo}/contents/${path3}?ref=${branch}`;
129
+ let fileSha = null;
129
130
  try {
130
- const response = await import_axios.default.put(
131
- url,
132
- {
133
- content: base64Content,
134
- sha,
135
- message,
136
- branch
137
- },
138
- {
139
- headers: {
140
- Authorization: `token ${token}`,
141
- "Content-Type": "application/json"
142
- }
131
+ const checkResponse = await import_axios.default.get(checkUrl, {
132
+ headers: {
133
+ Authorization: `token ${token}`
143
134
  }
144
- );
145
- return { success: true, data: response };
146
- } catch (error) {
147
- console.error(
148
- "Gitee Update Error:",
149
- error.response?.data || error.message
150
- );
151
- return {
152
- success: false,
153
- error: error.response?.data?.message || error.message
154
- };
135
+ });
136
+ if (checkResponse.status === 200) {
137
+ fileSha = checkResponse.data.sha;
138
+ } else {
139
+ throw new Error(`Unexpected status: ${checkResponse.status}`);
140
+ }
141
+ } catch (checkError) {
142
+ if (checkError.response?.status === 404) {
143
+ const createUrl = `https://gitee.com/api/v5/repos/${owner}/${repo}/contents/${path3}`;
144
+ try {
145
+ const createResponse = await import_axios.default.post(
146
+ createUrl,
147
+ {
148
+ content: base64Content,
149
+ message,
150
+ branch
151
+ },
152
+ {
153
+ headers: {
154
+ Authorization: `token ${token}`,
155
+ "Content-Type": "application/json"
156
+ }
157
+ }
158
+ );
159
+ return { success: true, data: createResponse };
160
+ } catch (createError) {
161
+ console.error(
162
+ "Gitee Create Error:",
163
+ createError.response?.data || createError.message
164
+ );
165
+ return {
166
+ success: false,
167
+ error: createError.response?.data?.message || createError.message
168
+ };
169
+ }
170
+ } else {
171
+ console.error(
172
+ "Gitee Check Error:",
173
+ checkError.response?.data || checkError.message
174
+ );
175
+ return {
176
+ success: false,
177
+ error: checkError.response?.data?.message || checkError.message
178
+ };
179
+ }
180
+ }
181
+ if (fileSha) {
182
+ const updateUrl = `https://gitee.com/api/v5/repos/${owner}/${repo}/contents/${path3}`;
183
+ try {
184
+ const updateResponse = await import_axios.default.put(
185
+ updateUrl,
186
+ {
187
+ content: base64Content,
188
+ sha: fileSha,
189
+ message,
190
+ branch
191
+ },
192
+ {
193
+ headers: {
194
+ Authorization: `token ${token}`,
195
+ "Content-Type": "application/json"
196
+ }
197
+ }
198
+ );
199
+ return { success: true, data: updateResponse };
200
+ } catch (updateError) {
201
+ console.error(
202
+ "Gitee Update Error:",
203
+ updateError.response?.data || updateError.message
204
+ );
205
+ return {
206
+ success: false,
207
+ error: updateError.response?.data?.message || updateError.message
208
+ };
209
+ }
155
210
  }
211
+ return { success: false, error: "Failed to determine file existence" };
156
212
  }
157
213
 
158
214
  // src/xaml-generator.ts
@@ -293,7 +349,7 @@ async function exportXaml(ctx, cfg, summary, version) {
293
349
  await import_node_fs.promises.writeFile(fullXamlPath, xaml);
294
350
  await import_node_fs.promises.copyFile(fullXamlPath, fullHomePagePath);
295
351
  if (cfg.giteeApiToken && cfg.giteeOwner && cfg.giteeRepo) {
296
- await updateFileOnGitee(
352
+ await upsertFileToGitee(
297
353
  cfg.giteeOwner,
298
354
  cfg.giteeRepo,
299
355
  "Custom.xaml",
@@ -615,9 +671,9 @@ var Config = import_koishi.Schema.object({
615
671
  checkInterval: import_koishi.Schema.number().default(3).description("\u5728\u7EBF\u72B6\u6001\u68C0\u67E5\u95F4\u9694\uFF08\u5206\u949F\uFF09"),
616
672
  baseApiUrl: import_koishi.Schema.string().default("https://api.openai.com/v1").description("AI \u63A5\u53E3\u7684\u57FA\u7840 URL"),
617
673
  model: import_koishi.Schema.string().default("gpt-5").description("\u4F7F\u7528\u7684 AI \u6A21\u578B"),
618
- apiKey: import_koishi.Schema.string().default("").description("AI \u63A5\u53E3\u7684 API \u5BC6\u94A5").required(),
674
+ apiKey: import_koishi.Schema.string().role("secret").default("").description("AI \u63A5\u53E3\u7684 API \u5BC6\u94A5").required(),
619
675
  notifyChannel: import_koishi.Schema.array(String).default([]).description("\u7528\u4E8E\u63A5\u6536\u66F4\u65B0\u901A\u77E5\u7684\u9891\u9053 ID \u5217\u8868"),
620
- giteeApiToken: import_koishi.Schema.string().default("").description("Gitee API \u8BBF\u95EE\u4EE4\u724C\uFF0C\u7528\u4E8E\u4E0A\u4F20 XAML \u6587\u4EF6"),
676
+ giteeApiToken: import_koishi.Schema.string().role("secret").default("").description("Gitee API \u8BBF\u95EE\u4EE4\u724C\uFF0C\u7528\u4E8E\u4E0A\u4F20 XAML \u6587\u4EF6"),
621
677
  giteeOwner: import_koishi.Schema.string().default("").description("Gitee \u4ED3\u5E93\u6240\u6709\u8005\u7528\u6237\u540D"),
622
678
  giteeRepo: import_koishi.Schema.string().default("").description("Gitee \u4ED3\u5E93\u540D\u79F0")
623
679
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-minecraft-notifier",
3
3
  "description": "A Minecraft new version notification plugin, also featuring a PCL homepage.",
4
- "version": "1.2.2",
4
+ "version": "1.2.3",
5
5
  "main": "lib/index.cjs",
6
6
  "typings": "lib/index.d.ts",
7
7
  "type": "module",