From 2962a58d34326edd65506cd0a564caa4f49944bf Mon Sep 17 00:00:00 2001 From: ningmengchongshui <916415899@qq.com> Date: Sun, 9 Jun 2024 01:31:31 +0800 Subject: [PATCH] refactor: lib&core&mys --- .gitignore | 3 + .puppeteerrc.cjs | 4 +- README.md | 2 +- package.json | 1 + rollup.config.js | 20 ++++++ src/core/runtime.ts | 150 ++++++++------------------------------------ src/db/index.ts | 1 - src/mys/dev.md | 6 ++ src/mys/index.ts | 21 ++++++- src/mys/mysInfo.ts | 2 +- 10 files changed, 81 insertions(+), 129 deletions(-) create mode 100644 rollup.config.js create mode 100644 src/mys/dev.md diff --git a/.gitignore b/.gitignore index 40fcb1a..c19fcad 100644 --- a/.gitignore +++ b/.gitignore @@ -145,3 +145,6 @@ yunzai /temp/ /pnpm-lock.yaml /entrypoint.sh + + +dist \ No newline at end of file diff --git a/.puppeteerrc.cjs b/.puppeteerrc.cjs index 7bd7c8d..90dc4e1 100644 --- a/.puppeteerrc.cjs +++ b/.puppeteerrc.cjs @@ -46,5 +46,7 @@ if (executablePath || arch === 'arm64' || arch === 'aarch64') { ) skipDownload = true } - +/** + * @type {import("puppeteer").Configuration} + */ module.exports = { skipDownload, executablePath } diff --git a/README.md b/README.md index fffbf94..53a15be 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Miao-Yunzai v3 +# Miao-Yunzai 这里是Miao-Yunzai V4 测试仓库, diff --git a/package.json b/package.json index a8973f8..da11683 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "restart": "pm2 restart ./config/pm2/pm2.json", "ts:app": "ts-node src/main.ts", "ts:login": "ts-node src/main.ts login", + "ts:build": "rollup --config rollup.config.js", "format": "prettier --write .", "prepare": "husky" }, diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 0000000..509483c --- /dev/null +++ b/rollup.config.js @@ -0,0 +1,20 @@ +import typescript from '@rollup/plugin-typescript' +/** + * @type {import("rollup").RollupOptions} + */ +export default [ + { + input: 'src/main.ts', + output: { + // file: 'index.js', + dir: 'dist', + format: 'module', + sourcemap: false + }, + plugins: [typescript()], + onwarn: (warning, warn) => { + if (warning.code === 'UNRESOLVED_IMPORT') return + warn(warning) + } + } +] diff --git a/src/core/runtime.ts b/src/core/runtime.ts index 3e8a5d9..8fa1658 100644 --- a/src/core/runtime.ts +++ b/src/core/runtime.ts @@ -1,37 +1,22 @@ -import lodash from 'lodash' -import fs from 'node:fs' +import common from '../lib/common/common.js' +import cfg from '../lib/config/config.js' +import Handler from '../lib/plugins/handler.js' + +import { + gsCfg, + mysApi as MysApi, + mysInfo as MysInfo, + NoteUser, + MysUser +} from '../mys/index.js' /** - * tudo - */ -import common from '../../lib/common/common.js' -import cfg from '../../lib/config/config.js' -import Handler from '../../lib/plugins/handler.js' -import puppeteer from '../../lib/puppeteer/puppeteer.js' - -/** - * tudo - * 不合理的调用逻辑 - */ -import { Version } from './local.js' - -/** - * tudo - * 不合理的调用逻辑 - */ -import gsCfg from '../../plugins/genshin/model/gsCfg.js' - -// -import MysApi from '../../plugins/genshin/model/mys/mysApi.js' -import MysInfo from '../../plugins/genshin/model/mys/mysInfo.js' -import NoteUser from '../../plugins/genshin/model/mys/NoteUser.js' -import MysUser from '../../plugins/genshin/model/mys/MysUser.js' - -/** - * ********************* + * ******************** * 对e进行重构的危险代码 + * ******************** + * tudo + * 写法混乱,需要重构 */ - export default class Runtime { e = null _mysInfo = null @@ -72,8 +57,11 @@ export default class Runtime { return common } + /** + * @deprecated 不符合架构设计,已废弃 + */ get puppeteer() { - return puppeteer + return null } get MysInfo() { @@ -102,12 +90,13 @@ export default class Runtime { } /** - * + * 初始化 */ async initUser() { let e = this.e let user = await NoteUser.create(e) if (user) { + // 对象代理 e.user = new Proxy(user, { get(self, key, receiver) { let game = e.game @@ -123,6 +112,7 @@ export default class Runtime { if (key === 'uidData') { return self.getUidData('', game) } + // 不能将类型“symbol”分配给类型“string”。 if ( [ 'getUid', @@ -131,7 +121,7 @@ export default class Runtime { 'getCkUidList', 'getUidMapList', 'getGameDs' - ].includes(key) + ].includes(key as string) ) { return (_game, arg2) => { return self[key](_game || game, arg2) @@ -145,7 +135,7 @@ export default class Runtime { 'addRegUid', 'delRegUid', 'setMainUid' - ].includes(key) + ].includes(key as string) ) { return (uid, _game = '') => { return self[key](uid, _game || game) @@ -210,97 +200,9 @@ export default class Runtime { } /** - * - * @param plugin plugin key - * @param path html文件路径,相对于plugin resources目录 - * @param data 渲染数据 - * @param cfg 渲染配置 - * @param cfg.retType 返回值类型 - * * default/空:自动发送图片,返回true - * * msgId:自动发送图片,返回msg id - * * base64: 不自动发送图像,返回图像base64数据 - * @param cfg.beforeRender({data}) 可改写渲染的data数据 - * @returns {Promise} + * @deprecated 不符合架构设计,已废弃 */ async render(plugin, path, data = {}, cfg = {}) { - // 处理传入的path - path = path.replace(/.html$/, '') - let paths = lodash.filter(path.split('/'), p => !!p) - path = paths.join('/') - // 创建目录 - const mkdir = check => { - let currDir = `${process.cwd()}/temp` - for (let p of check.split('/')) { - currDir = `${currDir}/${p}` - if (!fs.existsSync(currDir)) { - fs.mkdirSync(currDir) - } - } - return currDir - } - mkdir(`html/${plugin}/${path}`) - // 自动计算pluResPath - let pluResPath = `../../../${lodash.repeat('../', paths.length)}plugins/${plugin}/resources/` - let miaoResPath = `../../../${lodash.repeat('../', paths.length)}plugins/miao-plugin/resources/` - const layoutPath = - process.cwd() + '/plugins/miao-plugin/resources/common/layout/' - - /** - * tudo - * 不符合阅读习惯的,data重写data - */ - - // 渲染data - data = { - sys: { - scale: 1 - }, - /** miao 相关参数 **/ - copyright: `Created By Miao-Yunzai${Version.yunzai} `, - _res_path: pluResPath, - _miao_path: miaoResPath, - _tpl_path: process.cwd() + '/plugins/miao-plugin/resources/common/tpl/', - defaultLayout: layoutPath + 'default.html', - elemLayout: layoutPath + 'elem.html', - - ...data, - - /** 默认参数 **/ - _plugin: plugin, - _htmlPath: path, - pluResPath, - tplFile: `./plugins/${plugin}/resources/${path}.html`, - saveId: data.saveId || data.save_id || paths[paths.length - 1], - pageGotoParams: { - waitUntil: 'networkidle2' - } - } - - // 处理beforeRender - if (cfg.beforeRender) { - data = cfg.beforeRender({ data }) || data - } - // 保存模板数据 - if (process.argv.includes('dev')) { - // debug下保存当前页面的渲染数据,方便模板编写与调试 - // 由于只用于调试,开发者只关注自己当时开发的文件即可,暂不考虑app及plugin的命名冲突 - let saveDir = mkdir(`ViewData/${plugin}`) - let file = `${saveDir}/${data._htmlPath.split('/').join('_')}.json` - fs.writeFileSync(file, JSON.stringify(data)) - } - // 截图 - let base64 = await puppeteer.screenshot(`${plugin}/${path}`, data) - if (cfg.retType === 'base64') { - return base64 - } - let ret = true - if (base64) { - if (cfg.recallMsg) { - ret = await this.e.reply(base64, false, {}) - } else { - ret = await this.e.reply(base64) - } - } - return cfg.retType === 'msgId' ? ret : true + return false } } diff --git a/src/db/index.ts b/src/db/index.ts index 5a30eea..d41f98b 100644 --- a/src/db/index.ts +++ b/src/db/index.ts @@ -2,5 +2,4 @@ import UserDB from './UserDB.js' import MysUserDB from './MysUserDB.js' import UserGameDB from './UserGameDB.js' import { sequelize } from './BaseModel.js' - export { UserDB, MysUserDB, UserGameDB, sequelize } diff --git a/src/mys/dev.md b/src/mys/dev.md new file mode 100644 index 0000000..7ea2051 --- /dev/null +++ b/src/mys/dev.md @@ -0,0 +1,6 @@ +mys 存在循环引用 + +(!) Circular dependencies +src/mys/apiTool.ts -> src/mys/mysApi.ts -> src/mys/apiTool.ts +src/mys/gsCfg.ts -> src/mys/mysInfo.ts -> src/mys/gsCfg.ts +src/mys/NoteUser.ts -> src/mys/MysUser.ts -> src/mys/NoteUser.ts diff --git a/src/mys/index.ts b/src/mys/index.ts index 336ce12..2797df7 100644 --- a/src/mys/index.ts +++ b/src/mys/index.ts @@ -1 +1,20 @@ -export {} +import apiTool from './apiTool.js' +import BaseModel from './BaseModel.js' +import DailyCache from './DailyCache.js' +import gsCfg from './gsCfg.js' +import mysApi from './mysApi.js' +import mysInfo from './mysInfo.js' +import MysUser from './MysUser.js' +import MysUtil from './MysUtil.js' +import NoteUser from './NoteUser.js' +export { + apiTool, + BaseModel, + DailyCache, + gsCfg, + mysApi, + mysInfo, + MysUser, + MysUtil, + NoteUser +} diff --git a/src/mys/mysInfo.ts b/src/mys/mysInfo.ts index 9efd0dd..1ca79a8 100644 --- a/src/mys/mysInfo.ts +++ b/src/mys/mysInfo.ts @@ -1,7 +1,7 @@ import MysApi from './mysApi.js' // tudo -import GsCfg from '../gsCfg.js' +import GsCfg from './gsCfg.js' import lodash from 'lodash' import NoteUser from './NoteUser.js'