增加`#版本`命令

This commit is contained in:
Kokomi 2023-03-10 04:51:48 +08:00
parent 4938028e24
commit 74f5d7a861
3 changed files with 46 additions and 18 deletions

View File

@ -1,16 +1,16 @@
import template from 'art-template'
import fs from 'fs'
import fs from 'node:fs'
import os from 'node:os'
import lodash from 'lodash'
import template from 'art-template'
import chokidar from 'chokidar'
import cfg from '../config/config.js'
import os from 'os'
const _path = process.cwd()
let puppeteer = {}
// mac地址
let mac =''
let mac = ''
class Puppeteer {
constructor () {
@ -78,15 +78,15 @@ class Puppeteer {
//如果是pm2启动尝试连接已有实例
if (process.env.pm_id) {
//获取Mac地址
if (!mac){
if (!mac) {
mac = await this.getMac()
this.browserMacKey = `Yz:chromium:browserWSEndpoint:${mac}`
}
//是否有browser实例
const browserUrl = await redis.get(this.browserMacKey)
if (browserUrl){
const browserWSEndpoint = await puppeteer.connect({browserWSEndpoint: browserUrl}).catch((err) => {
logger.error("puppeteer Chromium 缓存的实例已关闭")
if (browserUrl) {
const browserWSEndpoint = await puppeteer.connect({ browserWSEndpoint: browserUrl }).catch((err) => {
logger.error('puppeteer Chromium 缓存的实例已关闭')
redis.del(this.browserMacKey)
})
//如果有实例,直接使用
@ -98,12 +98,12 @@ class Puppeteer {
}
}
}
}catch (e) {
logger.error("puppeteer Chromium 尝试连接已有实例失败")
} catch (e) {
logger.error('puppeteer Chromium 尝试连接已有实例失败')
}
if (!this.browser||!connectFlag){
if (!this.browser || !connectFlag) {
//如果没有实例初始化puppeteer
this.browser = await puppeteer.launch(this.config).catch((err, trace) => {
let errMsg = err.toString() + (trace ? trace.toString() : '')
@ -127,10 +127,10 @@ class Puppeteer {
logger.error('puppeteer Chromium 启动失败')
return false
}
if (connectFlag){
if (connectFlag) {
logger.mark('puppeteer Chromium 已连接启动的实例')
}else {
console.log("chromium",this.browser.wsEndpoint())
} else {
console.log('chromium', this.browser.wsEndpoint())
if (this.browserMacKey) {
//缓存一下实例30天
const expireTime = 60 * 60 * 24 * 30
@ -151,7 +151,7 @@ class Puppeteer {
//获取Mac地址
async getMac () {
//获取Mac地址
let mac="00:00:00:00:00:00"
let mac = '00:00:00:00:00:00'
try {
const network = os.networkInterfaces()
let osMac
@ -159,17 +159,17 @@ class Puppeteer {
if (os.platform() === 'win32') {
//windows下获取mac地址
let osMacList = Object.keys(network).map(key => network[key]).flat()
osMacList=osMacList.filter(item => item.family === 'IPv4'&&item.mac!==mac)
osMacList = osMacList.filter(item => item.family === 'IPv4' && item.mac !== mac)
osMac = osMacList[0].mac
} else if (os.platform() === 'linux') {
//linux下获取mac地址
osMac = network.eth0.filter(item => item.family === 'IPv4'&&item.mac!==mac)[0].mac
osMac = network.eth0.filter(item => item.family === 'IPv4' && item.mac !== mac)[0].mac
}
if (osMac) {
mac = String(osMac)
}
} catch (e) {
console.log("获取Mac地址失败", e.toString())
console.log('获取Mac地址失败', e.toString())
}
mac = mac.replace(/:/g, '')
return mac

1
plugins/.gitignore vendored
View File

@ -14,6 +14,7 @@ other/**
!other/sendLog.js
!other/update.js
!other/setPubCk.js
!other/version.js
!example
example/**

27
plugins/other/version.js Normal file
View File

@ -0,0 +1,27 @@
import { App, Common, Version } from '#miao'
let app = App.init({
id: 'version',
name: '版本',
desc: '版本'
})
app.reg({
version: {
rule: /^#版本$/,
desc: '【#帮助】 版本介绍',
fn: async function (e) {
let { changelogs, currentVersion } = Version.readLogFile('root')
return await Common.render('help/version-info', {
currentVersion,
changelogs,
name: 'Miao-Yunzai',
elem: 'cryo',
pluginName: false,
pluginVersion: false
}, { e, scale: 1.2 })
}
}
})
export const version = app.v3App()