diff --git a/packages/plugin-eval-addons/src/index.ts b/packages/plugin-eval-addons/src/index.ts index c791db7601..fcda4509b1 100644 --- a/packages/plugin-eval-addons/src/index.ts +++ b/packages/plugin-eval-addons/src/index.ts @@ -1,19 +1,24 @@ import { Context, CommandConfig, OptionConfig, User } from 'koishi-core' -import { resolve } from 'path' -import {} from 'koishi-plugin-eval' import { assertProperty, Logger, noop } from 'koishi-utils' +import { resolve } from 'path' import { safeLoad } from 'js-yaml' import { promises as fs } from 'fs' +import {} from 'koishi-plugin-eval' import Git, { CheckRepoActions } from 'simple-git' const logger = new Logger('addon') +type AddonConfig = Config + export interface Config { gitRemote?: string - moduleRoot?: string exclude?: RegExp } +declare module 'koishi-plugin-eval' { + interface MainConfig extends AddonConfig {} +} + interface OptionManifest extends OptionConfig { name: string desc: string @@ -39,10 +44,11 @@ interface Manifest { export function apply(ctx: Context, config: Config) { const { evalConfig } = ctx.app Object.assign(evalConfig, config) - const moduleRoot = assertProperty(evalConfig, 'moduleRoot') + const root = resolve(process.cwd(), assertProperty(evalConfig, 'moduleRoot')) + evalConfig.moduleRoot = root + evalConfig.dataKeys.push('addonNames', 'moduleRoot') evalConfig.setupFiles['koishi/addons.ts'] = resolve(__dirname, 'worker.js') - const root = resolve(process.cwd(), moduleRoot) const git = Git(root) const addon = ctx.command('addon', '扩展功能') @@ -59,7 +65,7 @@ export function apply(ctx: Context, config: Config) { ctx.on('before-connect', async () => { const isRepo = await git.checkIsRepo(CheckRepoActions.IS_REPO_ROOT) - if (!isRepo) throw new Error(`moduleRoot "${moduleRoot}" is not git repository`) + if (!isRepo) throw new Error(`moduleRoot "${root}" is not git repository`) }) let manifests: Record> diff --git a/packages/plugin-eval-addons/src/worker.ts b/packages/plugin-eval-addons/src/worker.ts index 11e43480fb..56e268c67c 100644 --- a/packages/plugin-eval-addons/src/worker.ts +++ b/packages/plugin-eval-addons/src/worker.ts @@ -3,7 +3,6 @@ import { promises, readFileSync } from 'fs' import { resolve, posix, dirname } from 'path' import { User } from 'koishi-core' import { Logger, Time, CQCode, Random } from 'koishi-utils' -import { Config } from '.' import json5 from 'json5' import ts from 'typescript' @@ -12,7 +11,9 @@ const logger = new Logger('addon') const { SourceTextModule, SyntheticModule } = require('vm') declare module 'koishi-plugin-eval/dist/worker' { - interface WorkerConfig extends Config {} + interface WorkerConfig { + moduleRoot?: string + } interface WorkerData { addonNames: string[] @@ -58,7 +59,6 @@ interface Module { evaluate(): Promise } -const root = resolve(process.cwd(), config.moduleRoot) export const modules: Record = {} export function synthetize(identifier: string, namespace: {}) { @@ -111,14 +111,14 @@ async function linker(specifier: string, { identifier }: Module) { throw new Error(`Unable to resolve dependency "${specifier}" in "${identifier}"`) } -const json = json5.parse(readFileSync(resolve(root, 'tsconfig.json'), 'utf8')) -const { options: compilerOptions } = ts.parseJsonConfigFileContent(json, ts.sys, root) +const json = json5.parse(readFileSync(resolve(config.moduleRoot, 'tsconfig.json'), 'utf8')) +const { options: compilerOptions } = ts.parseJsonConfigFileContent(json, ts.sys, config.moduleRoot) async function loadSource(path: string) { for (const postfix of suffixes) { try { const target = path + postfix - return [await promises.readFile(resolve(root, target), 'utf8'), target] + return [await promises.readFile(resolve(config.moduleRoot, target), 'utf8'), target] } catch {} } throw new Error(`cannot load source file "${path}"`) diff --git a/packages/plugin-eval/src/index.ts b/packages/plugin-eval/src/index.ts index 4771609f6e..7068e2b506 100644 --- a/packages/plugin-eval/src/index.ts +++ b/packages/plugin-eval/src/index.ts @@ -1,5 +1,5 @@ import { App, Context, Session } from 'koishi-core' -import { CQCode, Logger, defineProperty, omit, Random } from 'koishi-utils' +import { CQCode, Logger, defineProperty, Random, pick } from 'koishi-utils' import { Worker, ResourceLimits } from 'worker_threads' import { WorkerAPI, WorkerConfig, WorkerData, Response } from './worker' import { wrap, expose, Remote } from './transfer' @@ -22,9 +22,9 @@ declare module 'koishi-core/dist/command' { declare module 'koishi-core/dist/context' { interface EventMap { - 'worker/start' (): void | Promise - 'worker/ready' (response: Response): void - 'worker/exit' (): void + 'worker/start'(): void | Promise + 'worker/ready'(response: Response): void + 'worker/exit'(): void } } @@ -36,22 +36,24 @@ declare module 'koishi-core/dist/session' { } } -interface MainConfig { +export interface MainConfig { prefix?: string timeout?: number maxLogs?: number resourceLimits?: ResourceLimits + dataKeys?: (keyof WorkerData)[] } -interface EvalConfig extends MainConfig, WorkerData {} +export interface EvalConfig extends MainConfig, WorkerData {} export interface Config extends MainConfig, WorkerConfig {} -const defaultConfig: Config = { +const defaultConfig: EvalConfig = { prefix: '>', timeout: 1000, setupFiles: {}, maxLogs: Infinity, + dataKeys: ['inspect', 'setupFiles'], } const logger = new Logger('eval') @@ -107,7 +109,7 @@ export function apply(ctx: Context, config: Config = {}) { workerData: { entry: process.env.KOISHI_WORKER_ENTRY, logLevels: Logger.levels, - ...omit(config, ['maxLogs', 'resourceLimits', 'timeout']), + ...pick(config, config.dataKeys), }, resourceLimits: config.resourceLimits, })