semantic-release-minecraft 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.
Files changed (42) hide show
  1. package/.github/workflows/release.yml +43 -0
  2. package/.prettierrc +18 -0
  3. package/.releaserc.json +51 -0
  4. package/CHANGELOG.md +10 -0
  5. package/dist/curseforge.d.ts +4 -0
  6. package/dist/curseforge.d.ts.map +1 -0
  7. package/dist/curseforge.js +131 -0
  8. package/dist/curseforge.js.map +1 -0
  9. package/dist/definitions/curseforge.d.ts +40 -0
  10. package/dist/definitions/curseforge.d.ts.map +1 -0
  11. package/dist/definitions/curseforge.js +6 -0
  12. package/dist/definitions/curseforge.js.map +1 -0
  13. package/dist/definitions/plugin_config.d.ts +49 -0
  14. package/dist/definitions/plugin_config.d.ts.map +1 -0
  15. package/dist/definitions/plugin_config.js +2 -0
  16. package/dist/definitions/plugin_config.js.map +1 -0
  17. package/dist/index.d.ts +8 -0
  18. package/dist/index.d.ts.map +1 -0
  19. package/dist/index.js +60 -0
  20. package/dist/index.js.map +1 -0
  21. package/dist/modrinth.d.ts +7 -0
  22. package/dist/modrinth.d.ts.map +1 -0
  23. package/dist/modrinth.js +238 -0
  24. package/dist/modrinth.js.map +1 -0
  25. package/dist/prepare.d.ts +11 -0
  26. package/dist/prepare.d.ts.map +1 -0
  27. package/dist/prepare.js +88 -0
  28. package/dist/prepare.js.map +1 -0
  29. package/dist/utils/utils.d.ts +29 -0
  30. package/dist/utils/utils.d.ts.map +1 -0
  31. package/dist/utils/utils.js +92 -0
  32. package/dist/utils/utils.js.map +1 -0
  33. package/eslint.config.js +22 -0
  34. package/package.json +49 -0
  35. package/src/curseforge.ts +192 -0
  36. package/src/definitions/curseforge.ts +51 -0
  37. package/src/definitions/plugin_config.ts +68 -0
  38. package/src/index.ts +98 -0
  39. package/src/modrinth.ts +316 -0
  40. package/src/prepare.ts +199 -0
  41. package/src/utils/utils.ts +120 -0
  42. package/tsconfig.json +25 -0
@@ -0,0 +1,316 @@
1
+ import axios from 'axios';
2
+ import FormData from 'form-data';
3
+ import { readFile } from 'fs/promises';
4
+ import { glob } from 'glob';
5
+ import { template } from 'lodash';
6
+ import { resolve } from 'path';
7
+ import { PublishContext } from 'semantic-release';
8
+ import { Plugin_config } from './definitions/plugin_config';
9
+ import {
10
+ findFiles,
11
+ renderTemplates,
12
+ resolveTemplate,
13
+ toArray,
14
+ } from './utils/utils';
15
+
16
+ /**
17
+ * 发布到 Modrinth
18
+ */
19
+ export async function publishToModrinth(
20
+ pluginConfig: Plugin_config,
21
+ context: PublishContext
22
+ ): Promise<string> {
23
+ const { env, logger } = context;
24
+ const { modrinth } = pluginConfig;
25
+ const token = env.MODRINTH_TOKEN!;
26
+ const projectId = modrinth?.project_id!;
27
+ const nextRelease = context.nextRelease;
28
+ const nextReleaseVersion = nextRelease.version;
29
+
30
+ // 查找文件并确定主要文件
31
+ const { files, primaryFile } = await findModrinthJarFiles(
32
+ pluginConfig,
33
+ context
34
+ );
35
+ logger.log(
36
+ `Publishing ${files.length} file(s) to Modrinth project ${projectId}...`
37
+ );
38
+
39
+ // 使用 multipart/form-data 方式上传文件和版本信息
40
+ const form = new FormData();
41
+ const filePartNames: string[] = [];
42
+ let primaryFilePartName: string | undefined = undefined;
43
+
44
+ // 上传所有文件
45
+ for (let i = 0; i < files.length; i++) {
46
+ const jarPath = files[i];
47
+ const jarFile = await readFile(jarPath);
48
+ const fileName = jarPath.split('\\').pop() || `mod-${i}.jar`;
49
+ const filePartName = `file-${i}`;
50
+
51
+ form.append(filePartName, jarFile, { filename: fileName });
52
+ filePartNames.push(filePartName);
53
+
54
+ // 标记主要文件
55
+ if (jarPath === primaryFile) {
56
+ primaryFilePartName = filePartName;
57
+ }
58
+ }
59
+
60
+ // 使用根据primaryFileGlob确定的主要文件
61
+ const finalPrimaryFile: string | undefined = primaryFilePartName;
62
+
63
+ // 准备版本信息,只包含必需字段和存在的可选字段
64
+ // displayName按照优先级:平台特定配置 > 全局配置 > 平台特定环境变量 > 全局环境变量
65
+ let displayName: string | undefined;
66
+ if (modrinth?.display_name) {
67
+ displayName = template(modrinth.display_name)(nextRelease) as string;
68
+ } else if (pluginConfig.display_name) {
69
+ displayName = template(pluginConfig.display_name)(
70
+ nextRelease
71
+ ) as string;
72
+ } else if (env.MODRINTH_DISPLAY_NAME) {
73
+ displayName = env.MODRINTH_DISPLAY_NAME;
74
+ } else if (env.DISPLAY_NAME) {
75
+ displayName = env.DISPLAY_NAME;
76
+ }
77
+
78
+ // version_number 按照优先级:平台特定配置 > 平台特定环境变量 > 全局环境变量
79
+ const versionNumber = resolveTemplate(
80
+ [modrinth?.version_number, env.MODRINTH_VERSION_NUMBER],
81
+ nextRelease
82
+ );
83
+
84
+ // 准备版本信息,只包含必需字段和存在的可选字段
85
+ const versionData: any = {
86
+ project_id: projectId,
87
+ file_parts: filePartNames, // 必需字段,列出所有文件部分的名称
88
+ };
89
+
90
+ // 只有找到值时才添加 name 字段
91
+ if (displayName) {
92
+ versionData.name = displayName;
93
+ } else {
94
+ // 如果没有找到任何配置,使用默认值
95
+ versionData.name = context.nextRelease.name;
96
+ }
97
+
98
+ // 只有找到值时才添加 version_number 字段
99
+ if (versionNumber) {
100
+ versionData.version_number = versionNumber;
101
+ } else {
102
+ // 如果没有找到任何配置,使用默认值
103
+ versionData.version_number = nextReleaseVersion;
104
+ }
105
+
106
+ // 只添加存在的可选字段
107
+ if (modrinth?.changelog || context.nextRelease?.notes) {
108
+ versionData.changelog =
109
+ modrinth?.changelog || context.nextRelease?.notes || '';
110
+ }
111
+
112
+ if (modrinth?.game_versions && modrinth.game_versions.length > 0) {
113
+ versionData.game_versions = renderTemplates(modrinth.game_versions, {
114
+ nextRelease,
115
+ }) as string[];
116
+ } else if (
117
+ pluginConfig.game_versions &&
118
+ pluginConfig.game_versions.length > 0
119
+ ) {
120
+ versionData.game_versions = renderTemplates(
121
+ toArray(pluginConfig.game_versions),
122
+ {
123
+ nextRelease,
124
+ }
125
+ ) as string[];
126
+ }
127
+
128
+ // modLoaders 按照优先级:平台特定配置 > 全局配置 > 平台特定环境变量 > 全局环境变量
129
+ let modLoaders: string[] | undefined;
130
+ if (modrinth?.mod_loaders && modrinth.mod_loaders.length > 0) {
131
+ modLoaders = renderTemplates(modrinth.mod_loaders, {
132
+ nextRelease,
133
+ }) as string[];
134
+ } else if (
135
+ pluginConfig.mod_loaders &&
136
+ pluginConfig.mod_loaders.length > 0
137
+ ) {
138
+ modLoaders = renderTemplates(toArray(pluginConfig.mod_loaders), {
139
+ nextRelease,
140
+ }) as string[];
141
+ } else if (env.MODRINTH_MOD_LOADERS) {
142
+ modLoaders = env.MODRINTH_MOD_LOADERS.split(',').map((s) => s.trim());
143
+ } else if (env.MOD_LOADERS) {
144
+ modLoaders = env.MOD_LOADERS.split(',').map((s) => s.trim());
145
+ }
146
+
147
+ if (modLoaders) {
148
+ versionData.loaders = modLoaders;
149
+ }
150
+
151
+ if (pluginConfig.release_type) {
152
+ versionData.version_type = pluginConfig.release_type;
153
+ }
154
+
155
+ if (modrinth?.dependencies && modrinth.dependencies.length > 0) {
156
+ versionData.dependencies = modrinth.dependencies;
157
+ }
158
+
159
+ if (modrinth?.featured !== undefined) {
160
+ versionData.featured = modrinth.featured;
161
+ }
162
+
163
+ if (modrinth?.status) {
164
+ versionData.status = modrinth.status;
165
+ }
166
+
167
+ if (modrinth?.requested_status) {
168
+ versionData.requested_status = modrinth.requested_status;
169
+ }
170
+
171
+ // 只有当 finalPrimaryFile 存在时才添加 primary_file 字段
172
+ if (finalPrimaryFile) {
173
+ versionData.primary_file = finalPrimaryFile;
174
+ }
175
+
176
+ // 添加版本信息作为 JSON
177
+ form.append('data', JSON.stringify(versionData));
178
+
179
+ // 发送版本创建请求
180
+ try {
181
+ const versionResponse = await axios.post(
182
+ 'https://api.modrinth.com/v2/version',
183
+ form,
184
+ {
185
+ headers: {
186
+ ...form.getHeaders(),
187
+ Authorization: token,
188
+ },
189
+ validateStatus: (status) => status < 500, // 仅拒绝5xx错误
190
+ }
191
+ );
192
+
193
+ // 检查响应状态码
194
+ if (versionResponse.status === 200) {
195
+ // 成功
196
+ logger.log(
197
+ `Successfully published to Modrinth: ${versionResponse.data.name || versionResponse.data.id}`
198
+ );
199
+ return versionResponse.data.id;
200
+ } else if (
201
+ versionResponse.status === 400 ||
202
+ versionResponse.status === 401
203
+ ) {
204
+ // 处理客户端错误
205
+ const data = versionResponse.data;
206
+ if (data && data.error && data.description) {
207
+ logger.error(
208
+ `Modrinth API Error (${versionResponse.status}): ${data.error} - ${data.description}`
209
+ );
210
+ throw new Error(
211
+ `Modrinth发布失败: ${data.error} - ${data.description}`
212
+ );
213
+ } else {
214
+ logger.error(
215
+ `Modrinth API Error (${versionResponse.status}): ${JSON.stringify(data)}`
216
+ );
217
+ throw new Error(
218
+ `Modrinth发布失败 (状态码: ${versionResponse.status}): ${JSON.stringify(data)}`
219
+ );
220
+ }
221
+ } else {
222
+ // 其他错误状态码
223
+ const data = versionResponse.data;
224
+ logger.error(
225
+ `Modrinth API Error (${versionResponse.status}): ${JSON.stringify(data)}`
226
+ );
227
+ throw new Error(
228
+ `Modrinth发布失败 (状态码: ${versionResponse.status}): ${JSON.stringify(data)}`
229
+ );
230
+ }
231
+ } catch (error: any) {
232
+ if (error.response) {
233
+ // 已经在上面处理过
234
+ throw error;
235
+ } else if (error.request) {
236
+ // 请求已发送但未收到响应
237
+ logger.error('Modrinth API 请求失败,未收到响应');
238
+ throw new Error('Modrinth发布失败: 未收到服务器响应');
239
+ } else {
240
+ // 请求配置出错
241
+ logger.error('Modrinth API 请求配置错误:', error.message);
242
+ throw new Error(`Modrinth发布失败: ${error.message}`);
243
+ }
244
+ }
245
+ }
246
+
247
+ /**
248
+ * 为 Modrinth 查找文件并确定主要文件
249
+ */
250
+ async function findModrinthJarFiles(
251
+ pluginConfig: Plugin_config,
252
+ context: PublishContext
253
+ ): Promise<{ files: string[]; primaryFile: string | undefined }> {
254
+ const { logger } = context;
255
+
256
+ // 获取 Modrinth 专用的 glob 配置,如果没有则使用全局配置
257
+ const modrinthGlob = pluginConfig.modrinth?.glob || pluginConfig.glob || [];
258
+
259
+ // 查找所有文件
260
+ const jarFiles = await findFiles(modrinthGlob, context);
261
+ logger.log(
262
+ `Found ${jarFiles.length} JAR file(s) for Modrinth: ${jarFiles.join(', ')}`
263
+ );
264
+
265
+ // 确定主要文件
266
+ let primaryFile: string | undefined = undefined;
267
+
268
+ // 检查是否提供了 primaryFileGlob
269
+ if (pluginConfig.modrinth?.primary_file_glob) {
270
+ const primaryPatterns = Array.isArray(
271
+ pluginConfig.modrinth.primary_file_glob
272
+ )
273
+ ? pluginConfig.modrinth.primary_file_glob
274
+ : [pluginConfig.modrinth.primary_file_glob];
275
+
276
+ // 查找匹配 primaryFileGlob 的文件
277
+ let primaryCandidates: string[] = [];
278
+
279
+ for (const pattern of primaryPatterns) {
280
+ logger.log(`Searching for primary file with pattern: ${pattern}`);
281
+ const matches = await glob(pattern, {
282
+ cwd: context.cwd,
283
+ nodir: true,
284
+ });
285
+ primaryCandidates.push(
286
+ ...matches.map((file) => resolve(context.cwd!, file))
287
+ );
288
+ }
289
+
290
+ // 过滤出 JAR 文件并与找到的文件列表交叉检查
291
+ primaryCandidates = primaryCandidates
292
+ .filter((file) => file.endsWith('.jar'))
293
+ .filter((file) => jarFiles.includes(file));
294
+
295
+ if (primaryCandidates.length === 1) {
296
+ primaryFile = primaryCandidates[0];
297
+ logger.log(`Selected primary file for Modrinth: ${primaryFile}`);
298
+ } else if (primaryCandidates.length > 1) {
299
+ throw new Error(
300
+ `Multiple files matched primaryFileGlob for Modrinth. Please specify a more specific pattern. Found: ${primaryCandidates.join(', ')}`
301
+ );
302
+ } else {
303
+ throw new Error(
304
+ `No files matched primaryFileGlob for Modrinth that were also in the main file list.`
305
+ );
306
+ }
307
+ } else if (jarFiles.length > 1) {
308
+ // 当有多个文件但没有指定 primaryFileGlob 时,需要指定主要文件
309
+ throw new Error(
310
+ `Multiple files found for Modrinth but no primaryFileGlob specified. Please specify which file should be primary.`
311
+ );
312
+ }
313
+ // 当只有一个文件时,不设置 primaryFile,让网站做决定
314
+
315
+ return { files: jarFiles, primaryFile };
316
+ }
package/src/prepare.ts ADDED
@@ -0,0 +1,199 @@
1
+ import axios from 'axios';
2
+ import { NextRelease } from 'semantic-release';
3
+ import {
4
+ BUKKIT_GAME_VERSION_TYPE,
5
+ CurseForgeGameVersion,
6
+ CurseForgeGameVersionMap,
7
+ CurseForgeGameVersionType,
8
+ } from './definitions/curseforge';
9
+ import { Plugin_config } from './definitions/plugin_config';
10
+ import { getCurseForgeModLoaders, toArray } from './utils/utils';
11
+
12
+ /**
13
+ * 根据提供的游戏版本联合对象检索游戏版本 ID 变体数组。
14
+ */
15
+ export async function getCurseForgeGameVersionIds(
16
+ apiToken: string,
17
+ pluginConfig: Plugin_config,
18
+ env: Record<string, string>,
19
+ nextRelease: NextRelease
20
+ ): Promise<number[]> {
21
+ const curseforgeConfig = pluginConfig.curseforge!;
22
+
23
+ const modLoaders = getCurseForgeModLoaders(pluginConfig, env, nextRelease);
24
+ const javaVersions = toArray(curseforgeConfig.java_versions || []);
25
+ const gameVersions = toArray(
26
+ curseforgeConfig.game_versions || pluginConfig.game_versions || []
27
+ );
28
+ const pluginGameVersions = toArray(
29
+ curseforgeConfig.game_versions_for_plugins || []
30
+ );
31
+ const addonGameVersions = toArray(
32
+ curseforgeConfig.game_versions_for_addon || []
33
+ );
34
+ const environments = toArray(curseforgeConfig.environments || []);
35
+
36
+ const map = await createCurseForgeGameVersionMap(apiToken);
37
+
38
+ const javaVersionNames = javaVersions.map(
39
+ (javaVersion: string) => `Java ${javaVersion}`
40
+ );
41
+
42
+ // TODO: Modrinth 和 CurseForge 的游戏版本命名格式转化,以 Modrinth 为基准
43
+ // const gameVersionNames = gameVersions.map(x => formatCurseForgeGameVersionSnapshot(x));
44
+
45
+ // 模组的游戏版本名称
46
+ const gameVersionIds = findCurseForgeGameVersionIdsByNames(
47
+ map.game_versions,
48
+ gameVersions,
49
+ undefined,
50
+ CURSEFORGE_GAME_VERSION_SNAPSHOT_NAME_COMPARER
51
+ );
52
+
53
+ const loaderIds = findCurseForgeGameVersionIdsByNames(
54
+ map.loaders,
55
+ modLoaders
56
+ );
57
+
58
+ const javaIds = findCurseForgeGameVersionIdsByNames(
59
+ map.java_versions,
60
+ javaVersionNames
61
+ );
62
+
63
+ // 插件的游戏版本名称
64
+ const pluginGameVersionIds = findCurseForgeGameVersionIdsByNames(
65
+ map.game_versions_for_plugins,
66
+ pluginGameVersions
67
+ );
68
+
69
+ // 附加组件的游戏版本名称
70
+ const addonGameVersionIds = findCurseForgeGameVersionIdsByNames(
71
+ map.game_versions_for_addons,
72
+ addonGameVersions
73
+ );
74
+
75
+ const environmentIds = findCurseForgeGameVersionIdsByNames(
76
+ map.environments,
77
+ environments
78
+ );
79
+
80
+ const curseforgeGameVersionIds: number[] = [];
81
+ curseforgeGameVersionIds.push(
82
+ ...gameVersionIds,
83
+ ...loaderIds,
84
+ ...javaIds,
85
+ ...pluginGameVersionIds,
86
+ ...addonGameVersionIds,
87
+ ...environmentIds
88
+ );
89
+ return curseforgeGameVersionIds;
90
+ }
91
+
92
+ // 创建一个 CurseForge 游戏版本映射,通过根据类型名称对游戏版本类型进行分类。
93
+ async function createCurseForgeGameVersionMap(
94
+ apiToken: string
95
+ ): Promise<CurseForgeGameVersionMap> {
96
+ const { versions, types } = await fetchCurseForgeGameVersionInfo(apiToken);
97
+ return {
98
+ game_versions: filterGameVersionsByTypeName(
99
+ versions,
100
+ types,
101
+ 'minecraft'
102
+ ),
103
+ game_versions_for_plugins: filterGameVersionsByTypeName(
104
+ versions,
105
+ types,
106
+ 'bukkit'
107
+ ),
108
+ game_versions_for_addons: filterGameVersionsByTypeName(
109
+ versions,
110
+ types,
111
+ 'addon'
112
+ ),
113
+ loaders: filterGameVersionsByTypeName(versions, types, 'modloader'),
114
+ java_versions: filterGameVersionsByTypeName(versions, types, 'java'),
115
+ environments: filterGameVersionsByTypeName(
116
+ versions,
117
+ types,
118
+ 'environment'
119
+ ),
120
+ };
121
+ }
122
+
123
+ function filterGameVersionsByTypeName(
124
+ versions: CurseForgeGameVersion[],
125
+ types: CurseForgeGameVersionType[],
126
+ typeName: string
127
+ ): CurseForgeGameVersion[] {
128
+ const filteredTypes = types.filter((x) => x.slug.startsWith(typeName));
129
+ return versions.filter((v) =>
130
+ filteredTypes.some((t) => t.id === v.gameVersionTypeID)
131
+ );
132
+ }
133
+
134
+ // 获取 CurseForge 游戏版本和版本类型信息
135
+ async function fetchCurseForgeGameVersionInfo(apiToken: string): Promise<{
136
+ versions: CurseForgeGameVersion[];
137
+ types: CurseForgeGameVersionType[];
138
+ }> {
139
+ const gameVersionsRes = await axios.get(
140
+ 'https://minecraft.curseforge.com/api/game/versions',
141
+ {
142
+ headers: {
143
+ 'X-Api-Token': apiToken,
144
+ },
145
+ }
146
+ );
147
+
148
+ const gameVersionTypesRes = await axios.get(
149
+ 'https://minecraft.curseforge.com/api/game/version-types',
150
+ {
151
+ headers: {
152
+ 'X-Api-Token': apiToken,
153
+ },
154
+ }
155
+ );
156
+
157
+ const gameVersionTypes =
158
+ gameVersionTypesRes.data as CurseForgeGameVersionType[];
159
+
160
+ if (!gameVersionTypes.some((x) => x.id === BUKKIT_GAME_VERSION_TYPE.id)) {
161
+ gameVersionTypes.unshift(BUKKIT_GAME_VERSION_TYPE);
162
+ }
163
+
164
+ return {
165
+ versions: gameVersionsRes.data,
166
+ types: gameVersionTypes,
167
+ };
168
+ }
169
+
170
+ function findCurseForgeGameVersionIdsByNames(
171
+ versions: { id: number; name: string }[],
172
+ names: string[],
173
+ comparer: (a: string, b: string) => boolean = (a, b) =>
174
+ a.toLowerCase() === b.toLowerCase(),
175
+ fallbackComparer?: (a: string, b: string) => boolean
176
+ ): number[] {
177
+ const result: number[] = [];
178
+
179
+ for (const name of names) {
180
+ let version = versions.find((v) => comparer(v.name, name));
181
+ if (!version && fallbackComparer) {
182
+ version = versions.find((v) => fallbackComparer(v.name, name));
183
+ }
184
+ if (version) result.push(version.id);
185
+ }
186
+
187
+ return [...new Set(result)];
188
+ }
189
+
190
+ /**
191
+ * 比较器:忽略名称中的 "-Snapshot" 后缀
192
+ */
193
+ export const CURSEFORGE_GAME_VERSION_SNAPSHOT_NAME_COMPARER = (
194
+ a: string,
195
+ b: string
196
+ ): boolean => {
197
+ const normalize = (s: string) => s?.replace(/-snapshot$/i, '') ?? '';
198
+ return normalize(a).toLowerCase() === normalize(b).toLowerCase();
199
+ };
@@ -0,0 +1,120 @@
1
+ import { glob } from 'glob/dist/esm';
2
+ import { template } from 'lodash';
3
+ import { resolve } from 'path';
4
+ import { NextRelease, PublishContext } from 'semantic-release';
5
+ import { Plugin_config } from '../definitions/plugin_config';
6
+
7
+ /**
8
+ * 根据 glob 模式查找文件
9
+ */
10
+ export async function findFiles(
11
+ patterns: string | string[],
12
+ context: PublishContext,
13
+ defaultPatterns: string[] = [
14
+ 'build/libs/!(*-@(dev|sources|javadoc)).jar',
15
+ 'build/libs/*-@(dev|sources|javadoc).jar',
16
+ ]
17
+ ): Promise<string[]> {
18
+ const { logger, cwd } = context;
19
+
20
+ // 使用提供的 glob 模式,如果没有则使用默认模式
21
+ const searchPatterns = patterns
22
+ ? Array.isArray(patterns)
23
+ ? patterns
24
+ : [patterns]
25
+ : defaultPatterns;
26
+
27
+ const allFiles: string[] = [];
28
+
29
+ for (const pattern of searchPatterns) {
30
+ logger.log(`Searching for files with pattern: ${pattern}`);
31
+ const files = await glob(pattern, {
32
+ cwd,
33
+ nodir: true,
34
+ });
35
+ allFiles.push(...files);
36
+ }
37
+
38
+ // 转换为绝对路径
39
+ const files = allFiles.map((file) => resolve(cwd!, file));
40
+
41
+ if (files.length === 0) {
42
+ throw new Error(
43
+ `No files found matching patterns: ${searchPatterns.join(', ')}`
44
+ );
45
+ }
46
+
47
+ return files;
48
+ }
49
+
50
+ /**
51
+ * 依次使用 lodash.template 渲染字符串数组。
52
+ * @param templates 字符串模板数组
53
+ * @param context 模板变量上下文对象
54
+ * @returns 渲染后的字符串数组
55
+ */
56
+ export function renderTemplates(
57
+ templates: string[],
58
+ context: Record<string, any>
59
+ ): string[] {
60
+ return templates.map((tpl) => template(tpl)(context));
61
+ }
62
+
63
+ /**
64
+ * 从多个来源中按优先级选择第一个非空模板并渲染。
65
+ * @param sources 模板字符串来源(优先级从高到低)
66
+ * @param context 模板渲染上下文(传给 lodash.template)
67
+ * @returns 渲染后的字符串或 undefined
68
+ */
69
+ export function resolveTemplate(
70
+ sources: Array<string | undefined | null>,
71
+ context: Record<string, any>
72
+ ): string | undefined {
73
+ const source = sources.find(Boolean);
74
+ if (!source) return undefined;
75
+
76
+ try {
77
+ return template(source)(context);
78
+ } catch (err) {
79
+ console.error('Failed to render template:', err);
80
+ return undefined;
81
+ }
82
+ }
83
+
84
+ /**
85
+ * 确保给定的值总是以数组形式返回。
86
+ *
87
+ * @param value - 单个项目或项目数组。
88
+ * @returns 如果不是数组,则将值包装在数组中返回。
89
+ */
90
+ export function toArray<T>(value: T | T[]): T[] {
91
+ return Array.isArray(value) ? value : [value];
92
+ }
93
+
94
+ export function getCurseForgeModLoaders(
95
+ pluginConfig: Plugin_config,
96
+ env: Record<string, string>,
97
+ nextRelease: NextRelease
98
+ ): string[] {
99
+ const curseforge = pluginConfig.curseforge;
100
+
101
+ let modLoaders: string[] | undefined;
102
+ if (curseforge?.mod_loaders && curseforge.mod_loaders.length > 0) {
103
+ modLoaders = renderTemplates(toArray(curseforge.mod_loaders), {
104
+ nextRelease,
105
+ });
106
+ } else if (
107
+ pluginConfig.mod_loaders &&
108
+ pluginConfig.mod_loaders.length > 0
109
+ ) {
110
+ modLoaders = renderTemplates(toArray(pluginConfig.mod_loaders), {
111
+ nextRelease,
112
+ }) as string[];
113
+ } else if (env.CURSEFORGE_MOD_LOADERS) {
114
+ modLoaders = env.CURSEFORGE_MOD_LOADERS.split(',').map((s) => s.trim());
115
+ } else if (env.MOD_LOADERS) {
116
+ modLoaders = env.MOD_LOADERS.split(',').map((s) => s.trim());
117
+ }
118
+
119
+ return modLoaders || [];
120
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "$schema": "https://json.schemastore.org/tsconfig",
3
+ "compilerOptions": {
4
+ "target": "ES2022", // 产物使用的 JS 语言版本(现代 Node 推荐 ES2022/ES2023/ESNext)
5
+ "module": "ES2022", // 输出为 ESM 模块语法(import / export)
6
+ "moduleResolution": "node", // 如何根据 node 风格解析模块(node 或 bundler 友好)
7
+ "lib": ["ES2022"], // 编译时可用的内置类型(Promise, Map, etc.)
8
+ "rootDir": "src",
9
+ "outDir": "dist",
10
+ "declaration": true, // 生成 .d.ts 类型声明文件(发布库时强烈推荐)
11
+ "sourceMap": true, // 生成 source map,便于调试
12
+ "strict": true, // 启用一整套严格检查(等价开启多项 strict 选项)
13
+ "noImplicitAny": true, // 不允许隐式 any
14
+ "esModuleInterop": true, // 与 CommonJS 的互操作(允许 default 导入 commonjs 模块)
15
+ "allowSyntheticDefaultImports": true,
16
+ "skipLibCheck": true, // 跳过依赖声明的类型检查,显著加快编译并减少第三方问题
17
+ "forceConsistentCasingInFileNames": true,
18
+ "resolveJsonModule": true, // 允许 import .json
19
+ "moduleDetection": "force", // 强制模块类型检测(TypeScript 5+ 的选项)
20
+ "types": ["node"], // 引入 node 全局类型(比如 process、Buffer)
21
+ "declarationMap": true // 生成 declaration maps(对调试 d.ts 有帮助)
22
+ },
23
+ "include": ["src/**/*"],
24
+ "exclude": ["node_modules", "dist", "test", "coverage"]
25
+ }