@koishijs/loader
Advanced tools
Comparing version 2.5.0 to 2.5.1
import { Context, Dict, EnvData } from '@koishijs/core'; | ||
export * from './utils'; | ||
declare module '@koishijs/core' { | ||
@@ -23,2 +22,7 @@ interface Context { | ||
} | ||
export declare function unwrapExports(module: any): any; | ||
export interface Modifier { | ||
$if?: boolean; | ||
$filter?: Selection; | ||
} | ||
export declare abstract class Loader { | ||
@@ -46,5 +50,5 @@ static readonly kRecord: unique symbol; | ||
isTruthyLike(expr: any): boolean; | ||
reloadPlugin(parent: Context, key: string, config: any): Promise<import("cordis").ForkScope<Context>>; | ||
reloadPlugin(parent: Context, key: string, source: any): Promise<import("cordis").ForkScope<Context>>; | ||
unloadPlugin(ctx: Context, key: string): void; | ||
createApp(): Promise<Context>; | ||
} |
@@ -24,11 +24,5 @@ var __defProp = Object.defineProperty; | ||
Loader: () => Loader, | ||
Modifier: () => Modifier, | ||
patch: () => patch, | ||
select: () => select, | ||
unwrapExports: () => unwrapExports | ||
}); | ||
module.exports = __toCommonJS(shared_exports); | ||
var import_core2 = require("@koishijs/core"); | ||
// koishi/packages/loader/src/utils.ts | ||
var import_core = require("@koishijs/core"); | ||
@@ -39,62 +33,17 @@ function unwrapExports(module2) { | ||
__name(unwrapExports, "unwrapExports"); | ||
var Modifier; | ||
((Modifier2) => { | ||
function pick(config, positive = false) { | ||
const result = {}; | ||
for (const [key, value] of Object.entries(config || {})) { | ||
if (key.startsWith("$") !== positive) | ||
continue; | ||
result[key] = value; | ||
function separate(source, isGroup = false) { | ||
const config = {}, meta = {}; | ||
for (const [key, value] of Object.entries(source || {})) { | ||
if (key.startsWith("$")) { | ||
meta[key] = value; | ||
} else { | ||
config[key] = value; | ||
} | ||
return result; | ||
} | ||
Modifier2.pick = pick; | ||
__name(pick, "pick"); | ||
})(Modifier || (Modifier = {})); | ||
var selectors = ["user", "guild", "channel", "self", "private", "platform"]; | ||
function select(root, options) { | ||
let ctx = root; | ||
for (const type of selectors) { | ||
const value = options[type]; | ||
if (value === true) { | ||
ctx = ctx[type](); | ||
} else if (value === false) { | ||
ctx = ctx.exclude(ctx[type]()); | ||
} else if (value !== void 0) { | ||
ctx = ctx[type](...(0, import_core.makeArray)(value).map((item) => "" + item)); | ||
} | ||
} | ||
if (options.and) { | ||
for (const selection of options.and) { | ||
ctx = ctx.intersect(select(root, selection)); | ||
} | ||
} | ||
if (options.or) { | ||
let ctx2 = ctx.never(); | ||
for (const selection of options.or) { | ||
ctx2 = ctx2.union(select(root, selection)); | ||
} | ||
ctx = ctx.intersect(ctx2); | ||
} | ||
if (options.not) { | ||
ctx = ctx.exclude(select(root, options.not)); | ||
} | ||
return ctx; | ||
return [isGroup ? source : config, meta]; | ||
} | ||
__name(select, "select"); | ||
function patch(ctx, config) { | ||
config != null ? config : config = {}; | ||
const parent = Object.getPrototypeOf(ctx); | ||
if (config.$filter) { | ||
ctx.filter = parent.intersect(select(ctx.root, config.$filter)).filter; | ||
} else { | ||
delete ctx.filter; | ||
} | ||
} | ||
__name(patch, "patch"); | ||
// koishi/packages/loader/src/shared.ts | ||
__name(separate, "separate"); | ||
var kUpdate = Symbol("update"); | ||
import_core2.Context.service("loader"); | ||
var logger = new import_core2.Logger("app"); | ||
import_core.Context.service("loader"); | ||
var logger = new import_core.Logger("app"); | ||
var group = { | ||
@@ -139,3 +88,3 @@ name: "group", | ||
if (typeof source === "string") { | ||
return (0, import_core2.interpolate)(source, this.ctxData, /\$\{\{(.+?)\}\}/g); | ||
return (0, import_core.interpolate)(source, this.ctxData, /\$\{\{(.+?)\}\}/g); | ||
} else if (!source || typeof source !== "object") { | ||
@@ -146,3 +95,3 @@ return source; | ||
} else { | ||
return (0, import_core2.valueMap)(source, (item) => this.interpolate(item)); | ||
return (0, import_core.valueMap)(source, (item) => this.interpolate(item)); | ||
} | ||
@@ -154,34 +103,29 @@ } | ||
return; | ||
(0, import_core2.resolveConfig)(plugin, config); | ||
(0, import_core.resolveConfig)(plugin, config); | ||
return parent.plugin(plugin, this.interpolate(config)); | ||
} | ||
isTruthyLike(expr) { | ||
if ((0, import_core2.isNullable)(expr)) | ||
if ((0, import_core.isNullable)(expr)) | ||
return true; | ||
return !!this.interpolate(`\${{ ${expr} }}`); | ||
} | ||
async reloadPlugin(parent, key, config) { | ||
async reloadPlugin(parent, key, source) { | ||
let fork = parent.state[_Loader.kRecord][key]; | ||
const name = key.split(":", 1)[0]; | ||
const [config, meta] = separate(source, name === "group"); | ||
if (fork) { | ||
if (!this.isTruthyLike(config == null ? void 0 : config.$if)) { | ||
if (!this.isTruthyLike(meta.$if)) { | ||
this.unloadPlugin(parent, key); | ||
return; | ||
} | ||
patch(fork.parent, config); | ||
fork[kUpdate] = true; | ||
if (fork.runtime.plugin !== group) { | ||
config = Modifier.pick(config, false); | ||
} | ||
fork.update(config); | ||
} else { | ||
if (!this.isTruthyLike(config == null ? void 0 : config.$if)) | ||
if (!this.isTruthyLike(meta.$if)) | ||
return; | ||
logger.info(`apply plugin %c`, key); | ||
const name = key.split(":", 1)[0]; | ||
const ctx = parent.extend(); | ||
patch(ctx, config); | ||
if (name === "group") { | ||
fork = ctx.plugin(group, config); | ||
} else { | ||
config = Modifier.pick(config, false); | ||
fork = await this.forkPlugin(name, config, ctx); | ||
@@ -194,2 +138,5 @@ } | ||
} | ||
fork.parent.filter = (session) => { | ||
return parent.filter(session) && (!meta.$filter || session.resolve(meta.$filter)); | ||
}; | ||
return fork; | ||
@@ -206,3 +153,3 @@ } | ||
async createApp() { | ||
const app = this.app = new import_core2.Context(this.interpolate(this.config)); | ||
const app = this.app = new import_core.Context(this.interpolate(this.config)); | ||
app.loader = this; | ||
@@ -242,3 +189,3 @@ app.baseDir = this.baseDir; | ||
fork2.parent.state.config[name] = { | ||
...Modifier.pick(fork2.parent.state.config[name], true), | ||
...separate(fork2.parent.state.config[name])[1], | ||
...simplify ? simplify(config) : config | ||
@@ -258,7 +205,4 @@ }; | ||
Loader, | ||
Modifier, | ||
patch, | ||
select, | ||
unwrapExports | ||
}); | ||
//# sourceMappingURL=shared.js.map |
{ | ||
"name": "@koishijs/loader", | ||
"description": "Config Loader for Koishi", | ||
"version": "2.5.0", | ||
"version": "2.5.1", | ||
"main": "lib/node.js", | ||
@@ -41,6 +41,6 @@ "typings": "lib/index.d.ts", | ||
"peerDependencies": { | ||
"@koishijs/core": "^4.11.0" | ||
"@koishijs/core": "^4.11.1" | ||
}, | ||
"devDependencies": { | ||
"@koishijs/core": "^4.11.0", | ||
"@koishijs/core": "^4.11.1", | ||
"@koishijs/registry": "^4.2.7", | ||
@@ -47,0 +47,0 @@ "@types/js-yaml": "^4.0.5" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
54285
708