feat: 补充类型声明

This commit is contained in:
ningmengchongshui 2024-06-18 14:32:24 +08:00
parent b96ba6aac5
commit 773f85a168
3 changed files with 75 additions and 64 deletions

42
src/global.d.ts vendored
View File

@ -13,12 +13,40 @@ type LogType = string | Error | unknown
* *
*/ */
type LoggerType = { type LoggerType = {
/**
*
* @param arg
*/
trace(...arg: LogType[]): any trace(...arg: LogType[]): any
/**
*
* @param arg
*/
debug(...arg: LogType[]): any debug(...arg: LogType[]): any
/**
*
* @param arg
*/
info(...arg: LogType[]): any info(...arg: LogType[]): any
/**
*
* @param arg
*/
warn(...arg: LogType[]): any warn(...arg: LogType[]): any
/**
*
* @param arg
*/
error(...arg: LogType[]): any error(...arg: LogType[]): any
/**
*
* @param arg
*/
fatal(...arg: LogType[]): any fatal(...arg: LogType[]): any
/**
*
* @param arg
*/
mark(...arg: LogType[]): any mark(...arg: LogType[]): any
} }
@ -27,26 +55,32 @@ type LoggerType = {
*/ */
type ChalkInstanceType = { type ChalkInstanceType = {
/** /**
*
* @deprecated 使 * @deprecated 使
*/ */
red: ChalkInstance.red red: ChalkInstance.red
/** /**
* 绿
* @deprecated 使 * @deprecated 使
*/ */
green: ChalkInstance.green green: ChalkInstance.green
/** /**
*
* @deprecated 使 * @deprecated 使
*/ */
blue: ChalkInstance.blue blue: ChalkInstance.blue
/** /**
*
* @deprecated 使 * @deprecated 使
*/ */
yellow: ChalkInstance.yellow yellow: ChalkInstance.yellow
/** /**
*
* @deprecated 使 * @deprecated 使
*/ */
magenta: ChalkInstance.magenta magenta: ChalkInstance.magenta
/** /**
*
* @deprecated 使 * @deprecated 使
*/ */
cyan: ChalkInstance.cyan cyan: ChalkInstance.cyan
@ -54,13 +88,15 @@ type ChalkInstanceType = {
declare global { declare global {
/** /**
* import { Redis } from 'yunzai/core' *
* @deprecated 使 * @deprecated 使
* import { Redis } from 'yunzai/core'
*/ */
var redis: RedisClientType var redis: RedisClientType
/** /**
* import { Bot } from 'yunzai/core' *
* @deprecated 使 * @deprecated 使
* import { Bot } from 'yunzai/core'
*/ */
var Bot: typeof Client.prototype var Bot: typeof Client.prototype
/** /**
@ -69,8 +105,8 @@ declare global {
*/ */
var segment: typeof se var segment: typeof se
/** /**
* import { Plugin } from 'yunzai/core'
* @deprecated 使 * @deprecated 使
* import { Plugin } from 'yunzai/core'
*/ */
var plugin: typeof p var plugin: typeof p
/** /**

View File

@ -6,18 +6,42 @@ import { Dirent, readdirSync } from 'fs'
import { join } from 'path' import { join } from 'path'
import mount from 'koa-mount' import mount from 'koa-mount'
import { Component } from '../utils/index.js' import { Component } from '../utils/index.js'
export * from './types.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 Com = new Component()
const app = new Koa() const app = new Koa()
const router = new Router() 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目录 // 得到plugins目录
const flies = readdirSync(join(process.cwd(), '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) { for (const Router of Routers) {
router.get(Router.uri, async ctx => { router.get(Router.uri, async ctx => {
// 动态加载 // 动态加载

View File

@ -24,17 +24,11 @@ export function require(path: string) {
const now = () => `?t=${Date.now()}` const now = () => `?t=${Date.now()}`
/** /**
* @param basePath import.meta.url
* @returns
* *********** * ***********
* *
* *********** * @param basePath import.meta.url
* 访, * @returns
* ,
* ,
* ***********
* env.NODE_ENV=='production' * env.NODE_ENV=='production'
* @deprecated
*/ */
export const createDynamic = (basePath: string) => { 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<PropsType> }>
// export const createDynamicComponent = (basePath: string) => {
// /**
// * 与import作用相同
// * @param path 相对路径
// * @returns
// */
// return <T extends Record<string, any> = { default: any }>(
// path: string
// ): Promise<{ [K in keyof T]: React.ComponentType<T[K]> }> =>
// 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' * env.NODE_ENV=='production'
*/ */
export const createDynamicComponent = (basePath: string) => { export const createDynamicComponent = (basePath: string) => {
@ -90,7 +58,7 @@ export const createDynamicComponent = (basePath: string) => {
* @param path * @param path
* @returns * @returns
*/ */
return <T>(path: string): Promise<T> => return <T = any>(path: string): Promise<T> =>
import( import(
new URL( new URL(
`${path}${process.env.NODE_ENV == 'production' ? '' : now()}`, `${path}${process.env.NODE_ENV == 'production' ? '' : now()}`,