Skip to content

Commit

Permalink
feat(market): loader.market now accept search result
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jun 23, 2023
1 parent a7069f9 commit 44c2842
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
7 changes: 4 additions & 3 deletions packages/online/app/loader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { makeArray } from '@koishijs/core'
import { MarketResult } from '@koishijs/registry'
import { SearchResult } from '@koishijs/registry'
import { Loader, unwrapExports } from '@koishijs/loader'
import { global } from '@koishijs/client'
import process from 'process'
Expand All @@ -22,7 +22,7 @@ if (process.env.NODE_ENV !== 'development') {
class BrowserLoader extends Loader {
public envData: any = {}
public config: any = { plugins: {} }
public market: MarketResult
public market: SearchResult

async init(filename?: string) {
await super.init(filename)
Expand All @@ -33,7 +33,8 @@ class BrowserLoader extends Loader {
if (process.env.NODE_ENV === 'development') return
this.market = await fetch(global.endpoint + '/play.json').then(res => res.json())
for (const object of this.market.objects) {
this.cache[object.shortname] = `${global.endpoint}/modules/${object.name}/index.js`
const shortname = object.package.name.replace(/(koishi-|^@koishijs\/)plugin-/, '')
this.cache[shortname] = `${global.endpoint}/modules/${object.package.name}/index.js`
}
}

Expand Down
8 changes: 5 additions & 3 deletions packages/registry/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,16 @@ export interface Extension {
score: Score
rating: number
verified: boolean
license: string
manifest: Manifest
publishSize?: number
installSize?: number
downloads?: {
lastMonth: number
}
}

export interface SearchObject extends Extension {
export interface SearchObject extends Extension, Pick<RemotePackage, 'deprecated' | 'peerDependencies' | 'peerDependenciesMeta'> {
package: SearchPackage
searchScore: number
ignored?: boolean
Expand Down Expand Up @@ -165,8 +167,6 @@ export interface SharedPackage extends DatedPackage {
export interface AnalyzedPackage extends SearchPackage, Extension {
contributors: User[]
shortname: string
license: string
manifest: Manifest
createdAt: string
updatedAt: string
versions?: Dict<Partial<RemotePackage>>
Expand All @@ -177,6 +177,7 @@ export interface CollectConfig {
margin?: number
timeout?: number
ignored?: string[]
endpoint?: string
}

export interface AnalyzeConfig {
Expand Down Expand Up @@ -264,6 +265,7 @@ export default class Scanner {
private async search(offset: number, config: CollectConfig) {
const { step = 250, timeout = Time.second * 30 } = config
const result = await this.request<SearchResult>(`/-/v1/search?text=koishi+plugin&size=${step}&from=${offset}`, { timeout })
this.version = result.version
for (const object of result.objects) {
this.cache[object.package.name] = object
}
Expand Down
31 changes: 17 additions & 14 deletions plugins/config/src/browser/packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,29 @@ import * as shared from '../shared'
export class PackageProvider extends shared.PackageProvider {
async getManifest(name: string) {
return this.ctx.loader.market.objects.find(item => {
return name === item.name.replace(/(koishi-|^@koishijs\/)plugin-/, '')
return name === item.package.name.replace(/(koishi-|^@koishijs\/)plugin-/, '')
})?.manifest
}

async get(forced = false) {
const packages = await Promise.all(this.ctx.loader.market.objects.map(async (data) => {
const result = pick(data, [
'name',
'version',
'description',
'portable',
'manifest',
]) as shared.PackageProvider.Data
result.shortname = data.name.replace(/(koishi-|^@koishijs\/)plugin-/, '')
result.manifest = data.manifest
result.peerDependencies = { ...data.versions[data.version].peerDependencies }
result.peerDependenciesMeta = { ...data.versions[data.version].peerDependenciesMeta }
const result = {
...pick(data, [
'manifest',
'peerDependencies',
'peerDependenciesMeta',
]),
...pick(data.package, [
'name',
'version',
'description',
'portable',
]),
} as shared.PackageProvider.Data
result.shortname = result.name.replace(/(koishi-|^@koishijs\/)plugin-/, '')
if (!result.portable) return
result.runtime = await this.parseExports(data.name, () => {
return this.ctx.loader.resolvePlugin(data.shortname)
result.runtime = await this.parseExports(result.name, () => {
return this.ctx.loader.resolvePlugin(result.shortname)
})
return result
}))
Expand Down
4 changes: 2 additions & 2 deletions plugins/config/src/shared/packages.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Context, Dict, EffectScope, Logger, Schema } from 'koishi'
import { DataService } from '@koishijs/plugin-console'
import { Manifest, MarketResult, PackageJson } from '@koishijs/registry'
import { Manifest, PackageJson, SearchResult } from '@koishijs/registry'
import { debounce } from 'throttle-debounce'

declare module '@koishijs/loader' {
interface Loader {
market: MarketResult
market: SearchResult
}
}

Expand Down

0 comments on commit 44c2842

Please sign in to comment.