11import { CodePlatform , ICodePlatform } from './types' ;
22
33export interface ICodePlatformConfig {
4- platform : CodePlatform ;
4+ platform : CodePlatform | string ;
55 origin : string ;
66 hostname : string [ ] ;
77 endpoint : string ;
@@ -17,7 +17,7 @@ export interface ICodePlatformConfig {
1717}
1818
1919// 代码托管平台配置
20- export const CODE_PLATFORM_CONFIG : Record < ICodePlatform , ICodePlatformConfig > = {
20+ const CODE_PLATFORM_CONFIG : Record < ICodePlatform , ICodePlatformConfig > = {
2121 [ CodePlatform . github ] : {
2222 platform : CodePlatform . github ,
2323 hostname : [ 'github.com' ] ,
@@ -175,29 +175,77 @@ export const CODE_PLATFORM_CONFIG: Record<ICodePlatform, ICodePlatformConfig> =
175175 } ,
176176} ;
177177
178- export const extendPlatformConfig = (
179- platform : ICodePlatform ,
180- data : {
181- hostname ?: string [ ] ;
182- origin ?: string ;
183- endpoint ?: string ;
184- token ?: string ;
185- } ,
186- ) => {
187- const config = CODE_PLATFORM_CONFIG [ platform ] ;
188- if ( ! config ) {
189- return ;
178+ export class CodePlatformRegistry {
179+ protected platformMap = new Map < string , ICodePlatformConfig > ( ) ;
180+
181+ protected constructor ( ) {
182+ this . load ( CODE_PLATFORM_CONFIG ) ;
190183 }
191- if ( Array . isArray ( data . hostname ) ) {
192- config . hostname . push ( ...data . hostname ) ;
184+
185+ protected static _instance : CodePlatformRegistry ;
186+ static instance ( ) {
187+ if ( ! CodePlatformRegistry . _instance ) {
188+ CodePlatformRegistry . _instance = new CodePlatformRegistry ( ) ;
189+ }
190+ return CodePlatformRegistry . _instance ;
193191 }
194- if ( data . origin ) {
195- config . origin = data . origin ;
192+
193+ registerPlatformConfig (
194+ platform : string ,
195+ provider : ICodePlatformConfig ,
196+ ) {
197+ if ( ! this . platformMap . has ( platform ) ) {
198+ this . platformMap . set ( platform , provider ) ;
199+ }
196200 }
197- if ( data . endpoint ) {
198- config . endpoint = data . endpoint ;
201+
202+ getPlatformConfig ( platform : string ) : ICodePlatformConfig {
203+ if ( this . platformMap . has ( platform ) ) {
204+ return this . platformMap . get ( platform ) ! ;
205+ }
206+ throw new Error ( `[Code API]: no config found for ${ platform } ` ) ;
199207 }
200- if ( data . token ) {
201- config . token = data . token ;
208+
209+ extendPlatformConfig (
210+ platform : string ,
211+ data : {
212+ hostname ?: string [ ] | undefined ;
213+ origin ?: string | undefined ;
214+ endpoint ?: string | undefined ;
215+ token ?: string | undefined ;
216+ } ,
217+ ) : void {
218+ const provider = this . platformMap . get ( platform ) ;
219+ if ( ! provider ) {
220+ return ;
221+ }
222+ if ( Array . isArray ( data . hostname ) ) {
223+ provider . hostname . push ( ...data . hostname ) ;
224+ }
225+ if ( data . origin ) {
226+ provider . origin = data . origin ;
227+ }
228+ if ( data . endpoint ) {
229+ provider . endpoint = data . endpoint ;
230+ }
231+ if ( data . token ) {
232+ provider . token = data . token ;
233+ }
202234 }
203- } ;
235+
236+ load ( configs : Record < string , ICodePlatformConfig > ) {
237+ Object . keys ( configs ) . forEach ( ( key ) => {
238+ this . registerPlatformConfig ( key , configs [ key ] ) ;
239+ } ) ;
240+ }
241+
242+ getCodePlatformConfigs ( ) : Record < string , ICodePlatformConfig > {
243+ const result = { } as Record < string , ICodePlatformConfig > ;
244+
245+ this . platformMap . forEach ( ( config , key ) => {
246+ result [ key ] = config ;
247+ } ) ;
248+
249+ return result ;
250+ }
251+ }
0 commit comments