From 2f1b7f987b99222296f545dca8cd29a5eb1958d3 Mon Sep 17 00:00:00 2001 From: ningmengchongshui <916415899@qq.com> Date: Mon, 17 Jun 2024 13:19:18 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E8=B0=83=E6=95=B4=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E9=A1=BA=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- image/index.js => image.js | 2 +- image/main.ts | 130 --------------------------------- index.js | 17 ----- package.json | 10 ++- src/core/bot.ts | 30 ++++++-- src/core/icqq.ts | 5 ++ src/core/plugins.loader.ts | 2 +- src/core/plugins/index.ts | 9 ++- src/core/plugins/runtime.ts | 2 +- src/db/BaseModel.ts | 2 +- src/image/index.ts | 129 ++++++++++++++++++++++++++++++++ {image => src/image}/types.ts | 2 +- src/init.ts | 6 -- src/init/index.ts | 6 ++ src/init/redis.ts | 2 + src/main.ts | 27 +++---- src/mys/MysUser.ts | 2 +- src/mys/MysUtil.ts | 2 +- src/mys/NoteUser.ts | 2 +- src/mys/gsCfg.ts | 20 ++--- src/{ => mys}/miao.ts | 0 src/{config => }/qq.ts | 82 ++++++++++----------- src/server.ts | 2 + src/utils/puppeteer.ts | 22 +++--- src/utils/renderer/Renderer.ts | 5 ++ tsconfig.json | 7 +- 26 files changed, 271 insertions(+), 254 deletions(-) rename image/index.js => image.js (86%) delete mode 100644 image/main.ts create mode 100644 src/image/index.ts rename {image => src/image}/types.ts (63%) delete mode 100644 src/init.ts create mode 100644 src/init/index.ts rename src/{ => mys}/miao.ts (100%) rename src/{config => }/qq.ts (73%) create mode 100644 src/server.ts diff --git a/image/index.js b/image.js similarity index 86% rename from image/index.js rename to image.js index f409099..12a604a 100644 --- a/image/index.js +++ b/image.js @@ -10,7 +10,7 @@ const child1 = spawn( } ) const child2 = spawn( - 'node --no-warnings=ExperimentalWarning --loader ts-node/esm image/main.ts', + 'node --no-warnings=ExperimentalWarning --loader ts-node/esm src/server.ts', argvs.split(' '), { shell: true, diff --git a/image/main.ts b/image/main.ts deleted file mode 100644 index 3668d2b..0000000 --- a/image/main.ts +++ /dev/null @@ -1,130 +0,0 @@ -import '../src/init/require.js' -import '../src/init/config.js' -import '../src/init/logger.js' -import '../src/init/redis.js' -import Koa from 'koa' -import KoaStatic from 'koa-static' -import Router from 'koa-router' -import { Component } from 'yunzai/utils' -import { Dirent, readdirSync } from 'fs' -import { join } from 'path' -import mount from 'koa-mount' - -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'), { - withFileTypes: true -}) - .filter(flie => !flie.isFile()) - .map(flie => { - const dir = flie?.path ?? flie?.parentPath - flie.parentPath = dir - return flie - }) // 增加兼容性 - -// -const Routers = [] - -// 解析路由 -for (const flie of flies) { - const plugins = readdirSync(join(flie?.parentPath, flie.name), { - withFileTypes: true - }) - .filter( - flie => flie.isFile() && /^(routes.jsx|routes.tsx)$/.test(flie.name) - ) - .map(flie => { - const dir = flie?.path ?? flie?.parentPath - flie.parentPath = dir - return flie - }) // 增加兼容性 - - // - for (const plugin of plugins) { - const routes = await Dynamic(plugin) - // 不存在 - if (!routes) continue - // 不是数组 - if (!Array.isArray(routes)) continue - // - for (const item of routes) { - const url = `/${flie.name}${item.url}` - console.log(`http://127.0.0.1:${Port}${url}`) - Routers.push({ - parentPath: plugin.parentPath, - name: plugin.name, - uri: url, - url: item.url - }) - } - } -} - -// 辅助函数:替换路径 -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 => { - // 动态加载 - const routes = await Dynamic(Router) - // 不存在 - if (!routes) return - // 不是数组 - if (!Array.isArray(routes)) return - // 查找 - const item = routes.find(i => i.url == Router.url) - // 丢失了 - if (!item) return - /** - * 渲染html - */ - const options = item?.options ?? {} - const HTML = Com.create(item.element, { - ...options, - file_create: false - }) - // 置换为file请求 - ctx.body = replacePaths(HTML) - }) -} - -// static -app.use(mount('/file', KoaStatic(PATH))) - -// routes -app.use(router.routes()) - -// listen 8000 -app.listen(Port, () => { - console.log('______________') - console.log('Server is running on port ' + Port) - console.log('______________') - console.log('自行调整默认浏览器尺寸 800 X 1280 100%') - console.log('如果需要运行时重新计算className') - console.log('请确保一直打开此程序') - console.log('______________') -}) diff --git a/index.js b/index.js index 009706e..31532aa 100644 --- a/index.js +++ b/index.js @@ -1,19 +1,7 @@ import { execSync, spawn } from 'child_process' const argv = [...process.argv].splice(2) const argvs = argv.join(' ').replace(/(\S+\.js|\S+\.ts)/g, '') - -/** - * ********** - * 生成css文件 - * ********** - */ execSync('tailwindcss -i ./src/input.css -o ./public/output.css') - -/** - * *************** - * 启动内部运行脚本 - * *************** - */ const child = spawn( 'node --no-warnings=ExperimentalWarning --loader ts-node/esm src/main.ts', argvs.split(' '), @@ -22,11 +10,6 @@ const child = spawn( stdio: 'inherit' } ) -/** - * ************* - * exit - * ************* - */ process.on('SIGINT', () => { if (child.pid) process.kill(child.pid) if (process.pid) process.exit() diff --git a/package.json b/package.json index 2dc426e..b3760b9 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "logs": "pm2 logs", "monit": "pm2 monit", "pm2": "pm2", - "image": "node image/index.js", + "image": "node image.js", "css": "tailwindcss -i ./src/input.css -o ./public/output.css", "format": "prettier --write .", "prepare": "husky" @@ -98,8 +98,14 @@ "./core": { "import": "./src/core/index.js" }, + "./init": { + "import": "./src/init/index.js" + }, + "./image": { + "import": "./src/image/index.js" + }, "./image/types": { - "import": "./image/types.js" + "import": "./src/image/types.js" } }, "imports": { diff --git a/src/core/bot.ts b/src/core/bot.ts index f7f09b0..af6108b 100644 --- a/src/core/bot.ts +++ b/src/core/bot.ts @@ -48,18 +48,22 @@ export class Client extends IcqqClient { /** * 跳过登录 */ - if (cfg.bot.skip_login) return await this.skip_login(bot) + if (cfg.bot.skip_login) { + return await this.skip_login(bot) + } /** * 正常的登录 */ await bot.login(cfg.qq, cfg.pwd) + bot[bot.uin] = bot /** * 全局变量 bot */ global.Bot = bot + return bot } @@ -69,29 +73,39 @@ export class Client extends IcqqClient { * @returns */ static async skip_login(bot) { + // bot.uin = 88888 + // bot[bot.uin] = bot /** * 全局变量 bot */ global.Bot = bot - /** - * 加载插件 - */ - await PluginsLoader.load() /** * */ - return + return bot } + + /** + * 加载插件 + * @param bot + * @returns + */ + static async PluginsLoader() { + await PluginsLoader.load() + } + + // } /** - * 内置Redis + * 不推荐使用,可能放弃 + * @deprecated */ export const Redis = global.redis /** * 机器人客户端 */ -export const Bot = global.Bot as typeof Client.prototype +export const Bot = global.Bot diff --git a/src/core/icqq.ts b/src/core/icqq.ts index 78e8edf..b14bb1c 100644 --- a/src/core/icqq.ts +++ b/src/core/icqq.ts @@ -1,2 +1,7 @@ +import { segment } from 'icqq' +/** + * global.segment + */ +global.segment = segment export { segment } from 'icqq' export { segment as Segment } from 'icqq' diff --git a/src/core/plugins.loader.ts b/src/core/plugins.loader.ts index 8de0be5..35c820c 100644 --- a/src/core/plugins.loader.ts +++ b/src/core/plugins.loader.ts @@ -163,7 +163,7 @@ class PluginsLoader { logger.error(decodeURI(i.reason)) } } catch (error) { - console.error(error) + logger.error(error) if (packageErr && error.stack.includes('Cannot find package')) { packageErr.push({ error, file }) } else { diff --git a/src/core/plugins/index.ts b/src/core/plugins/index.ts index c8fbc88..cc01b75 100644 --- a/src/core/plugins/index.ts +++ b/src/core/plugins/index.ts @@ -1,5 +1,5 @@ -import { Common } from '../../miao.js' +import { Common } from '../../mys/miao.js' import { EventType } from './types.js' import { type EventMap } from 'icqq' @@ -265,4 +265,9 @@ export class Plugin { /** * @deprecated 已废弃 */ -export const plugin = Plugin \ No newline at end of file +export const plugin = Plugin + +/** + * global.plugin + */ +global.plugin = plugin \ No newline at end of file diff --git a/src/core/plugins/runtime.ts b/src/core/plugins/runtime.ts index 7c8aa7a..5ab3057 100644 --- a/src/core/plugins/runtime.ts +++ b/src/core/plugins/runtime.ts @@ -11,7 +11,7 @@ import puppeteer from '../../utils/puppeteer/puppeteer.js' import * as common from '../../utils/common.js' import cfg from '../../config/config.js' import Handler from './handler.js' -import { Version } from '../../miao.js' +import { Version } from '../../mys/miao.js' /** * @deprecated 已废弃 diff --git a/src/db/BaseModel.ts b/src/db/BaseModel.ts index dcda308..bfabfb2 100644 --- a/src/db/BaseModel.ts +++ b/src/db/BaseModel.ts @@ -1,5 +1,5 @@ import { Sequelize, DataTypes, Model } from 'sequelize' -import { Data } from '../miao.js' +import { Data } from '../mys/miao.js' Data.createDir('/data/db', 'root') diff --git a/src/image/index.ts b/src/image/index.ts new file mode 100644 index 0000000..5753aba --- /dev/null +++ b/src/image/index.ts @@ -0,0 +1,129 @@ +import '../init/index' +import Koa from 'koa' +import KoaStatic from 'koa-static' +import Router from 'koa-router' +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 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'), { + withFileTypes: true + }) + .filter(flie => !flie.isFile()) + .map(flie => { + const dir = flie?.path ?? flie?.parentPath + flie.parentPath = dir + return flie + }) // 增加兼容性 + + // + const Routers = [] + + // 解析路由 + for (const flie of flies) { + const plugins = readdirSync(join(flie?.parentPath, flie.name), { + withFileTypes: true + }) + .filter( + flie => flie.isFile() && /^(routes.jsx|routes.tsx)$/.test(flie.name) + ) + .map(flie => { + const dir = flie?.path ?? flie?.parentPath + flie.parentPath = dir + return flie + }) // 增加兼容性 + + // + for (const plugin of plugins) { + const routes = await Dynamic(plugin) + // 不存在 + if (!routes) continue + // 不是数组 + if (!Array.isArray(routes)) continue + // + for (const item of routes) { + const url = `/${flie.name}${item.url}` + console.log(`http://127.0.0.1:${Port}${url}`) + Routers.push({ + parentPath: plugin.parentPath, + name: plugin.name, + uri: url, + url: item.url + }) + } + } + } + + // 辅助函数:替换路径 + 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 => { + // 动态加载 + const routes = await Dynamic(Router) + // 不存在 + if (!routes) return + // 不是数组 + if (!Array.isArray(routes)) return + // 查找 + const item = routes.find(i => i.url == Router.url) + // 丢失了 + if (!item) return + /** + * 渲染html + */ + const options = item?.options ?? {} + const HTML = Com.create(item.element, { + ...options, + file_create: false + }) + // 置换为file请求 + ctx.body = replacePaths(HTML) + }) + } + + // static + app.use(mount('/file', KoaStatic(PATH))) + + // routes + app.use(router.routes()) + + // listen 8000 + app.listen(Port, () => { + console.log('______________') + console.log('Server is running on port ' + Port) + console.log('______________') + console.log('自行调整默认浏览器尺寸 800 X 1280 100%') + console.log('如果需要运行时重新计算className') + console.log('请确保一直打开此程序') + console.log('______________') + }) +} diff --git a/image/types.ts b/src/image/types.ts similarity index 63% rename from image/types.ts rename to src/image/types.ts index 431c99f..a93ff6f 100644 --- a/image/types.ts +++ b/src/image/types.ts @@ -1,4 +1,4 @@ -import { type ComponentCreateOpsionType } from 'yunzai/utils' +import { ComponentCreateOpsionType } from '../utils/component' export type RouterType = { url: string element: React.ReactNode diff --git a/src/init.ts b/src/init.ts deleted file mode 100644 index 5fb6a1a..0000000 --- a/src/init.ts +++ /dev/null @@ -1,6 +0,0 @@ -import './init/require.js' -import './init/config.js' -import './init/logger.js' -import './init/redis.js' -import './init/process.js' -import './init/run.js' diff --git a/src/init/index.ts b/src/init/index.ts new file mode 100644 index 0000000..a6d9fc2 --- /dev/null +++ b/src/init/index.ts @@ -0,0 +1,6 @@ +import './require.js' +import './config.js' +import './logger.js' +import './redis.js' +import './process.js' +import './run.js' diff --git a/src/init/redis.ts b/src/init/redis.ts index f0902ec..ef68f3e 100644 --- a/src/init/redis.ts +++ b/src/init/redis.ts @@ -44,6 +44,8 @@ async function redisInit() { }) /** 全局变量 redis */ global.redis = client as any + + // logger.info('Redis 连接成功') return client } diff --git a/src/main.ts b/src/main.ts index 537f2d9..9ff035b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,19 +1,6 @@ -import './init.js' -import { plugin, segment, Client } from './core/index.js' -import { Renderer } from './utils/index.js' -import { createQQ } from './config/qq.js' -/** - * global.plugin - */ -global.plugin = plugin -/** - * global.segment - */ -global.segment = segment -/** - * global.Renderer - */ -global.Renderer = Renderer +import 'yunzai/init' +import { Client, Bot as bot } from 'yunzai/core' +import { createQQ } from './qq.js' /** * 确保所有微任务做好准备后 * 再进行宏任务 @@ -24,4 +11,12 @@ setTimeout(async () => { * run */ await Client.run() + + console.log('bot', bot) + console.log('Bot', Bot) + + /** + * Loader + */ + await Client.PluginsLoader() }, 0) diff --git a/src/mys/MysUser.ts b/src/mys/MysUser.ts index 23c8dbc..c2fde82 100644 --- a/src/mys/MysUser.ts +++ b/src/mys/MysUser.ts @@ -13,7 +13,7 @@ import NoteUser from './NoteUser.js' import MysApi from './mysApi.js' import MysUtil from './MysUtil.js' import { MysUserDB } from '../db/index.js' -import { Data } from '../miao.js' +import { Data } from './miao.js' const tables = { // ltuid-uid 查询表 diff --git a/src/mys/MysUtil.ts b/src/mys/MysUtil.ts index ce5eb8d..5615b41 100644 --- a/src/mys/MysUtil.ts +++ b/src/mys/MysUtil.ts @@ -1,4 +1,4 @@ -import { Data } from '../miao.js' +import { Data } from './miao.js' /** * diff --git a/src/mys/NoteUser.ts b/src/mys/NoteUser.ts index 28ccf6f..76f4696 100644 --- a/src/mys/NoteUser.ts +++ b/src/mys/NoteUser.ts @@ -3,7 +3,7 @@ import lodash from 'lodash' import MysUser from './MysUser.js' import MysUtil from './MysUtil.js' import { UserDB } from '../db/index.js' -import { Data } from '../miao.js' +import { Data } from './miao.js' /** * ******************* * Bot实际User用户类 diff --git a/src/mys/gsCfg.ts b/src/mys/gsCfg.ts index 1e5ab8a..24865c6 100644 --- a/src/mys/gsCfg.ts +++ b/src/mys/gsCfg.ts @@ -4,7 +4,7 @@ import fs from 'node:fs' import lodash from 'lodash' import MysInfo from './mysInfo.js' import NoteUser from './NoteUser.js' -import { Character, Weapon } from '../miao.js' +import { Character, Weapon } from './miao.js' /** * *********** @@ -350,7 +350,7 @@ class GsCfg { * @returns */ getWeaponDataByWeaponHash(_) { - console.log('gsCfg.getWeaponDataByWeaponHash() 已废弃') + logger.info('gsCfg.getWeaponDataByWeaponHash() 已废弃') return {} } @@ -360,7 +360,7 @@ class GsCfg { * @returns */ getAllAbbr() { - console.log('gsCfg.getAllAbbr() 已废弃') + logger.info('gsCfg.getAllAbbr() 已废弃') return {} } @@ -371,7 +371,7 @@ class GsCfg { * @returns */ getBingCkSingle(_) { - console.log('gsCfg.getBingCkSingle() 已废弃') + logger.info('gsCfg.getBingCkSingle() 已废弃') return {} } @@ -382,7 +382,7 @@ class GsCfg { * @param data */ saveBingCk(_, __) { - console.log('gsCfg.saveBingCk() 已废弃') + logger.info('gsCfg.saveBingCk() 已废弃') } /** @@ -392,7 +392,7 @@ class GsCfg { * @returns */ getElementByRoleName(_) { - console.log('gsCfg.getElementByRoleName() 已废弃') + logger.info('gsCfg.getElementByRoleName() 已废弃') return '' } @@ -404,7 +404,7 @@ class GsCfg { * @returns */ getSkillDataByskillId(_, __) { - console.log('gsCfg.getSkillDataByskillId() 已废弃') + logger.info('gsCfg.getSkillDataByskillId() 已废弃') return {} } @@ -415,7 +415,7 @@ class GsCfg { * @returns */ fightPropIdToName(_) { - console.log('gsCfg.fightPropIdToName() 已废弃') + logger.info('gsCfg.fightPropIdToName() 已废弃') return '' } @@ -426,7 +426,7 @@ class GsCfg { * @returns */ getRoleTalentByTalentId(_) { - console.log('gsCfg.getRoleTalentByTalentId 已废弃') + logger.info('gsCfg.getRoleTalentByTalentId 已废弃') return {} } @@ -435,7 +435,7 @@ class GsCfg { * @deprecated 已废弃 */ getAbbr() { - console.log('gsCfg.getAbbr() 已经废弃') + logger.info('gsCfg.getAbbr() 已经废弃') } } diff --git a/src/miao.ts b/src/mys/miao.ts similarity index 100% rename from src/miao.ts rename to src/mys/miao.ts diff --git a/src/config/qq.ts b/src/qq.ts similarity index 73% rename from src/config/qq.ts rename to src/qq.ts index b186ce1..6a1af88 100644 --- a/src/config/qq.ts +++ b/src/qq.ts @@ -1,34 +1,36 @@ import fs from 'fs' import inquirer from 'inquirer' import chalk from 'chalk' -import { BOT_NAME, CONFIG_DEFAULT_PATH, CONFIG_INIT_PATH } from './system.js' -import cfg from './config.js' -import { sleep } from "./utils.js" +import { + BOT_NAME, + CONFIG_DEFAULT_PATH, + CONFIG_INIT_PATH, + ConfigController as cfg +} from 'yunzai/config' +import { sleep } from 'yunzai/utils' /** * 创建qq配置文件 `config/bot/qq.yaml` * Git Bash 运行npm命令会无法选择列表 - * @returns + * @returns */ export async function createQQ() { /** 跳过登录ICQQ */ if (cfg.bot.skip_login) return - /** - * + * */ if (cfg.qq && !process.argv.includes('login')) { return } - /** - * + * */ - console.log(`欢迎使用${chalk.green(`${BOT_NAME} v` + cfg.package.version)}\n请按提示输入完成QQ配置`) - - + logger.info( + `欢迎使用${chalk.green(`${BOT_NAME} v` + cfg.package.version)}\n请按提示输入完成QQ配置` + ) /** - * + * */ const propmtList = [ { @@ -51,22 +53,28 @@ export async function createQQ() { name: 'platform', default: '6', choices: ['Tim', 'iPad', '安卓手机', '安卓手表', 'MacOS', 'aPad'], - filter: (val) => { + filter: val => { switch (val) { - case 'Tim': return 6 - case 'iPad': return 5 - case 'MacOS': return 4 - case '安卓手机': return 1 - case '安卓手表': return 3 - case 'aPad': return 2 - default: return 6 + case 'Tim': + return 6 + case 'iPad': + return 5 + case 'MacOS': + return 4 + case '安卓手机': + return 1 + case '安卓手表': + return 3 + case 'aPad': + return 2 + default: + return 6 } } } ] - /** - * + * */ if (!process.argv.includes('login')) { propmtList.push({ @@ -75,59 +83,49 @@ export async function createQQ() { name: 'masterQQ' }) } - /** - * + * */ propmtList.push({ type: 'input', message: '请输入签名API地址(可留空):', name: 'signAPI' }) - /** - * + * */ const ret = await inquirer.prompt(propmtList) - /** - * + * */ const file = `./${CONFIG_INIT_PATH}` - const fileDef = `./${CONFIG_DEFAULT_PATH}` - let qq = fs.readFileSync(`${fileDef}qq.yaml`, 'utf8') - qq = qq.replace(/qq:/g, 'qq: ' + ret.QQ) qq = qq.replace(/pwd:/g, `pwd: '${ret.pwd}'`) qq = qq.replace(/platform: [1-6]/g, 'platform: ' + Number(ret.platform)) fs.writeFileSync(`${file}qq.yaml`, qq, 'utf8') - let bot = fs.readFileSync(`${fileDef}bot.yaml`, 'utf8') - /** - * + * */ if (ret.masterQQ) { let other = fs.readFileSync(`${fileDef}other.yaml`, 'utf8') other = other.replace(/masterQQ:/g, `masterQQ:\n - ${ret.masterQQ}`) fs.writeFileSync(`${file}other.yaml`, other, 'utf8') } - /** - * + * */ if (ret.signAPI) { bot = bot.replace(/sign_api_addr:/g, `sign_api_addr: ${ret.signAPI}`) } - fs.writeFileSync(`${file}bot.yaml`, bot, 'utf8') - - console.log(`\nQQ配置完成,正在登录\n后续修改账号可以运行命令: ${chalk.green('npm run login')}\n`) - + logger.info( + `\nQQ配置完成,正在登录\n后续修改账号可以运行命令: ${chalk.green('npm run login')}\n` + ) /** - * + * */ await sleep(2000) -} \ No newline at end of file +} diff --git a/src/server.ts b/src/server.ts new file mode 100644 index 0000000..d0b36be --- /dev/null +++ b/src/server.ts @@ -0,0 +1,2 @@ +import { createServer } from 'yunzai/image' +await createServer() diff --git a/src/utils/puppeteer.ts b/src/utils/puppeteer.ts index aeb5ee5..aaa0776 100644 --- a/src/utils/puppeteer.ts +++ b/src/utils/puppeteer.ts @@ -65,11 +65,11 @@ export class Puppeteer { try { this.browser = await puppeteer.launch(this.#launch) this.#isBrowser = true - console.info('[puppeteer] open success') + logger.info('[puppeteer] open success') return true } catch (err) { this.#isBrowser = false - console.error('[puppeteer] err', err) + logger.error('[puppeteer] err', err) return false } } @@ -96,12 +96,12 @@ export class Puppeteer { * 重置次数 */ this.#pic = 0 - console.info('[puppeteer] close') + logger.info('[puppeteer] close') this.#isBrowser = false this.browser?.close().catch(err => { - console.error('[puppeteer] close', err) + logger.error('[puppeteer] close', err) }) - console.info('[puppeteer] reopen') + logger.info('[puppeteer] reopen') if (!(await this.start())) return false this.#pic++ } @@ -121,7 +121,7 @@ export class Puppeteer { if (!(await this.isStart())) return false try { const page = await this.browser?.newPage().catch(err => { - console.error(err) + logger.error(err) }) if (!page) return false await page.goto(`file://${htmlPath}`, { @@ -129,7 +129,7 @@ export class Puppeteer { }) const body = await page.$(Options?.tab ?? 'body') if (!body) return false - console.info('[puppeteer] success') + logger.info('[puppeteer] success') const buff: string | false | Buffer = await body .screenshot( Options?.SOptions ?? { @@ -137,19 +137,19 @@ export class Puppeteer { } ) .catch(err => { - console.error('[puppeteer]', 'screenshot', err) + logger.error('[puppeteer]', 'screenshot', err) return false }) await page.close().catch(err => { - console.error('[puppeteer]', 'page close', err) + logger.error('[puppeteer]', 'page close', err) }) if (!buff) { - console.error('[puppeteer]', htmlPath) + logger.error('[puppeteer]', htmlPath) return false } return buff } catch (err) { - console.error('[puppeteer] newPage', err) + logger.error('[puppeteer] newPage', err) return false } } diff --git a/src/utils/renderer/Renderer.ts b/src/utils/renderer/Renderer.ts index 5f68d77..1d3e3b3 100644 --- a/src/utils/renderer/Renderer.ts +++ b/src/utils/renderer/Renderer.ts @@ -87,3 +87,8 @@ export default class Renderer { this.watcher[tplFile] = watcher } } + +/** + * global.Renderer + */ +global.Renderer = Renderer diff --git a/tsconfig.json b/tsconfig.json index d1c7c04..9e26399 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -34,12 +34,15 @@ // "paths": { // 内部核心模块导出 - "yunzai/image/types": ["./image/types.js"], "yunzai/config": ["./src/config/index.js"], "yunzai/db": ["./src/db/index.js"], "yunzai/mys": ["./src/mys/index.js"], "yunzai/utils": ["./src/utils/index.js"], + "yunzai/init": ["./src/init/index.js"], "yunzai/core": ["./src/core/index.js"], + // 图片开发 + "yunzai/image/types": ["./src/image/types.js"], + "yunzai/image": ["./src/image/index.js"], // 外部模块 待移除 "#miao": ["./plugins/miao-plugin/components/index.js"], "#miao.models": ["./plugins/miao-plugin/models/index.js"] @@ -50,5 +53,5 @@ "transpileOnly": true, "experimentalSpecifierResolution": "node" }, - "include": ["src", "image", "lib", "renderers", "plugins"] + "include": ["src", "plugins", "lib", "renderers"] }