Skip to content

Commit

Permalink
fix(config): load full list of local packages, fix #265
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Dec 20, 2023
1 parent f280e60 commit 663241f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
19 changes: 8 additions & 11 deletions packages/registry/src/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { conclude } from './utils'
export interface LocalScanner extends SearchResult {}

export class LocalScanner {
cache: Dict<SearchObject>
task: Promise<void>
private cache: Dict<Promise<SearchObject>>
private task: Promise<SearchObject[]>

constructor(public baseDir: string) {
defineProperty(this, 'cache', {})
Expand All @@ -29,26 +29,26 @@ export class LocalScanner {
root = parent
}
await Promise.all(tasks)
return Promise.all(Object.values(this.cache))
}

async collect(forced = false) {
if (forced) delete this.task
await (this.task ||= this._collect())
this.objects = Object.values(this.cache)
this.objects = await (this.task ||= this._collect())
}

private async loadDirectory(baseDir: string) {
const base = baseDir + '/node_modules'
const files = await readdir(base).catch(() => [])
for (const name of files) {
if (name.startsWith('koishi-plugin-')) {
this.loadPackage(name)
this.cache[name] ||= this.loadPackage(name)
} else if (name.startsWith('@')) {
const base2 = base + '/' + name
const files = await readdir(base2).catch(() => [])
for (const name2 of files) {
if (name === '@koishijs' && name2.startsWith('plugin-') || name2.startsWith('koishi-plugin-')) {
this.loadPackage(name + '/' + name2)
this.cache[name + '/' + name2] ||= this.loadPackage(name + '/' + name2)
}
}
}
Expand All @@ -57,10 +57,7 @@ export class LocalScanner {

private async loadPackage(name: string) {
try {
// require.resolve(name) may be different from require.resolve(path)
// because tsconfig-paths may resolve the path differently
const entry = require.resolve(name)
this.cache[entry] = await this.parsePackage(name, entry)
return await this.parsePackage(name)
} catch (error) {
this.onError(error, name)
}
Expand All @@ -74,7 +71,7 @@ export class LocalScanner {
return [meta, !filename.includes('node_modules')] as const
}

protected async parsePackage(name: string, entry: string) {
protected async parsePackage(name: string) {
const [data, workspace] = await this.loadManifest(name)
return {
workspace,
Expand Down
17 changes: 12 additions & 5 deletions plugins/config/src/node/packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@ class PackageScanner extends LocalScanner {
logger.warn(error)
}

async parsePackage(name: string, entry: string) {
const result = await super.parsePackage(name, entry)
if (require.cache[entry]) {
name = name.replace(/(koishi-|^@koishijs\/)plugin-/, '')
this.service.cache[name] = await this.service.parseExports(name)
async parsePackage(name: string) {
const result = await super.parsePackage(name)
try {
// require.resolve(name) may be different from require.resolve(path)
// because tsconfig-paths may resolve the path differently
const entry = require.resolve(name)
if (require.cache[entry]) {
name = name.replace(/(koishi-|^@koishijs\/)plugin-/, '')
this.service.cache[name] = await this.service.parseExports(name)
}
} catch (error) {
this.onError(error, name)
}
return result
}
Expand Down
14 changes: 10 additions & 4 deletions plugins/market/src/node/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,18 @@ class Installer extends Service {
return this.depTask ||= this._getDeps()
}

refreshData() {
this.ctx.get('console.registry')?.refresh()
this.ctx.get('console.packages')?.refresh()
}

refresh(refresh = false) {
this.pkgTasks = {}
this.fullCache = {}
this.tempCache = {}
this.depTask = this._getDeps()
if (!refresh) return
this.ctx.get('console.registry')?.refresh()
this.ctx.get('console.packages')?.refresh()
this.refreshData()
}

async exec(command: string, args: string[]) {
Expand Down Expand Up @@ -228,11 +232,13 @@ class Installer extends Service {
try {
if (!(require.resolve(name) in require.cache)) continue
} catch (error) {
logger.warn(error)
continue
// FIXME https://github.com/koishijs/webui/issues/265
// I have no idea why this happens and how to fix it.
logger.error(error)
}
this.ctx.loader.fullReload()
}
this.refreshData()

return 0
}
Expand Down

0 comments on commit 663241f

Please sign in to comment.