feat: 优化顺序

This commit is contained in:
ningmengchongshui 2024-06-13 21:15:53 +08:00
parent 668b12e23f
commit d4bf0cb718
9 changed files with 229 additions and 191 deletions

View File

@ -1,4 +1,4 @@
import { execAsync, readJSON } from '../utils/index.js'
import { execAsync, readJSON } from "./utils.js"
/**
*

View File

@ -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()

View File

@ -3,7 +3,7 @@ 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/common.js'
import { sleep } from "./utils.js"
/**
* qq配置文件 `config/bot/qq.yaml`

View File

@ -1,5 +1,5 @@
import cfg from "./config.js"
import { execAsync, sleep } from "../utils/common.js"
import { execAsync, sleep } from "./utils.js"
import { createClient } from "redis"
/**

42
src/config/utils.ts Normal file
View File

@ -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
}
}

View File

@ -1,9 +1,3 @@
/**
* **********
*
* **********
*/
import '../config/main.js'
/**
* **********
*

171
src/init.ts Normal file
View File

@ -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()

View File

@ -1,9 +1,14 @@
/**
*
* **********
*
* **********
*/
import './init.js'
/**
*
*/
import { plugin, segment, Client } from './core/index.js'
import { Renderer } from './utils/index.js'
/**
* global.plugin
*/
@ -12,12 +17,10 @@ global.plugin = plugin
* global.segment
*/
global.segment = segment
/**
*
* global.Renderer
*/
global.Renderer = Renderer
/**
* run
*/

View File

@ -54,15 +54,13 @@ class RendererLoader {
!renderer.render ||
!lodash.isFunction(renderer.render)
) {
console.warn(
'渲染后端 ' + (renderer.id || subFolder.name) + ' 不可用'
)
logger.warn('渲染后端 ' + (renderer.id || subFolder.name) + ' 不可用')
}
this.renderers.set(renderer.id, renderer)
console.info(`加载渲染后端 ${renderer.id}`)
logger.info(`加载渲染后端 ${renderer.id}`)
} catch (err) {
console.error(`渲染后端 ${name} 加载失败`)
console.error(err)
logger.error(`渲染后端 ${name} 加载失败`)
logger.error(err)
}
}
}