From cfce62195d7f84c6b77858334094e59659bdcb59 Mon Sep 17 00:00:00 2001 From: CPTProgrammer <46586216+CPTProgrammer@users.noreply.github.com> Date: Mon, 6 Jul 2026 21:22:04 +0800 Subject: [PATCH] Add Zod schemas for project config and secrets types --- src/types/config.ts | 122 ++++++++++++++++++++++++++++++++++++++++++++ src/types/index.ts | 2 +- 2 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 src/types/config.ts diff --git a/src/types/config.ts b/src/types/config.ts new file mode 100644 index 0000000..6f1e7c1 --- /dev/null +++ b/src/types/config.ts @@ -0,0 +1,122 @@ +import { z } from "zod"; + +// region ModrinthDependency — config 中的 Modrinth 依赖项(比 VersionDependency 简化) +export const ModrinthDependencySchema = z.object({ + /** 依赖类型 */ + dependency_type: z.enum([ + "required", + "optional", + "incompatible", + "embedded", + ]), + /** 依赖的项目 ID(8 位 base62) */ + project_id: z.string().min(1), +}); +export type ModrinthDependency = z.infer; +// endregion + +// region ModrinthConfig — config 中 modrinth 字段 +export const ModrinthConfigSchema = z.object({ + /** Modrinth 项目 ID(8 位 base62) */ + project_id: z.string().min(1), + /** 版本名称模板,支持 ${version}、${mc_version_range} 等占位符 */ + version_name: z.string().min(1), + /** 版本号模板,支持 ${version}、${mc_version} 等占位符 */ + version: z.string().min(1), + /** 加载器列表 */ + loaders: z.array(z.string().min(1)), + /** 运行环境 */ + environment: z.enum([ + "unknown", + "client_only", + "server_only", + "dedicated_server_only", + "client_and_server", + "server_only_client_optional", + "client_only_server_optional", + "client_or_server_prefers_both", + "client_or_server", + "singleplayer_only", + ]), + /** 依赖列表,无依赖填 [] */ + dependencies: z.array(ModrinthDependencySchema), +}); +export type ModrinthConfig = z.infer; +// endregion + +// region CurseForgeRelationProject — config 中 CurseForge 关联项目 +export const CurseForgeRelationProjectSchema = z.object({ + /** 关联项目的 slug */ + slug: z.string().min(1), + /** 关联项目的 ID(精确匹配),可选 */ + projectID: z.string().optional(), + /** 关联类型 */ + type: z.enum([ + "embeddedLibrary", + "incompatible", + "optionalDependency", + "requiredDependency", + "tool", + ]), +}); +export type CurseForgeRelationProject = z.infer< + typeof CurseForgeRelationProjectSchema +>; +// endregion + +// region CurseForgeRelations — config 中 CurseForge 关联项目集合 +export const CurseForgeRelationsSchema = z.object({ + projects: z.array(CurseForgeRelationProjectSchema), +}); +export type CurseForgeRelations = z.infer; +// endregion + +// region CurseForgeConfig — config 中 curseforge 字段 +export const CurseForgeConfigSchema = z.object({ + /** CurseForge 项目 ID(数字) */ + project_id: z.number().int().positive(), + /** 版本名称模板,支持 ${filename_format} 等占位符 */ + version_name: z.string().min(1), + /** 运行环境 */ + environment: z.array(z.string().min(1)), + /** 加载器列表 */ + loaders: z.array(z.string().min(1)), + /** 关联项目 */ + relations: CurseForgeRelationsSchema, +}); +export type CurseForgeConfig = z.infer; +// endregion + +// region Config — configs/{name}.json 完整结构 +export const ConfigSchema = z.object({ + /** 配置文件显示名称(模组名称) */ + name: z.string().min(1), + /** Modrinth 配置 */ + modrinth: ModrinthConfigSchema, + /** CurseForge 配置 */ + curseforge: CurseForgeConfigSchema, + /** 项目目录绝对路径 */ + project_dir: z.string().min(1), + /** 相对 project_dir 的 MC 版本配置目录,默认 ./properties */ + minecraft_properties_dir: z.string().min(1), + /** 相对 project_dir 的项目版本配置文件路径,默认 ./gradle.properties */ + project_properties_path: z.string().min(1), + /** 版本号在 project_properties_path 文件中的键名,默认 "mod_version" */ + mod_version_field: z.string().min(1), + /** 构建产物文件名模板,支持 ${version}、${mc_version} 占位符 */ + filename_format: z.string().min(1), + /** 源码 jar 文件名模板,支持 ${version}、${mc_version} 占位符 */ + source_filename_format: z.string().min(1), +}); +export type Config = z.infer; +// endregion + +// region Secrets — secrets.json 结构 +export const SecretsSchema = z.object({ + /** CurseForge API Token */ + curseforge: z.string().min(1), + /** Modrinth Personal Access Token */ + modrinth: z.string().min(1), +}); +export type Secrets = z.infer; +// endregion diff --git a/src/types/index.ts b/src/types/index.ts index 296fef4..3b5cf8d 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,3 +1,3 @@ +export * from "./config"; export * from "./modrinth"; export * from "./curseforge"; -// export * from "./config"; // TODO