diff --git a/dprint.json b/dprint.json index 1d0703b4..d70e417a 100644 --- a/dprint.json +++ b/dprint.json @@ -2,16 +2,10 @@ "typescript": { "quoteStyle": "alwaysSingle" }, - "json": { - }, - "markdown": { - }, - "toml": { - }, - "excludes": [ - "**/node_modules", - "**/*-lock.json" - ], + "json": {}, + "markdown": {}, + "toml": {}, + "excludes": ["**/node_modules", "**/*-lock.json"], "plugins": [ "https://plugins.dprint.dev/typescript-0.91.0.wasm", "https://plugins.dprint.dev/json-0.19.3.wasm", diff --git a/packages/code-api/src/atomgit/atomgit.service.ts b/packages/code-api/src/atomgit/atomgit.service.ts index e87b4e28..ff07433a 100644 --- a/packages/code-api/src/atomgit/atomgit.service.ts +++ b/packages/code-api/src/atomgit/atomgit.service.ts @@ -1,7 +1,7 @@ import { request, RequestOptions } from '@codeblitzjs/ide-common'; import { Autowired, Injectable } from '@opensumi/di'; import { isObject, MessageType, URI } from '@opensumi/ide-core-common'; -import { CODE_PLATFORM_CONFIG, HelperService } from '../common'; +import { CodePlatformRegistry, HelperService } from '../common'; import { Branch, BranchOrTag, @@ -28,7 +28,7 @@ export class AtomGitAPIService implements ICodeAPIService { @Autowired(HelperService) helper: HelperService; - private config = CODE_PLATFORM_CONFIG[CodePlatform.atomgit]; + private config = CodePlatformRegistry.instance().getPlatformConfig(CodePlatform.atomgit); private _PRIVATE_TOKEN: string | null; diff --git a/packages/code-api/src/code-api.provider.ts b/packages/code-api/src/code-api.provider.ts index 68f07918..1f3f41d6 100644 --- a/packages/code-api/src/code-api.provider.ts +++ b/packages/code-api/src/code-api.provider.ts @@ -5,7 +5,14 @@ import { IMainLayoutService } from '@opensumi/ide-main-layout'; import { IIconService } from '@opensumi/ide-theme'; import { AtomGitAPIService } from './atomgit/atomgit.service'; import { CodeUPAPIService } from './codeup/codeup.service'; -import { CodePlatform, ICodeAPIProvider, ICodeAPIService, ICodePlatform } from './common/types'; +import { CodePlatformRegistry, ICodePlatformConfig } from './common'; +import { + CodePlatform, + ICodeAPIProvider, + ICodeAPIService, + ICodePlatform, + ICodePlatformAPIProvider, +} from './common/types'; import { GiteeAPIService } from './gitee/gitee.service'; import { GitHubAPIService } from './github/github.service'; import { GitHubView } from './github/github.view'; @@ -27,13 +34,10 @@ export class CodeAPIProvider implements ICodeAPIProvider, ClientAppContribution private started = new Deferred(); private apiProviderMap = new Map< - ICodePlatform, - { - provider: ConstructorOf; - onCreate?: () => void; - } + string, + ICodePlatformAPIProvider >(); - private apiServiceMap = new Map(); + private apiServiceMap = new Map(); constructor() { this.registerPlatformProvider(CodePlatform.github, { @@ -95,8 +99,8 @@ export class CodeAPIProvider implements ICodeAPIProvider, ClientAppContribution } registerPlatformProvider( - platform: ICodePlatform, - provider: { provider: ConstructorOf; onCreate?: () => void }, + platform: string, + provider: ICodePlatformAPIProvider, ) { if (!this.apiProviderMap.has(platform)) { this.apiProviderMap.set(platform, provider); @@ -136,6 +140,7 @@ export class CodeAPIProvider implements ICodeAPIProvider, ClientAppContribution get codeup() { return this.asPlatform(CodePlatform.codeup) as CodeUPAPIService; } + onStart() { this.started.resolve(); } diff --git a/packages/code-api/src/codeup/codeup.service.ts b/packages/code-api/src/codeup/codeup.service.ts index 64b145ef..1b02ad32 100644 --- a/packages/code-api/src/codeup/codeup.service.ts +++ b/packages/code-api/src/codeup/codeup.service.ts @@ -15,7 +15,7 @@ import { CommitParams, CommitFileStatus, } from '../common/types'; -import { CODE_PLATFORM_CONFIG } from '../common/config'; +import { CodePlatformRegistry } from '../common/config'; import { HelperService } from '../common/service'; import { DEFAULT_SEARCH_IN_WORKSPACE_LIMIT } from '@opensumi/ide-search'; const toType = (d: API.ResponseCommitFileChange) => { @@ -48,10 +48,7 @@ export class CodeUPAPIService implements ICodeAPIService { @Autowired(HelperService) helper: HelperService; - - - config = CODE_PLATFORM_CONFIG[CodePlatform.codeup]; - + config = CodePlatformRegistry.instance().getPlatformConfig(CodePlatform.codeup); // 只保留上一次的缓存,用于匹配过滤 private readonly searchContentLRU = new LRUCache(1); diff --git a/packages/code-api/src/common/config.ts b/packages/code-api/src/common/config.ts index ed93be17..eac465b0 100644 --- a/packages/code-api/src/common/config.ts +++ b/packages/code-api/src/common/config.ts @@ -1,7 +1,7 @@ import { CodePlatform, ICodePlatform } from './types'; export interface ICodePlatformConfig { - platform: CodePlatform; + platform: CodePlatform | string; origin: string; hostname: string[]; endpoint: string; @@ -17,7 +17,7 @@ export interface ICodePlatformConfig { } // 代码托管平台配置 -export const CODE_PLATFORM_CONFIG: Record = { +const CODE_PLATFORM_CONFIG: Record = { [CodePlatform.github]: { platform: CodePlatform.github, hostname: ['github.com'], @@ -175,29 +175,77 @@ export const CODE_PLATFORM_CONFIG: Record = }, }; -export const extendPlatformConfig = ( - platform: ICodePlatform, - data: { - hostname?: string[]; - origin?: string; - endpoint?: string; - token?: string; - }, -) => { - const config = CODE_PLATFORM_CONFIG[platform]; - if (!config) { - return; +export class CodePlatformRegistry { + protected platformMap = new Map(); + + protected constructor() { + this.load(CODE_PLATFORM_CONFIG); } - if (Array.isArray(data.hostname)) { - config.hostname.push(...data.hostname); + + protected static _instance: CodePlatformRegistry; + static instance() { + if (!CodePlatformRegistry._instance) { + CodePlatformRegistry._instance = new CodePlatformRegistry(); + } + return CodePlatformRegistry._instance; } - if (data.origin) { - config.origin = data.origin; + + registerPlatformConfig( + platform: string, + provider: ICodePlatformConfig, + ) { + if (!this.platformMap.has(platform)) { + this.platformMap.set(platform, provider); + } } - if (data.endpoint) { - config.endpoint = data.endpoint; + + getPlatformConfig(platform: string): ICodePlatformConfig { + if (this.platformMap.has(platform)) { + return this.platformMap.get(platform)!; + } + throw new Error(`[Code API]: no config found for ${platform}`); } - if (data.token) { - config.token = data.token; + + extendPlatformConfig( + platform: string, + data: { + hostname?: string[] | undefined; + origin?: string | undefined; + endpoint?: string | undefined; + token?: string | undefined; + }, + ): void { + const provider = this.platformMap.get(platform); + if (!provider) { + return; + } + if (Array.isArray(data.hostname)) { + provider.hostname.push(...data.hostname); + } + if (data.origin) { + provider.origin = data.origin; + } + if (data.endpoint) { + provider.endpoint = data.endpoint; + } + if (data.token) { + provider.token = data.token; + } } -}; + + load(configs: Record) { + Object.keys(configs).forEach((key) => { + this.registerPlatformConfig(key, configs[key]); + }); + } + + getCodePlatformConfigs(): Record { + const result = {} as Record; + + this.platformMap.forEach((config, key) => { + result[key] = config; + }); + + return result; + } +} diff --git a/packages/code-api/src/common/service.ts b/packages/code-api/src/common/service.ts index 27bf0c4d..1d1997f3 100644 --- a/packages/code-api/src/common/service.ts +++ b/packages/code-api/src/common/service.ts @@ -12,7 +12,7 @@ import { } from '@opensumi/ide-core-common'; import { IDialogService, IMessageService } from '@opensumi/ide-overlay'; import { DialogService } from '@opensumi/ide-overlay/lib/browser/dialog.service'; -import { CODE_PLATFORM_CONFIG } from './config'; +import { CodePlatformRegistry } from './config'; import { ATOMGIT_PRIVATE_TOKEN, GITEE_PRIVATE_TOKEN, GITHUB_OAUTH_TOKEN, GITLAB_PRIVATE_TOKEN } from './constant'; import { ICodePlatform } from './types'; @@ -121,19 +121,21 @@ export class HelperService { } showMessage( - platform: ICodePlatform, + platform: ICodePlatform | string, msg: { type: MessageType; status?: number; symbol?: string; args?: any[]; message?: string }, config?: { buttons?: string[]; closable?: boolean }, ) { const message = `${msg.status ? `${msg.status} - ` : ''}${ msg.symbol ? localize(msg.symbol, ...(msg.args || [])) : msg.message }`; + const platformConfig = CodePlatformRegistry.instance().getPlatformConfig(platform); + return this.messageService.open( message, msg.type, config?.buttons, config?.closable, - CODE_PLATFORM_CONFIG[platform].brand, + platformConfig.brand, ); } diff --git a/packages/code-api/src/common/types.ts b/packages/code-api/src/common/types.ts index dfc63acb..46659ac1 100644 --- a/packages/code-api/src/common/types.ts +++ b/packages/code-api/src/common/types.ts @@ -1,3 +1,5 @@ +import { ICodePlatformConfig } from './config'; + export namespace CodeAPI { interface Entry { id: string; @@ -209,7 +211,7 @@ export type ISearchResults = Array<{ }>; export interface IRepositoryModel { - platform: ICodePlatform; + platform: ICodePlatform | string; owner: string; name: string; commit: string; @@ -246,7 +248,7 @@ export interface CommitFileChange { deletions: number | null; } -export const enum CommitFileStatus { +export enum CommitFileStatus { Added = 'A', Modified = 'M', Deleted = 'D', @@ -344,14 +346,19 @@ export interface Iteration { } export enum IterationPlatform {} +export interface ICodePlatformAPIProvider { + provider: ConstructorOf; + onCreate?: () => void; +} + export const ICodeAPIProvider = Symbol('ICodeAPIProvider'); export interface ICodeAPIProvider { registerPlatformProvider( - platform: ICodePlatform, - provider: { provider: ConstructorOf; onView?: () => void }, + platform: string, + provider: ICodePlatformAPIProvider, ): void; - asPlatform(platform: ICodePlatform): ICodeAPIService; + asPlatform(platform: string): ICodeAPIService; } export interface RequestFailed { diff --git a/packages/code-api/src/gitee/gitee.service.ts b/packages/code-api/src/gitee/gitee.service.ts index f05c5b4c..25b8b4ea 100644 --- a/packages/code-api/src/gitee/gitee.service.ts +++ b/packages/code-api/src/gitee/gitee.service.ts @@ -21,7 +21,7 @@ import { CodeAPI, } from '../common/types'; import { request, RequestOptions } from '@codeblitzjs/ide-common'; -import { CODE_PLATFORM_CONFIG, HelperService } from '../common'; +import { CodePlatformRegistry, HelperService } from '../common'; import { URI, MessageType, isObject } from '@opensumi/ide-core-common'; import { API } from './types'; @@ -43,7 +43,7 @@ export class GiteeAPIService implements ICodeAPIService { @Autowired(HelperService) helper: HelperService; - private config = CODE_PLATFORM_CONFIG[CodePlatform.gitee]; + private config = CodePlatformRegistry.instance().getPlatformConfig(CodePlatform.gitee); private _PRIVATE_TOKEN: string | null; diff --git a/packages/code-api/src/github/github.service.ts b/packages/code-api/src/github/github.service.ts index 7c9da3c9..6c689671 100644 --- a/packages/code-api/src/github/github.service.ts +++ b/packages/code-api/src/github/github.service.ts @@ -24,7 +24,7 @@ import { } from '../common/types'; import type { IRepositoryModel, EntryParam } from '../common/types'; import { CodePlatform, CommitFileStatus, CodeAPI as ConflictAPI } from '../common/types'; -import { CODE_PLATFORM_CONFIG } from '../common/config'; +import { CodePlatformRegistry } from '../common/config'; const toType = (status: string) => { switch (status) { @@ -52,7 +52,7 @@ export class GitHubAPIService implements ICodeAPIService { @Autowired(CommandService) commandService: CommandService; - private config = CODE_PLATFORM_CONFIG[CodePlatform.github]; + private config = CodePlatformRegistry.instance().getPlatformConfig(CodePlatform.github); /** 资源限制信息 */ @observable diff --git a/packages/code-api/src/gitlab/gitlab.service.ts b/packages/code-api/src/gitlab/gitlab.service.ts index 126549e3..7d007e5e 100644 --- a/packages/code-api/src/gitlab/gitlab.service.ts +++ b/packages/code-api/src/gitlab/gitlab.service.ts @@ -3,7 +3,7 @@ import { localize, IReporterService, formatLocalize, MessageType } from '@opensu import { request, RequestOptions } from '@codeblitzjs/ide-common'; import { API } from './types'; import { HelperService } from '../common/service'; -import { CODE_PLATFORM_CONFIG } from '../common/config'; +import { CodePlatformRegistry } from '../common/config'; import type { TreeEntry, EntryParam, @@ -51,7 +51,7 @@ export class GitLabAPIService implements ICodeAPIService { @Autowired() helper: HelperService; - private config = CODE_PLATFORM_CONFIG[CodePlatform.gitlab]; + private config = CodePlatformRegistry.instance().getPlatformConfig(CodePlatform.gitlab); private _PRIVATE_TOKEN: string | null; diff --git a/packages/code-api/src/gitlink/gitlink.service.ts b/packages/code-api/src/gitlink/gitlink.service.ts index bb6b0055..4742e021 100644 --- a/packages/code-api/src/gitlink/gitlink.service.ts +++ b/packages/code-api/src/gitlink/gitlink.service.ts @@ -2,7 +2,7 @@ import { request, RequestOptions } from '@codeblitzjs/ide-common'; import { Autowired, Injectable } from '@opensumi/di'; import { formatLocalize, IReporterService, MessageType } from '@opensumi/ide-core-common'; import { DEFAULT_SEARCH_IN_WORKSPACE_LIMIT } from '@opensumi/ide-search'; -import { CODE_PLATFORM_CONFIG } from '../common/config'; +import { CodePlatformRegistry } from '../common/config'; import { HelperService } from '../common/service'; import type { BranchOrTag, @@ -45,7 +45,7 @@ export class GitLinkAPIService implements ICodeAPIService { @Autowired() helper: HelperService; - private config = CODE_PLATFORM_CONFIG[CodePlatform.gitlink]; + private config = CodePlatformRegistry.instance().getPlatformConfig(CodePlatform.gitlink); private _PRIVATE_TOKEN: string | null; diff --git a/packages/code-service/src/code-model.service.ts b/packages/code-service/src/code-model.service.ts index 76a1c7e9..a2bbec55 100644 --- a/packages/code-service/src/code-model.service.ts +++ b/packages/code-service/src/code-model.service.ts @@ -1,4 +1,4 @@ -import { CODE_PLATFORM_CONFIG, extendPlatformConfig, ICodeAPIProvider } from '@codeblitzjs/ide-code-api'; +import { CodePlatformRegistry, ICodeAPIProvider } from '@codeblitzjs/ide-code-api'; import { AppConfig } from '@codeblitzjs/ide-sumi-core'; import { Autowired, Injectable, Injector, INJECTOR_TOKEN } from '@opensumi/di'; import { CommandService, Emitter, localize } from '@opensumi/ide-core-common'; @@ -40,15 +40,16 @@ export class CodeModelService { readonly onDidOpenRepository = this._onDidOpenRepository.event; constructor() { - const config = this.codeServiceConfig; + const codeServiceConfig = this.codeServiceConfig; + const configs = CodePlatformRegistry.instance().getCodePlatformConfigs(); this._rootRepository = this.injector.get(RootRepository, [ { root: this.appConfig.workspaceDir, - owner: config.owner, - name: config.name, - platform: config.platform, - projectId: config.projectId, + owner: codeServiceConfig.owner, + name: codeServiceConfig.name, + platform: codeServiceConfig.platform, + projectId: codeServiceConfig.projectId, commit: 'HEAD', }, ]); @@ -57,9 +58,9 @@ export class CodeModelService { this._onDidOpenRepository.fire(this._rootRepository); - Object.keys(CODE_PLATFORM_CONFIG).forEach((platform) => { - if (config[platform]) { - extendPlatformConfig(platform as ICodePlatform, config[platform]); + Object.keys(configs).forEach((platform) => { + if (codeServiceConfig[platform]) { + CodePlatformRegistry.instance().extendPlatformConfig(platform, codeServiceConfig[platform]!); } }); } @@ -107,7 +108,8 @@ export class CodeModelService { logger.error(`[Code Service] not submodule for ${path}`); return; } - const project = parseSubmoduleUrl(submodule.url); + const configs = CodePlatformRegistry.instance().getCodePlatformConfigs(); + const project = parseSubmoduleUrl(submodule.url, configs); if (!project) { this.messageService.error(localize('code-service.submodules-parse-error', submodule.url)); logger.error(`[Code Service] not support ${submodule.url}`); @@ -188,13 +190,13 @@ export class CodeModelService { } replaceBrowserUrlLine(lineNumbers: [number, number]) { - const hash = CODE_PLATFORM_CONFIG[this.rootRepository.platform].line.format(lineNumbers); + const hash = this.rootRepository.platformConfig.line.format(lineNumbers); this.commandService.tryExecuteCommand('code-service.replace-browser-url-hash', hash); } parseLineHash(hash: string) { if (hash) { - return CODE_PLATFORM_CONFIG[this.rootRepository.platform].line.parse(hash); + return this.rootRepository.platformConfig.line.parse(hash); } } } diff --git a/packages/code-service/src/code-service.contribution.ts b/packages/code-service/src/code-service.contribution.ts index cd68c118..f291bebd 100644 --- a/packages/code-service/src/code-service.contribution.ts +++ b/packages/code-service/src/code-service.contribution.ts @@ -1,4 +1,4 @@ -import { CODE_PLATFORM_CONFIG, RequestFailed } from '@codeblitzjs/ide-code-api'; +import { RequestFailed } from '@codeblitzjs/ide-code-api'; import { AppConfig, CODE_ROOT, @@ -276,7 +276,8 @@ export class CodeContribution await repo.refsInitialized; const getShortCommit = (commit: string) => (commit || '').substr(0, 8); const createBranch: { name: string; commit: string; type: PickBranch }[] = []; - if (CODE_PLATFORM_CONFIG[repo.platform].createBranchAble) { + const config = repo.platformConfig; + if (config.createBranchAble) { createBranch.push( { name: localize('code-service.command.create-branch'), diff --git a/packages/code-service/src/commands.contribution.ts b/packages/code-service/src/commands.contribution.ts index e7e96cfa..48fc68c6 100644 --- a/packages/code-service/src/commands.contribution.ts +++ b/packages/code-service/src/commands.contribution.ts @@ -1,11 +1,4 @@ -import { - CODE_PLATFORM_CONFIG, - CodeAPI, - CodePlatform, - CommitFileChange, - CommitParams, - CommitRecord, -} from '@codeblitzjs/ide-code-api'; +import { CodeAPI, CodePlatform, CommitFileChange, CommitParams, CommitRecord } from '@codeblitzjs/ide-code-api'; import { Autowired } from '@opensumi/di'; import { IClipboardService, IOpenerService } from '@opensumi/ide-core-browser'; import { Command, CommandContribution, CommandRegistry, Disposable, Domain } from '@opensumi/ide-core-common'; @@ -115,7 +108,8 @@ export class CommandsContribution extends Disposable implements CommandContribut const repo = this.codeModel.getRepository(filepath); if (repo) { if (res.type === RemoteResourceType.Commit) { - const { origin } = CODE_PLATFORM_CONFIG[repo.platform]; + const { origin } = repo.platformConfig; + if (repo.platform === CodePlatform.gitlink) { this.openerService.open(`${origin}/${repo.owner}/${repo.name}/commits/${res.sha}`); } else { @@ -137,7 +131,8 @@ export class CommandsContribution extends Disposable implements CommandContribut } async repository() { - const { HEAD, commit, headLabel, name, owner, platform, ref, refs } = this.codeModel.rootRepository; + const { HEAD, commit, headLabel, name, owner, platform, ref, platformConfig } = this.codeModel.rootRepository; + return { HEAD, commit, @@ -146,7 +141,7 @@ export class CommandsContribution extends Disposable implements CommandContribut owner, platform, ref, - origin: CODE_PLATFORM_CONFIG[platform].origin, + origin: platformConfig.origin, // refs: refs }; } @@ -256,7 +251,7 @@ export class CommandsContribution extends Disposable implements CommandContribut async remoteUrl(repoPath: string): Promise { const repo = this.codeModel.getRepository(repoPath); if (!repo) return null; - const { origin } = CODE_PLATFORM_CONFIG[repo.platform]; + const { origin } = repo.platformConfig; return `${origin}/${repo.owner}/${repo.name}`; } diff --git a/packages/code-service/src/repository.ts b/packages/code-service/src/repository.ts index b6446835..9263e3de 100644 --- a/packages/code-service/src/repository.ts +++ b/packages/code-service/src/repository.ts @@ -1,4 +1,4 @@ -import { ICodeAPIProvider } from '@codeblitzjs/ide-code-api'; +import { CodePlatformRegistry, ICodeAPIProvider } from '@codeblitzjs/ide-code-api'; import { fsExtra } from '@codeblitzjs/ide-sumi-core'; import { Autowired, Injectable } from '@opensumi/di'; import { Deferred, Emitter } from '@opensumi/ide-core-common'; @@ -41,6 +41,10 @@ export class Repository implements IRepositoryModel { return this._platform; } + get platformConfig() { + return CodePlatformRegistry.instance().getPlatformConfig(this.platform); + } + /** * 仓库群组或用户 */ diff --git a/packages/code-service/src/static-resource.contribution.ts b/packages/code-service/src/static-resource.contribution.ts index 70f2ce33..250e585c 100644 --- a/packages/code-service/src/static-resource.contribution.ts +++ b/packages/code-service/src/static-resource.contribution.ts @@ -1,4 +1,4 @@ -import { CODE_PLATFORM_CONFIG, CodePlatform, ICodeAPIProvider } from '@codeblitzjs/ide-code-api'; +import { CodePlatform, CodePlatformRegistry, ICodeAPIProvider } from '@codeblitzjs/ide-code-api'; import { AppConfig, RuntimeConfig } from '@codeblitzjs/ide-sumi-core'; import { Autowired } from '@opensumi/di'; import { Domain, URI } from '@opensumi/ide-core-browser'; @@ -24,6 +24,8 @@ export class CodeStaticResourceContribution implements StaticResourceContributio codeModel: CodeModelService; registerStaticResolver(staticService: StaticResourceService) { + const configs = CodePlatformRegistry.instance().getCodePlatformConfigs(); + staticService.registerStaticResourceProvider({ scheme: 'file', resolveStaticResource: (uri: URI) => { @@ -37,10 +39,10 @@ export class CodeStaticResourceContribution implements StaticResourceContributio return uri; }, roots: [ - CODE_PLATFORM_CONFIG[CodePlatform.github].origin, - CODE_PLATFORM_CONFIG[CodePlatform.gitlab].origin, - CODE_PLATFORM_CONFIG[CodePlatform.gitlink].origin, - CODE_PLATFORM_CONFIG[CodePlatform.atomgit].origin, + configs[CodePlatform.github].origin, + configs[CodePlatform.gitlab].origin, + configs[CodePlatform.gitlink].origin, + configs[CodePlatform.atomgit].origin, ], }); } diff --git a/packages/code-service/src/types.ts b/packages/code-service/src/types.ts index 09370db7..5518ece4 100644 --- a/packages/code-service/src/types.ts +++ b/packages/code-service/src/types.ts @@ -1,66 +1,63 @@ import type { - ICodePlatform, - RefsParam, + EntryFileType, EntryInfo, EntryParam, - TreeEntry, - IRepositoryModel, - EntryFileType, ICodeAPIService, -} from '@codeblitzjs/ide-code-api'; - -export type { ICodePlatform, + IRepositoryModel, RefsParam, - EntryInfo, - EntryParam, TreeEntry, - EntryFileType, - IRepositoryModel, -}; +} from '@codeblitzjs/ide-code-api'; + +export type { EntryFileType, EntryInfo, EntryParam, ICodePlatform, IRepositoryModel, RefsParam, TreeEntry }; export { CodePlatform } from '@codeblitzjs/ide-code-api'; export const ICodeServiceConfig = Symbol('ICodeServiceConfig'); -export type ICodeServiceConfig = { - /** 平台 */ - platform: ICodePlatform; - /** 群组或用户 */ - owner: string; - /** 仓库名 */ - name: string; - /** 从代码托管平台跳转过来的路径,解析出 ref 和默认打开的文件,如 blob/master/README.md */ - refPath?: string; - /** ref */ - ref?: string; - /** tag */ - tag?: string; - /** branch */ - branch?: string; - /** commit sha */ - commit?: string; - /** url hash */ - hash?: string; - /** 仓库id */ - projectId?: string; -} & { - /** submodules 多平台配置 */ - [key in ICodePlatform]?: { - hostname?: string[]; - /** location.origin */ - origin?: string; - /** 用于接口请求,不设置为 origin */ - endpoint?: string; - /** api 请求 token,上层可预设 token */ - token?: string; - /** 文件存储系统 默认 IndexedDB 全局缓存 */ - isInMemory?: boolean; - /** 是否递归获取文件 只请求一次文件列表 */ - recursive?: boolean; +interface ICodePlatformConfig { + hostname?: string[]; + /** location.origin */ + origin?: string; + /** 用于接口请求,不设置为 origin */ + endpoint?: string; + /** api 请求 token,上层可预设 token */ + token?: string; + /** 文件存储系统 默认 IndexedDB 全局缓存 */ + isInMemory?: boolean; + /** 是否递归获取文件 只请求一次文件列表 */ + recursive?: boolean; + [key: string]: any; +} + +export type ICodeServiceConfig = + & { + /** 平台 */ + platform: ICodePlatform | string; + /** 群组或用户 */ + owner: string; + /** 仓库名 */ + name: string; + /** 从代码托管平台跳转过来的路径,解析出 ref 和默认打开的文件,如 blob/master/README.md */ + refPath?: string; + /** ref */ + ref?: string; + /** tag */ + tag?: string; + /** branch */ + branch?: string; + /** commit sha */ + commit?: string; + /** url hash */ + hash?: string; + /** 仓库id */ + projectId?: string; [key: string]: any; + } + & { + /** submodules 多平台配置 */ + [key in ICodePlatform]?: ICodePlatformConfig; }; -}; export type InitializeState = | 'Uninitialized' @@ -70,7 +67,7 @@ export type InitializeState = /** * 无需 Remote */ -export const enum RefType { +export enum RefType { Head, Tag, } @@ -99,15 +96,14 @@ export interface Submodule { } export interface ProjectDesc { - platform: ICodePlatform; + platform: ICodePlatform | string; owner: string; name: string; } type Tail = T extends [IRepositoryModel, ...infer P] ? P : T; -type Carry = F extends (...args: any[]) => any - ? (...args: Tail>) => ReturnType +type Carry = F extends (...args: any[]) => any ? (...args: Tail>) => ReturnType : F; export type ICodeAPIProxy = { diff --git a/packages/code-service/src/utils.ts b/packages/code-service/src/utils.ts index cd015670..b3863d51 100644 --- a/packages/code-service/src/utils.ts +++ b/packages/code-service/src/utils.ts @@ -1,7 +1,7 @@ -import { CODE_PLATFORM_CONFIG } from '@codeblitzjs/ide-code-api'; +import { ICodePlatformConfig } from '@codeblitzjs/ide-code-api'; import { getDebugLogger, Uri } from '@opensumi/ide-core-common'; import { sep } from 'path'; -import { ICodePlatform, ProjectDesc, Submodule } from './types'; +import { ProjectDesc, Submodule } from './types'; export const HEAD = 'HEAD'; @@ -66,7 +66,7 @@ export function parseGitmodules(raw: string): Submodule[] { return result; } -export const parseSubmoduleUrl = (url: string): ProjectDesc | null => { +export const parseSubmoduleUrl = (url: string, configs: Record): ProjectDesc | null => { let authority = ''; let path = ''; if (url.startsWith('git@')) { @@ -78,8 +78,8 @@ export const parseSubmoduleUrl = (url: string): ProjectDesc | null => { authority = submoduleUri.authority; path = submoduleUri.path; } - const targetPlatform = Object.keys(CODE_PLATFORM_CONFIG).find((platform: ICodePlatform) => { - const config = CODE_PLATFORM_CONFIG[platform]; + const targetPlatform = Object.keys(configs).find((platform: string) => { + const config = configs[platform]; return config.hostname.some((item) => new RegExp(`\\b${item}$`).test(authority)); }); if (!targetPlatform) { @@ -91,7 +91,7 @@ export const parseSubmoduleUrl = (url: string): ProjectDesc | null => { const [owner, name] = path.split('/').filter(Boolean); return { - platform: targetPlatform as ICodePlatform, + platform: targetPlatform as string, owner, name, }; diff --git a/packages/sumi-core/src/server/node/extend/fs-watcher.ts b/packages/sumi-core/src/server/node/extend/fs-watcher.ts index 335dec7a..c5c46c1b 100644 --- a/packages/sumi-core/src/server/node/extend/fs-watcher.ts +++ b/packages/sumi-core/src/server/node/extend/fs-watcher.ts @@ -10,7 +10,7 @@ import debounce from 'lodash.debounce'; import * as path from 'path'; import { fs } from '../bfs'; -const enum ActionType { +enum ActionType { CREATED = 0, DELETED = 1, MODIFIED = 2, @@ -88,13 +88,13 @@ api.forEach(({ name, action }) => { directory: path.dirname(p), ...(name !== 'rename' ? { - file: path.basename(p), - } + file: path.basename(p), + } : { - oldFile: path.basename(p), - newDirectory: path.dirname(args[0]), - newFile: path.basename(args[0]), - }), + oldFile: path.basename(p), + newDirectory: path.dirname(args[0]), + newFile: path.basename(args[0]), + }), } as FileChangeEvent); } return res; @@ -113,13 +113,13 @@ apiSync.forEach(({ name, action }) => { directory: path.dirname(p), ...(name !== 'renameSync' ? { - file: path.basename(p), - } + file: path.basename(p), + } : { - oldFile: path.basename(p), - newDirectory: path.dirname(args[0]), - newFile: path.basename(args[0]), - }), + oldFile: path.basename(p), + newDirectory: path.dirname(args[0]), + newFile: path.basename(args[0]), + }), } as FileChangeEvent); return res; }; @@ -284,14 +284,14 @@ interface FWFunction { ( watchPath: string, eventCallback: (events: Array) => void, - options?: Partial + options?: Partial, ): Promise; } export const watch: FWFunction = ( dirOrFile: string, eventHandler: (events: ChangeEvent[]) => void, - { debounceMS = 500 } = {} + { debounceMS = 500 } = {}, ) => { if (Number.isInteger(debounceMS)) { if (debounceMS < 1) { @@ -369,7 +369,7 @@ export const watch: FWFunction = ( } }, null, - disposables + disposables, ); }, stop() { diff --git a/scripts/release.js b/scripts/release.js index beb66568..055b8b99 100644 --- a/scripts/release.js +++ b/scripts/release.js @@ -88,7 +88,7 @@ invoke(async () => { const { yes } = await prompt({ type: 'confirm', name: 'yes', - message: `确认发布 v${targetVersion} (tag: ${args.tag || 'latest'})}`, + message: `确认发布 v${targetVersion} (tag: ${args.tag || 'latest'})`, }); if (!yes) {