Skip to content

Commit

Permalink
feat(loader): support loader.keyFor()
Browse files Browse the repository at this point in the history
given plugin, returns short name
  • Loading branch information
shigma committed Jun 27, 2023
1 parent 9d6ee0d commit 1303744
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 17 deletions.
2 changes: 1 addition & 1 deletion packages/koishi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
],
"dependencies": {
"@koishijs/core": "4.13.5",
"@koishijs/loader": "3.2.5",
"@koishijs/loader": "3.3.0",
"@koishijs/utils": "^7.0.2",
"@satorijs/satori": "^2.4.0",
"cac": "^6.7.14",
Expand Down
3 changes: 2 additions & 1 deletion packages/loader/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@koishijs/loader",
"description": "Config Loader for Koishi",
"version": "3.2.5",
"version": "3.3.0",
"main": "lib/index.js",
"module": "lib/shared.mjs",
"typings": "lib/index.d.ts",
Expand All @@ -14,6 +14,7 @@
"require": "./lib/shared.js",
"import": "./lib/shared.mjs"
},
"./src/*": "./src/*",
"./package.json": "./package.json"
},
"files": [
Expand Down
10 changes: 3 additions & 7 deletions packages/loader/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Logger } from '@koishijs/core'
import { Loader, unwrapExports } from './shared'
import { Loader } from './shared'
import { promises as fs } from 'fs'
import * as dotenv from 'dotenv'
import ns from 'ns-require'
Expand Down Expand Up @@ -55,18 +55,14 @@ export default class NodeLoader extends Loader {
return await super.readConfig()
}

async resolve(name: string) {
return this.scope.resolve(name)
}

async resolvePlugin(name: string) {
async import(name: string) {
try {
this.cache[name] ||= this.scope.resolve(name)
} catch (err) {
logger.error(err.message)
return
}
return unwrapExports(require(this.cache[name]))
return require(this.cache[name])
}

fullReload(code = Loader.exitCode) {
Expand Down
15 changes: 13 additions & 2 deletions packages/loader/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@ export abstract class Loader {
public cache: Dict<string> = Object.create(null)
public prolog?: Logger.Record[]

abstract resolve(name: string): Promise<string>
abstract resolvePlugin(name: string): Promise<any>
private store = new WeakMap<Plugin, string>()

abstract import(name: string): Promise<any>
abstract fullReload(code?: number): void

async init(filename?: string) {
Expand Down Expand Up @@ -215,6 +216,16 @@ export abstract class Loader {
}
}

async resolvePlugin(name: string) {
const plugin = unwrapExports(await this.import(name))
if (plugin) this.store.set(plugin, name)
return plugin
}

keyFor(plugin: any) {
return this.store.get(plugin)
}

private async forkPlugin(name: string, config: any, parent: Context) {
const plugin = await this.resolvePlugin(name)
if (!plugin) return
Expand Down
4 changes: 2 additions & 2 deletions packages/loader/tests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'chai'
import { Context } from 'koishi'
import { Context, sleep } from 'koishi'
import * as jest from 'jest-mock'
import mock from '@koishijs/plugin-mock'
import Loader from './utils'
Expand Down Expand Up @@ -53,7 +53,7 @@ describe('@koishijs/loader', () => {
},
}
app.scope.update(loader.config)
await app.lifecycle.flush()
await sleep(0)
expect(app.config.prefix).to.deep.equal(['/'])
expect(app.registry.get(loader.data.foo)).to.be.not.ok
expect(app.registry.get(loader.data.bar)).to.be.ok
Expand Down
6 changes: 2 additions & 4 deletions packages/loader/tests/utils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { Dict, Logger, Plugin } from 'koishi'
import { Dict, Plugin } from 'koishi'
import { Loader } from '../src'
import * as jest from 'jest-mock'

const logger = new Logger('app')

export default class TestLoader extends Loader {
data: Dict<Plugin.Object> = Object.create(null)

async resolvePlugin(name: string) {
async import(name: string) {
return this.data[name] ||= {
name,
apply: (ctx) => {
Expand Down

0 comments on commit 1303744

Please sign in to comment.