@@ -53,10 +53,12 @@ export interface DBConfig {
5353 [ key : string ] : unknown ;
5454}
5555
56- export interface PermissionsConfig {
57- permissions ?: Array < { player_name ?: string ; level ?: number ; [ key : string ] : unknown } > ;
56+ /** permissions.json 根为数组(与 permissions.schema.json 一致;勿写成 { permissions: [] }) */
57+ export type PermissionsConfig = Array < {
58+ player_name : string ;
59+ level : number ;
5860 [ key : string ] : unknown ;
59- }
61+ } > ;
6062
6163export interface RuntimeConfig {
6264 runtime_root ?: string ;
@@ -137,20 +139,15 @@ export type ConfigSchemaId =
137139 | "module_catalog" ;
138140
139141/**
140- * 生成文件内 `$schema` 相对路径(相对 configs/ 或 packs/)。
141- * IDE 用;运行时加载器应忽略该键。
142+ * 生成文件内 `$schema` 相对路径。
143+ * configs/、packs/、modules/ 均在仓顶下一层,相对 node_modules 深度相同;
144+ * `from` 仅作调用点语义标注,便于日后若布局分叉再按位置扩展(OCP)。
142145 */
143146export function configSchemaRef (
144147 schemaId : ConfigSchemaId ,
145- from : "configs" | "packs" | "modules" = "configs"
148+ _from : "configs" | "packs" | "modules" = "configs"
146149) : string {
147- const rel =
148- from === "configs"
149- ? "../node_modules/@sfmc-bds/sdk/schemas"
150- : from === "packs"
151- ? "../node_modules/@sfmc-bds/sdk/schemas"
152- : "../node_modules/@sfmc-bds/sdk/schemas" ;
153- return `${ rel } /${ schemaId } .schema.json` ;
150+ return `../node_modules/@sfmc-bds/sdk/schemas/${ schemaId } .schema.json` ;
154151}
155152
156153/** 在对象根写入 `$schema`(不覆盖已有);数组根勿调用。 */
@@ -215,7 +212,7 @@ export const DEFAULT_BDS_UPDATER_CONFIG: BdsUpdaterConfig = {
215212} ;
216213
217214/** permissions.json 根为数组;默认空表 */
218- export const DEFAULT_PERMISSIONS : Array < { player_name : string ; level : number } > = [ ] ;
215+ export const DEFAULT_PERMISSIONS : PermissionsConfig = [ ] ;
219216
220217/** remote.json 骨架(enroll 前) */
221218export const DEFAULT_REMOTE_CONFIG : RemoteConfig = {
@@ -341,4 +338,78 @@ export function ensureJson<T>(filePath: string, seed: T = {} as T): T {
341338 */
342339export function ensureJsonConfig < T > ( root : string , name : ConfigName , seed : T = { } as T ) : T {
343340 return ensureJson < T > ( configPath ( root , name ) , seed ) ;
341+ }
342+
343+ /**
344+ * ensure 带 `$schema` 的配置对象(DRY:禁止各服务手写 ensureJsonConfig+withConfigSchema)。
345+ * 返回磁盘内容(可能含元数据键);运行时业务请用 {@link loadEnsuredConfig}。
346+ */
347+ export function ensureSchemaConfig < T extends Record < string , unknown > > (
348+ root : string ,
349+ name : ConfigName ,
350+ schemaId : ConfigSchemaId ,
351+ defaults : T ,
352+ from : "configs" | "packs" | "modules" = "configs"
353+ ) : T & { $schema ?: string } {
354+ return ensureJsonConfig (
355+ root ,
356+ name ,
357+ withConfigSchema ( { ...defaults } as Record < string , unknown > , schemaId , from )
358+ ) as T & { $schema ?: string } ;
359+ }
360+
361+ /**
362+ * ensure 后剥离元数据,返回可交给业务逻辑的纯配置(LSP:与无 `$schema` 的契约一致)。
363+ */
364+ export function loadEnsuredConfig < T extends Record < string , unknown > > (
365+ root : string ,
366+ name : ConfigName ,
367+ schemaId : ConfigSchemaId ,
368+ defaults : T ,
369+ from : "configs" | "packs" | "modules" = "configs"
370+ ) : T {
371+ return stripConfigMeta (
372+ ensureSchemaConfig ( root , name , schemaId , defaults , from ) as Record < string , unknown >
373+ ) as T ;
374+ }
375+
376+ /** 平台核心配置种子集合(扩展新文件时只改此处 + switch) */
377+ export type CoreConfigKind = "db_config" | "qq_config" | "bds_updater" | "permissions" | "remote" ;
378+
379+ /**
380+ * 按需 ensure 平台核心配置。新种类加到 CoreConfigKind + switch 即可(OCP)。
381+ * permissions 根为数组,不能走 withConfigSchema。
382+ */
383+ export function ensureCoreConfigs ( root : string , kinds : readonly CoreConfigKind [ ] ) : void {
384+ for ( const kind of kinds ) {
385+ switch ( kind ) {
386+ case "db_config" :
387+ ensureSchemaConfig ( root , "db_config.json" , "db_config" , {
388+ ...DEFAULT_DB_CONFIG ,
389+ } as Record < string , unknown > ) ;
390+ break ;
391+ case "qq_config" :
392+ ensureSchemaConfig ( root , "qq_config.json" , "qq_config" , {
393+ ...DEFAULT_QQ_CONFIG ,
394+ } as Record < string , unknown > ) ;
395+ break ;
396+ case "bds_updater" :
397+ ensureSchemaConfig ( root , "bds_updater.json" , "bds_updater" , {
398+ ...DEFAULT_BDS_UPDATER_CONFIG ,
399+ } as Record < string , unknown > ) ;
400+ break ;
401+ case "permissions" :
402+ ensureJson ( configPath ( root , "permissions.json" ) , DEFAULT_PERMISSIONS ) ;
403+ break ;
404+ case "remote" :
405+ ensureSchemaConfig ( root , "remote.json" , "remote" , {
406+ ...DEFAULT_REMOTE_CONFIG ,
407+ } as Record < string , unknown > ) ;
408+ break ;
409+ default : {
410+ const _exhaustive : never = kind ;
411+ void _exhaustive ;
412+ }
413+ }
414+ }
344415}
0 commit comments