diff --git a/src/global.d.ts b/src/global.d.ts index 2b6634f..2b85467 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -13,12 +13,40 @@ type LogType = string | Error | unknown * */ type LoggerType = { + /** + *痕迹 + * @param arg + */ trace(...arg: LogType[]): any + /** + *调试 + * @param arg + */ debug(...arg: LogType[]): any + /** + *信息 + * @param arg + */ info(...arg: LogType[]): any + /** + *警告 + * @param arg + */ warn(...arg: LogType[]): any + /** + *错误 + * @param arg + */ error(...arg: LogType[]): any + /** + *致命 + * @param arg + */ fatal(...arg: LogType[]): any + /** + *标记 + * @param arg + */ mark(...arg: LogType[]): any } @@ -27,26 +55,32 @@ type LoggerType = { */ type ChalkInstanceType = { /** + * 红色的 * @deprecated 不推荐使用 */ red: ChalkInstance.red /** + * 绿色的 * @deprecated 不推荐使用 */ green: ChalkInstance.green /** + * 蓝色的 * @deprecated 不推荐使用 */ blue: ChalkInstance.blue /** + * 黄色的 * @deprecated 不推荐使用 */ yellow: ChalkInstance.yellow /** + * 品红 * @deprecated 不推荐使用 */ magenta: ChalkInstance.magenta /** + * 青色 * @deprecated 不推荐使用 */ cyan: ChalkInstance.cyan @@ -54,13 +88,15 @@ type ChalkInstanceType = { declare global { /** - * import { Redis } from 'yunzai/core' + * 键值对型数据库 * @deprecated 不推荐使用,未来将废弃 + * import { Redis } from 'yunzai/core' */ var redis: RedisClientType /** - * import { Bot } from 'yunzai/core' + * 机器人客户端 * @deprecated 不推荐使用,未来将废弃 + * import { Bot } from 'yunzai/core' */ var Bot: typeof Client.prototype /** @@ -69,8 +105,8 @@ declare global { */ var segment: typeof se /** - * import { Plugin } from 'yunzai/core' * @deprecated 不推荐使用,未来将废弃 + * import { Plugin } from 'yunzai/core' */ var plugin: typeof p /** diff --git a/src/image/index.ts b/src/image/index.ts index 5753aba..06ec47b 100644 --- a/src/image/index.ts +++ b/src/image/index.ts @@ -6,18 +6,42 @@ import { Dirent, readdirSync } from 'fs' import { join } from 'path' import mount from 'koa-mount' import { Component } from '../utils/index.js' + export * from './types.js' -export async function createServer() { + +const PATH = process.cwd().replace(/\\/g, '\\\\') + +// 辅助函数:替换路径 +const replacePaths = (htmlContent: string) => { + // 置换成 /file请求 + htmlContent = htmlContent.replace(new RegExp(PATH, 'g'), '/file') + // 正则表达式匹配 src、href 和 url 中的路径 + const regex = /(src|href|url)\s*=\s*["']([^"']*\\[^"']*)["']/g + htmlContent = htmlContent.replace(regex, (_, p1, p2) => { + const correctedPath = p2.replace(/\\/g, '/') + return `${p1}="${correctedPath}"` + }) + const cssUrlRegex = /url\(["']?([^"'\)\\]*\\[^"'\)]*)["']?\)/g + return htmlContent.replace(cssUrlRegex, (_, p1) => { + const correctedPath = p1.replace(/\\/g, '/') + return `url(${correctedPath})` + }) +} + +const Dynamic = async (Router: Dirent) => { + const modulePath = `file://${join(Router.parentPath, Router.name)}?update=${Date.now()}` + return (await import(modulePath))?.default +} + +/** + * + * @param Port + */ +export async function createServer(Port = 8080) { // const Com = new Component() const app = new Koa() const router = new Router() - const Port = 8080 - const PATH = process.cwd().replace(/\\/g, '\\\\') - const Dynamic = async (Router: Dirent) => { - const modulePath = `file://${join(Router.parentPath, Router.name)}?update=${Date.now()}` - return (await import(modulePath))?.default - } // 得到plugins目录 const flies = readdirSync(join(process.cwd(), 'plugins'), { @@ -68,23 +92,6 @@ export async function createServer() { } } - // 辅助函数:替换路径 - const replacePaths = htmlContent => { - // 置换成 /file请求 - htmlContent = htmlContent.replace(new RegExp(PATH, 'g'), '/file') - // 正则表达式匹配 src、href 和 url 中的路径 - const regex = /(src|href|url)\s*=\s*["']([^"']*\\[^"']*)["']/g - const cssUrlRegex = /url\(["']?([^"'\)\\]*\\[^"'\)]*)["']?\)/g - htmlContent = htmlContent.replace(regex, (_, p1, p2) => { - const correctedPath = p2.replace(/\\/g, '/') - return `${p1}="${correctedPath}"` - }) - return htmlContent.replace(cssUrlRegex, (_, p1) => { - const correctedPath = p1.replace(/\\/g, '/') - return `url(${correctedPath})` - }) - } - for (const Router of Routers) { router.get(Router.uri, async ctx => { // 动态加载 diff --git a/src/utils/module.ts b/src/utils/module.ts index 48543a7..ddf597b 100644 --- a/src/utils/module.ts +++ b/src/utils/module.ts @@ -24,17 +24,11 @@ export function require(path: string) { const now = () => `?t=${Date.now()}` /** - * @param basePath import.meta.url - * @returns * *********** * 创建动态模块 - * *********** - * 动态模块每次访问都将重新加载, - * 如果动态模块内包含动态模块, - * 内部模块也会跟着重新加载, - * *********** + * @param basePath import.meta.url + * @returns * 在env.NODE_ENV=='production'下禁用 - * @deprecated 实验性的,请勿烂用 */ export const createDynamic = (basePath: string) => { /** @@ -52,36 +46,10 @@ export const createDynamic = (basePath: string) => { } /** - * 动态组件类型 - */ -export type ModuleWithComponent< - ComponentName extends string, - PropsType = any -> = Promise<{ [K in ComponentName]: React.ComponentType }> - -// export const createDynamicComponent = (basePath: string) => { -// /** -// * 与import作用相同 -// * @param path 相对路径 -// * @returns -// */ -// return = { default: any }>( -// path: string -// ): Promise<{ [K in keyof T]: React.ComponentType }> => -// import( -// new URL( -// `${path}${process.env.NODE_ENV == 'production' ? '' : now()}`, -// basePath -// ).href -// ) -// } - -/** - * @param basePath import.meta.url - * @returns * *********** * 创建动态组件 - * *********** + * @param basePath import.meta.url + * @returns * 在env.NODE_ENV=='production'下禁用 */ export const createDynamicComponent = (basePath: string) => { @@ -90,7 +58,7 @@ export const createDynamicComponent = (basePath: string) => { * @param path 相对路径 * @returns */ - return (path: string): Promise => + return (path: string): Promise => import( new URL( `${path}${process.env.NODE_ENV == 'production' ? '' : now()}`,