feat: 优化顺序
This commit is contained in:
		
							parent
							
								
									668b12e23f
								
							
						
					
					
						commit
						d4bf0cb718
					
				|  | @ -1,4 +1,4 @@ | ||||||
| import { execAsync, readJSON } from '../utils/index.js' | import { execAsync, readJSON } from "./utils.js" | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * 校验运行 |  * 校验运行 | ||||||
|  |  | ||||||
|  | @ -1,170 +0,0 @@ | ||||||
| import fs, { promises } from "node:fs" |  | ||||||
| import yaml from "yaml" |  | ||||||
| import { BOT_NAME, CONFIG_INIT_PATH } from "./system.js" |  | ||||||
| import { createQQ } from "./qq.js" |  | ||||||
| import { setLogger } from "./log.js" |  | ||||||
| import { redisInit } from "./redis.js" |  | ||||||
| import { checkRun } from "./check.js" |  | ||||||
| import { join } from "node:path" |  | ||||||
| 
 |  | ||||||
| /** |  | ||||||
|  *  |  | ||||||
|  */ |  | ||||||
| async function UpdateTitle() { |  | ||||||
|   /** |  | ||||||
|    * 添加一些多余的标题内容 |  | ||||||
|    */ |  | ||||||
|   let title = BOT_NAME |  | ||||||
| 
 |  | ||||||
|   //
 |  | ||||||
|   const qq = await promises.readFile(`./${CONFIG_INIT_PATH}qq.yaml`, 'utf-8').then(yaml.parse).catch(() => null) |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    *  |  | ||||||
|    */ |  | ||||||
|   if (qq) { |  | ||||||
|     title += `@${qq.qq || ""}` |  | ||||||
|     switch (qq.platform) { |  | ||||||
|       case 1: { |  | ||||||
|         title += " 安卓手机" |  | ||||||
|         break |  | ||||||
|       } |  | ||||||
|       case 2: { |  | ||||||
|         title += " aPad" |  | ||||||
|         break |  | ||||||
|       } |  | ||||||
|       case 3: { |  | ||||||
|         title += " 安卓手表" |  | ||||||
|         break |  | ||||||
|       } |  | ||||||
|       case 4: { |  | ||||||
|         title += " MacOS" |  | ||||||
|         break |  | ||||||
|       } |  | ||||||
|       case 5: { |  | ||||||
|         title += " iPad" |  | ||||||
|         break |  | ||||||
|       } |  | ||||||
|       case 6: { |  | ||||||
|         title += " Tim" |  | ||||||
|         break |  | ||||||
|       } |  | ||||||
|       default: { |  | ||||||
|         break |  | ||||||
|       } |  | ||||||
|     } |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * 设置标题 |  | ||||||
|    */ |  | ||||||
|   process.title = title |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| /** |  | ||||||
|  * 初始化事件 |  | ||||||
|  */ |  | ||||||
| export async function checkInit() { |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * 检查node_modules |  | ||||||
|    */ |  | ||||||
|   if (!fs.existsSync(join(process.cwd(), "./node_modules"))) { |  | ||||||
|     console.log("未安装依赖。。。。") |  | ||||||
|     console.log("请先运行命令:pnpm install -P 安装依赖") |  | ||||||
|     process.exit() |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * 检查node_modules/icqq |  | ||||||
|    */ |  | ||||||
|   if (!fs.existsSync(join(process.cwd(), "./node_modules/icqq"))) { |  | ||||||
|     console.log("未安装icqq。。。。") |  | ||||||
|     console.log("请先运行命令:pnpm install -P 安装依赖") |  | ||||||
|     process.exit() |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * 检查qq.yaml |  | ||||||
|    */ |  | ||||||
|   await createQQ() |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * 日志设置 |  | ||||||
|    */ |  | ||||||
|   setLogger() |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    *  |  | ||||||
|    */ |  | ||||||
|   logger.mark(`${BOT_NAME} 启动中...`) |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    *  初始化客户端 |  | ||||||
|    */ |  | ||||||
|   await redisInit() |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * 检查程序 |  | ||||||
|    */ |  | ||||||
|   await checkRun() |  | ||||||
| 
 |  | ||||||
|   /** |  | ||||||
|    * 更新标题 |  | ||||||
|    */ |  | ||||||
|   await UpdateTitle() |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| /** |  | ||||||
|  * 设置标题 |  | ||||||
|  */ |  | ||||||
| process.title = BOT_NAME |  | ||||||
| 
 |  | ||||||
| /** |  | ||||||
|  * 设置时区 |  | ||||||
|  */ |  | ||||||
| process.env.TZ = "Asia/Shanghai" |  | ||||||
| 
 |  | ||||||
| /** |  | ||||||
|  *  |  | ||||||
|  */ |  | ||||||
| process.on("SIGHUP", () => process.exit()) |  | ||||||
| 
 |  | ||||||
| /** |  | ||||||
|  * 捕获未处理的错误 |  | ||||||
|  */ |  | ||||||
| process.on("uncaughtException", error => { |  | ||||||
|   if (typeof logger == "undefined") console.log(error) |  | ||||||
|   else logger.error(error) |  | ||||||
| }) |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| /** |  | ||||||
|  * 捕获未处理的Promise错误 |  | ||||||
|  */ |  | ||||||
| process.on("unhandledRejection", (error) => { |  | ||||||
|   if (typeof logger == "undefined") console.log(error) |  | ||||||
|   else logger.error(error) |  | ||||||
| }) |  | ||||||
| 
 |  | ||||||
| /** |  | ||||||
|  * 退出事件 |  | ||||||
|  */ |  | ||||||
| process.on("exit", async () => { |  | ||||||
|   if (typeof redis != "undefined") { |  | ||||||
|     await redis.save() |  | ||||||
|   } |  | ||||||
|   if (typeof logger == "undefined") { |  | ||||||
|     console.log(`${BOT_NAME} 已停止运行`) |  | ||||||
|   } |  | ||||||
|   else { |  | ||||||
|     logger.mark(logger.magenta(`${BOT_NAME} 已停止运行`)) |  | ||||||
|   } |  | ||||||
| }) |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| /** |  | ||||||
|  * 初始化 |  | ||||||
|  */ |  | ||||||
| await checkInit() |  | ||||||
|  | @ -3,7 +3,7 @@ import inquirer from 'inquirer' | ||||||
| import chalk from 'chalk' | import chalk from 'chalk' | ||||||
| import { BOT_NAME, CONFIG_DEFAULT_PATH, CONFIG_INIT_PATH } from './system.js' | import { BOT_NAME, CONFIG_DEFAULT_PATH, CONFIG_INIT_PATH } from './system.js' | ||||||
| import cfg from './config.js' | import cfg from './config.js' | ||||||
| import { sleep } from '../utils/common.js' | import { sleep } from "./utils.js" | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  * 创建qq配置文件 `config/bot/qq.yaml` |  * 创建qq配置文件 `config/bot/qq.yaml` | ||||||
|  |  | ||||||
|  | @ -1,5 +1,5 @@ | ||||||
| import cfg from "./config.js" | import cfg from "./config.js" | ||||||
| import { execAsync, sleep } from "../utils/common.js" | import { execAsync, sleep } from "./utils.js" | ||||||
| import { createClient } from "redis" | import { createClient } from "redis" | ||||||
| 
 | 
 | ||||||
| /** | /** | ||||||
|  |  | ||||||
|  | @ -0,0 +1,42 @@ | ||||||
|  | import fs from 'node:fs' | ||||||
|  | import { exec } from 'child_process' | ||||||
|  | import { join } from 'path' | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * 休眠函数 | ||||||
|  |  * @param ms 毫秒 | ||||||
|  |  */ | ||||||
|  | export function sleep(ms: number) { | ||||||
|  |   return new Promise(resolve => setTimeout(resolve, ms)) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * | ||||||
|  |  * @param cmd | ||||||
|  |  * @returns | ||||||
|  |  */ | ||||||
|  | export function execAsync(cmd: string): Promise<{ | ||||||
|  |   stdout: string | ||||||
|  |   stderr: string | ||||||
|  | }> { | ||||||
|  |   return new Promise((resolve, reject) => { | ||||||
|  |     exec(cmd, (error, stdout, stderr) => { | ||||||
|  |       if (error) reject(error) | ||||||
|  |       resolve({ stdout, stderr }) | ||||||
|  |     }) | ||||||
|  |   }) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * | ||||||
|  |  * @param dir | ||||||
|  |  * @returns | ||||||
|  |  */ | ||||||
|  | export function readJSON(dir: string) { | ||||||
|  |   try { | ||||||
|  |     const cfg = fs.readFileSync(join(process.cwd(), dir), 'utf-8') | ||||||
|  |     return JSON.parse(cfg) | ||||||
|  |   } catch { | ||||||
|  |     return false | ||||||
|  |   } | ||||||
|  | } | ||||||
|  | @ -1,9 +1,3 @@ | ||||||
| /** |  | ||||||
|  * ********** |  | ||||||
|  * 配置初始化 |  | ||||||
|  * ********** |  | ||||||
|  */ |  | ||||||
| import '../config/main.js' |  | ||||||
| /** | /** | ||||||
|  * ********** |  * ********** | ||||||
|  * 配置读取工具 |  * 配置读取工具 | ||||||
|  |  | ||||||
|  | @ -0,0 +1,171 @@ | ||||||
|  | import fs, { promises } from 'node:fs' | ||||||
|  | import yaml from 'yaml' | ||||||
|  | import { join } from 'node:path' | ||||||
|  | /** | ||||||
|  |  * | ||||||
|  |  */ | ||||||
|  | import { BOT_NAME, CONFIG_INIT_PATH } from './config/system.js' | ||||||
|  | import { createQQ } from './config/qq.js' | ||||||
|  | import { setLogger } from './config/log.js' | ||||||
|  | import { redisInit } from './config/redis.js' | ||||||
|  | import { checkRun } from './config/check.js' | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * | ||||||
|  |  */ | ||||||
|  | async function UpdateTitle() { | ||||||
|  |   /** | ||||||
|  |    * 添加一些多余的标题内容 | ||||||
|  |    */ | ||||||
|  |   let title = BOT_NAME | ||||||
|  | 
 | ||||||
|  |   //
 | ||||||
|  |   const qq = await promises | ||||||
|  |     .readFile(`./${CONFIG_INIT_PATH}qq.yaml`, 'utf-8') | ||||||
|  |     .then(yaml.parse) | ||||||
|  |     .catch(() => null) | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * | ||||||
|  |    */ | ||||||
|  |   if (qq) { | ||||||
|  |     title += `@${qq.qq || ''}` | ||||||
|  |     switch (qq.platform) { | ||||||
|  |       case 1: { | ||||||
|  |         title += ' 安卓手机' | ||||||
|  |         break | ||||||
|  |       } | ||||||
|  |       case 2: { | ||||||
|  |         title += ' aPad' | ||||||
|  |         break | ||||||
|  |       } | ||||||
|  |       case 3: { | ||||||
|  |         title += ' 安卓手表' | ||||||
|  |         break | ||||||
|  |       } | ||||||
|  |       case 4: { | ||||||
|  |         title += ' MacOS' | ||||||
|  |         break | ||||||
|  |       } | ||||||
|  |       case 5: { | ||||||
|  |         title += ' iPad' | ||||||
|  |         break | ||||||
|  |       } | ||||||
|  |       case 6: { | ||||||
|  |         title += ' Tim' | ||||||
|  |         break | ||||||
|  |       } | ||||||
|  |       default: { | ||||||
|  |         break | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 设置标题 | ||||||
|  |    */ | ||||||
|  |   process.title = title | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * 初始化事件 | ||||||
|  |  */ | ||||||
|  | export async function checkInit() { | ||||||
|  |   /** | ||||||
|  |    * 检查node_modules | ||||||
|  |    */ | ||||||
|  |   if (!fs.existsSync(join(process.cwd(), './node_modules'))) { | ||||||
|  |     console.log('未安装依赖。。。。') | ||||||
|  |     console.log('请先运行命令:pnpm install -P 安装依赖') | ||||||
|  |     process.exit() | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 检查node_modules/icqq | ||||||
|  |    */ | ||||||
|  |   if (!fs.existsSync(join(process.cwd(), './node_modules/icqq'))) { | ||||||
|  |     console.log('未安装icqq。。。。') | ||||||
|  |     console.log('请先运行命令:pnpm install -P 安装依赖') | ||||||
|  |     process.exit() | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 检查qq.yaml | ||||||
|  |    */ | ||||||
|  |   await createQQ() | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 日志设置 | ||||||
|  |    */ | ||||||
|  |   setLogger() | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * | ||||||
|  |    */ | ||||||
|  |   logger.mark(`${BOT_NAME} 启动中...`) | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    *  初始化客户端 | ||||||
|  |    */ | ||||||
|  |   await redisInit() | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 检查程序 | ||||||
|  |    */ | ||||||
|  |   await checkRun() | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * 更新标题 | ||||||
|  |    */ | ||||||
|  |   await UpdateTitle() | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * 设置标题 | ||||||
|  |  */ | ||||||
|  | process.title = BOT_NAME | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * 设置时区 | ||||||
|  |  */ | ||||||
|  | process.env.TZ = 'Asia/Shanghai' | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * | ||||||
|  |  */ | ||||||
|  | process.on('SIGHUP', () => process.exit()) | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * 捕获未处理的错误 | ||||||
|  |  */ | ||||||
|  | process.on('uncaughtException', error => { | ||||||
|  |   if (typeof logger == 'undefined') console.log(error) | ||||||
|  |   else logger.error(error) | ||||||
|  | }) | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * 捕获未处理的Promise错误 | ||||||
|  |  */ | ||||||
|  | process.on('unhandledRejection', error => { | ||||||
|  |   if (typeof logger == 'undefined') console.log(error) | ||||||
|  |   else logger.error(error) | ||||||
|  | }) | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * 退出事件 | ||||||
|  |  */ | ||||||
|  | process.on('exit', async () => { | ||||||
|  |   if (typeof redis != 'undefined') { | ||||||
|  |     await redis.save() | ||||||
|  |   } | ||||||
|  |   if (typeof logger == 'undefined') { | ||||||
|  |     console.log(`${BOT_NAME} 已停止运行`) | ||||||
|  |   } else { | ||||||
|  |     logger.mark(logger.magenta(`${BOT_NAME} 已停止运行`)) | ||||||
|  |   } | ||||||
|  | }) | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * 初始化 | ||||||
|  |  */ | ||||||
|  | await checkInit() | ||||||
							
								
								
									
										13
									
								
								src/main.ts
								
								
								
								
							
							
						
						
									
										13
									
								
								src/main.ts
								
								
								
								
							|  | @ -1,9 +1,14 @@ | ||||||
| /** | /** | ||||||
|  * |  * ********** | ||||||
|  |  * 配置初始化 | ||||||
|  |  * ********** | ||||||
|  |  */ | ||||||
|  | import './init.js' | ||||||
|  | /** | ||||||
|  |  * 引入模块 | ||||||
|  */ |  */ | ||||||
| import { plugin, segment, Client } from './core/index.js' | import { plugin, segment, Client } from './core/index.js' | ||||||
| import { Renderer } from './utils/index.js' | import { Renderer } from './utils/index.js' | ||||||
| 
 |  | ||||||
| /** | /** | ||||||
|  * global.plugin |  * global.plugin | ||||||
|  */ |  */ | ||||||
|  | @ -12,12 +17,10 @@ global.plugin = plugin | ||||||
|  * global.segment |  * global.segment | ||||||
|  */ |  */ | ||||||
| global.segment = segment | global.segment = segment | ||||||
| 
 |  | ||||||
| /** | /** | ||||||
|  * |  * global.Renderer | ||||||
|  */ |  */ | ||||||
| global.Renderer = Renderer | global.Renderer = Renderer | ||||||
| 
 |  | ||||||
| /** | /** | ||||||
|  * run |  * run | ||||||
|  */ |  */ | ||||||
|  |  | ||||||
|  | @ -54,15 +54,13 @@ class RendererLoader { | ||||||
|           !renderer.render || |           !renderer.render || | ||||||
|           !lodash.isFunction(renderer.render) |           !lodash.isFunction(renderer.render) | ||||||
|         ) { |         ) { | ||||||
|           console.warn( |           logger.warn('渲染后端 ' + (renderer.id || subFolder.name) + ' 不可用') | ||||||
|             '渲染后端 ' + (renderer.id || subFolder.name) + ' 不可用' |  | ||||||
|           ) |  | ||||||
|         } |         } | ||||||
|         this.renderers.set(renderer.id, renderer) |         this.renderers.set(renderer.id, renderer) | ||||||
|         console.info(`加载渲染后端 ${renderer.id}`) |         logger.info(`加载渲染后端 ${renderer.id}`) | ||||||
|       } catch (err) { |       } catch (err) { | ||||||
|         console.error(`渲染后端 ${name} 加载失败`) |         logger.error(`渲染后端 ${name} 加载失败`) | ||||||
|         console.error(err) |         logger.error(err) | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue