根据文件路径计算miao-plugin路径及yunzai路径,规避在外部import,非yunzai根目录执行脚本时,使用process.cwd()查找文件有可能错误的问题

This commit is contained in:
Kokomi 2024-05-22 02:05:37 +08:00
parent 3d1e602337
commit 0109f9ad2a
20 changed files with 65 additions and 42 deletions

View File

@ -2,9 +2,10 @@ import fs from 'node:fs'
import lodash from 'lodash' import lodash from 'lodash'
import { exec } from 'child_process' import { exec } from 'child_process'
import { Cfg, Common, Data, Version, App } from '#miao' import { Cfg, Common, Data, Version, App } from '#miao'
import makemsg from "../../../lib/common/common.js" import makemsg from '../../../lib/common/common.js'
import { execSync } from "child_process"; import { execSync } from 'child_process'
import fetch from 'node-fetch' import fetch from 'node-fetch'
import { miaoPath } from '#miao.path'
let keys = lodash.map(Cfg.getCfgSchemaMap(), (i) => i.key) let keys = lodash.map(Cfg.getCfgSchemaMap(), (i) => i.key)
let app = App.init({ let app = App.init({
@ -45,8 +46,7 @@ app.reg({
export default app export default app
const _path = process.cwd() const resPath = `${miaoPath}/resources/`
const resPath = `${_path}/plugins/miao-plugin/resources/`
const plusPath = `${resPath}/miao-res-plus/` const plusPath = `${resPath}/miao-res-plus/`
const checkAuth = async function (e) { const checkAuth = async function (e) {
@ -58,7 +58,7 @@ const checkAuth = async function (e) {
return true return true
} }
async function sysCfg(e) { async function sysCfg (e) {
if (!await checkAuth(e)) { if (!await checkAuth(e)) {
return true return true
} }
@ -99,7 +99,7 @@ async function sysCfg(e) {
}, { e, scale: 1.4 }) }, { e, scale: 1.4 })
} }
async function updateRes(e) { async function updateRes (e) {
if (!await checkAuth(e)) { if (!await checkAuth(e)) {
return true return true
} }
@ -143,7 +143,7 @@ async function updateRes(e) {
let timer let timer
async function updateMiaoPlugin(e) { async function updateMiaoPlugin (e) {
if (!await checkAuth(e)) { if (!await checkAuth(e)) {
return true return true
} }
@ -155,7 +155,7 @@ async function updateMiaoPlugin(e) {
} else { } else {
e.reply('正在执行更新操作,请稍等') e.reply('正在执行更新操作,请稍等')
} }
exec(command, { cwd: `${_path}/plugins/miao-plugin/` }, function (error, stdout, stderr) { exec(command, { cwd: miaoPath }, function (error, stdout, stderr) {
if (/(Already up[ -]to[ -]date|已经是最新的)/.test(stdout)) { if (/(Already up[ -]to[ -]date|已经是最新的)/.test(stdout)) {
e.reply('目前已经是最新版喵喵了~') e.reply('目前已经是最新版喵喵了~')
return true return true
@ -191,7 +191,7 @@ async function updateMiaoPlugin(e) {
return true return true
} }
async function Miaoupdatelog(e, plugin = 'miao-plugin') { async function Miaoupdatelog (e, plugin = 'miao-plugin') {
let cm = 'git log -20 --oneline --pretty=format:"%h||[%cd] %s" --date=format:"%F %T"' let cm = 'git log -20 --oneline --pretty=format:"%h||[%cd] %s" --date=format:"%F %T"'
if (plugin) { if (plugin) {
cm = `cd ./plugins/${plugin}/ && ${cm}` cm = `cd ./plugins/${plugin}/ && ${cm}`
@ -220,7 +220,7 @@ async function Miaoupdatelog(e, plugin = 'miao-plugin') {
e.reply(log) e.reply(log)
} }
async function miaoApiInfo(e) { async function miaoApiInfo (e) {
if (!await checkAuth(e)) { if (!await checkAuth(e)) {
return true return true
} }

View File

@ -2,6 +2,7 @@ import { Button, Character, MysApi, Player } from '#miao.models'
import { Cfg, Common, Meta } from '#miao' import { Cfg, Common, Meta } from '#miao'
import lodash from 'lodash' import lodash from 'lodash'
import moment from 'moment' import moment from 'moment'
import { miaoPath } from '#miao.path'
let Avatar = { let Avatar = {
render (e) { render (e) {
@ -46,7 +47,7 @@ let Avatar = {
return true return true
} }
if (renderType === 'photo') { if (renderType === 'photo') {
e.reply(segment.image(`file://${process.cwd()}/plugins/miao-plugin/resources/${bg.img}`)) e.reply(segment.image(`file://${miaoPath}/resources/${bg.img}`))
return true return true
} }
let uid = e.uid || (e.targetUser && e.targetUser.uid) let uid = e.uid || (e.targetUser && e.targetUser.uid)
@ -88,7 +89,7 @@ let Avatar = {
custom, custom,
isRelease, isRelease,
data data
}, { e, scale, retType: "base64" }), new Button(e).profile(char, uid)]) }, { e, scale, retType: 'base64' }), new Button(e).profile(char, uid)])
if (msgRes) { if (msgRes) {
// 如果消息发送成功就将message_id和图片路径存起来3小时过期 // 如果消息发送成功就将message_id和图片路径存起来3小时过期
const message_id = [e.message_id] const message_id = [e.message_id]

View File

@ -6,8 +6,9 @@ import fetch from 'node-fetch'
import lodash from 'lodash' import lodash from 'lodash'
import { Cfg, Data } from '#miao' import { Cfg, Data } from '#miao'
import { Character } from '#miao.models' import { Character } from '#miao.models'
import { miaoPath } from '#miao.path'
const resPath = process.cwd() + '/plugins/miao-plugin/resources/' const resPath = miaoPath + '/resources/'
let regex = /^#?\s*(?:喵喵)?(?:上传|添加)(.+)(?:照片|写真|图片|图像)\s*$/ let regex = /^#?\s*(?:喵喵)?(?:上传|添加)(.+)(?:照片|写真|图片|图像)\s*$/
let profileRegex = /^#?\s*(?:喵喵)?(?:上传|添加)(.+)(?:面板图)\s*$/ let profileRegex = /^#?\s*(?:喵喵)?(?:上传|添加)(.+)(?:面板图)\s*$/
let isProfile = false let isProfile = false

View File

@ -2,9 +2,9 @@ import { Cfg, Common, Data, Version } from '#miao'
import fs from 'node:fs' import fs from 'node:fs'
import lodash from 'lodash' import lodash from 'lodash'
import HelpTheme from './HelpTheme.js' import HelpTheme from './HelpTheme.js'
import { miaoPath } from '#miao.path'
const _path = process.cwd() const helpPath = `${miaoPath}/resources/help`
const helpPath = `${_path}/plugins/miao-plugin/resources/help`
const Help = { const Help = {
async render (e) { async render (e) {

View File

@ -1,8 +1,8 @@
/* /*
* 面板公共方法及处理 * 面板公共方法及处理
* */ * */
import { Version } from '#miao'
import { Button, Character, MysApi, Player } from '#miao.models' import { Button, Character, MysApi, Player } from '#miao.models'
import { miaoPath } from '#miao.path'
/* /*
* 获取面板查询的 目标uid * 获取面板查询的 目标uid
@ -72,6 +72,6 @@ export async function getProfileRefresh (e, avatar) {
* 面板帮助 * 面板帮助
* */ * */
export async function profileHelp (e) { export async function profileHelp (e) {
e.reply(segment.image(`file://${process.cwd()}/plugins/miao-plugin/resources/character/imgs/help.jpg`)) e.reply(segment.image(`file://${miaoPath}/resources/character/imgs/help.jpg`))
return true return true
} }

View File

@ -1,5 +1,6 @@
import { Cfg } from '#miao' import { Cfg } from '#miao'
import { MysApi } from '#miao.models' import { MysApi } from '#miao.models'
import { miaoPath } from '#miao.path'
/** 获取角色卡片的原图 */ /** 获取角色卡片的原图 */
export async function getOriginalPicture (e) { export async function getOriginalPicture (e) {
@ -48,12 +49,12 @@ export async function getOriginalPicture (e) {
} }
} }
if (imgPath && imgPath.img) { if (imgPath && imgPath.img) {
e.reply(segment.image(`file://${process.cwd()}/plugins/miao-plugin/resources/${decodeURIComponent(imgPath.img)}`), false, { recallMsg: 30 }) e.reply(segment.image(`file://${miaoPath}/resources/${decodeURIComponent(imgPath.img)}`), false, { recallMsg: 30 })
} }
return true return true
} }
// 对at错图像的增加嘲讽... // 对at错图像的增加嘲讽...
e.reply(segment.image(`file://${process.cwd()}/plugins/miao-plugin/resources/common/face/what.jpg`)) e.reply(segment.image(`file://${miaoPath}/resources/common/face/what.jpg`))
return false return false
} }
e.reply('消息太过久远了,俺也忘了原图是啥了,下次早点来吧~') e.reply('消息太过久远了,俺也忘了原图是啥了,下次早点来吧~')

View File

@ -4,6 +4,7 @@ import CharWikiData from './CharWikiData.js'
import CharMaterial from './CharMaterial.js' import CharMaterial from './CharMaterial.js'
import { Cfg, Common } from '#miao' import { Cfg, Common } from '#miao'
import { Character } from '#miao.models' import { Character } from '#miao.models'
import { miaoPath } from '#miao.path'
const wikiReg = /^(?:#|喵喵)?(?:星铁)?(.*)(天赋|技能|行迹|命座|命之座|星魂|资料|图鉴|照片|写真|图片|图像)$/ const wikiReg = /^(?:#|喵喵)?(?:星铁)?(.*)(天赋|技能|行迹|命座|命之座|星魂|资料|图鉴|照片|写真|图片|图像)$/
@ -53,7 +54,7 @@ const CharWiki = {
if (mode === 'pic') { if (mode === 'pic') {
let img = char.getCardImg(Cfg.get('charPicSe', false), false) let img = char.getCardImg(Cfg.get('charPicSe', false), false)
if (img && img.img) { if (img && img.img) {
e.reply(segment.image(`file://${process.cwd()}/plugins/miao-plugin/resources/${img.img}`)) e.reply(segment.image(`file://${miaoPath}/resources/${img.img}`))
} else { } else {
e.reply('暂无图片') e.reply('暂无图片')
} }

View File

@ -2,9 +2,9 @@ import fs from 'node:fs'
import lodash from 'lodash' import lodash from 'lodash'
import cfgData from './cfg/CfgData.js' import cfgData from './cfg/CfgData.js'
import { Version } from '#miao' import { Version } from '#miao'
import { miaoPath } from '#miao.path'
const _path = process.cwd() const _cfgPath = `${miaoPath}/components/`
const _cfgPath = `${_path}/plugins/miao-plugin/components/`
let cfg = {} let cfg = {}
let miaoCfg = {} let miaoCfg = {}

View File

@ -1,8 +1,9 @@
import lodash from 'lodash' import lodash from 'lodash'
import fs from 'node:fs' import fs from 'node:fs'
import util from 'node:util' import util from 'node:util'
import { rootPath } from '#miao.path'
const _path = process.cwd() const _path = rootPath
const getRoot = (root = '') => { const getRoot = (root = '') => {
if (!root) { if (!root) {
root = `${_path}/` root = `${_path}/`

View File

@ -2,6 +2,7 @@ import { cfgSchema } from '../../config/system/cfg_system.js'
import lodash from 'lodash' import lodash from 'lodash'
import { Data } from '../index.js' import { Data } from '../index.js'
import fs from 'node:fs' import fs from 'node:fs'
import { miaoPath } from '#miao.path'
let cfgData = { let cfgData = {
saveCfg (cfg) { saveCfg (cfg) {
@ -21,7 +22,7 @@ let cfgData = {
} }
}) })
}) })
fs.writeFileSync(`${process.cwd()}/plugins/miao-plugin/config/cfg.js`, ret.join('\n'), 'utf8') fs.writeFileSync(`${miaoPath}/config/cfg.js`, ret.join('\n'), 'utf8')
}, },
async getCfg () { async getCfg () {

View File

@ -1,4 +1,5 @@
import { Version, Cfg } from '#miao' import { Version, Cfg } from '#miao'
import { miaoPath } from '#miao.path'
const Render = { const Render = {
async render (path, params, cfg) { async render (path, params, cfg) {
@ -17,13 +18,13 @@ const Render = {
} }
} }
let resPath = data.pluResPath let resPath = data.pluResPath
const layoutPath = process.cwd() + '/plugins/miao-plugin/resources/common/layout/' const layoutPath = miaoPath + '/resources/common/layout/'
return { return {
_miao_path: resPath, _miao_path: resPath,
...data, ...data,
_res_path: resPath, _res_path: resPath,
_layout_path: layoutPath, _layout_path: layoutPath,
_tpl_path: process.cwd() + '/plugins/miao-plugin/resources/common/tpl/', _tpl_path: miaoPath + '/resources/common/tpl/',
defaultLayout: layoutPath + 'default.html', defaultLayout: layoutPath + 'default.html',
elemLayout: layoutPath + 'elem.html', elemLayout: layoutPath + 'elem.html',
sys: { sys: {

View File

@ -6,6 +6,7 @@ import DmgBuffs from './dmg/DmgBuffs.js'
import DmgAttr from './dmg/DmgAttr.js' import DmgAttr from './dmg/DmgAttr.js'
import DmgCalc from './dmg/DmgCalc.js' import DmgCalc from './dmg/DmgCalc.js'
import { MiaoError, Meta, Common } from '#miao' import { MiaoError, Meta, Common } from '#miao'
import { miaoPath } from '#miao.path'
export default class ProfileDmg extends Base { export default class ProfileDmg extends Base {
constructor (profile = {}, game = 'gs') { constructor (profile = {}, game = 'gs') {
@ -20,14 +21,13 @@ export default class ProfileDmg extends Base {
} }
static dmgRulePath (name, game = 'gs') { static dmgRulePath (name, game = 'gs') {
const _path = process.cwd()
let dmgFile = [ let dmgFile = [
{ file: 'calc_user', name: '自定义伤害' }, { file: 'calc_user', name: '自定义伤害' },
{ file: 'calc_auto', name: '组团伤害', test: () => Common.cfg('teamCalc') }, { file: 'calc_auto', name: '组团伤害', test: () => Common.cfg('teamCalc') },
{ file: 'calc', name: '喵喵' } { file: 'calc', name: '喵喵' }
] ]
for (let ds of dmgFile) { for (let ds of dmgFile) {
let path = `${_path}/plugins/miao-plugin/resources/meta-${game}/character/${name}/${ds.file}.js` let path = `${miaoPath}/resources/meta-${game}/character/${name}/${ds.file}.js`
if (ds.test && !ds.test()) { if (ds.test && !ds.test()) {
continue continue
} }

View File

@ -1,8 +1,9 @@
import { Data } from '#miao' import { Data } from '#miao'
import lodash from 'lodash' import lodash from 'lodash'
import fs from 'node:fs' import fs from 'node:fs'
import { miaoPath } from '#miao.path'
const charPath = process.cwd() + '/plugins/miao-plugin/resources/meta-gs/character' const charPath = `${miaoPath}/resources/meta-gs/character`
let cfgMap = { let cfgMap = {
char: {}, char: {},
async init () { async init () {

View File

@ -4,9 +4,9 @@
import fs from 'node:fs' import fs from 'node:fs'
import lodash from 'lodash' import lodash from 'lodash'
import sizeOf from 'image-size' import sizeOf from 'image-size'
import { Cfg } from '#miao' import { miaoPath } from '#miao.path'
const rPath = `${process.cwd()}/plugins/miao-plugin/resources` const rPath = `${miaoPath}/resources`
const CharImg = { const CharImg = {
// 获取角色的插画 // 获取角色的插画

View File

@ -14,13 +14,14 @@ import Weapon from './Weapon.js'
import User from './User.js' import User from './User.js'
import MysApi from './MysApi.js' import MysApi from './MysApi.js'
import Button from './Button.js' import Button from './Button.js'
import { rootPath } from '#miao.path'
for (let game of ['gs', 'sr']) { for (let game of ['gs', 'sr']) {
for (let type of ['artifact', 'character', 'material', 'weapon']) { for (let type of ['artifact', 'character', 'material', 'weapon']) {
let file = `./plugins/miao-plugin/resources/meta-${game}/${type}/index.js` let file = `./plugins/miao-plugin/resources/meta-${game}/${type}/index.js`
if (fs.existsSync(file)) { if (fs.existsSync(file)) {
try { try {
await import(`file://${process.cwd()}/${file}`) await import(`file://${rootPath}/${file}`)
} catch (e) { } catch (e) {
console.log(e) console.log(e)
} }

View File

@ -15,6 +15,7 @@
}, },
"imports": { "imports": {
"#miao": "./components/index.js", "#miao": "./components/index.js",
"#miao.models": "./models/index.js" "#miao.models": "./models/index.js",
"#miao.path": "./tools/path.js"
} }
} }

View File

@ -377,8 +377,4 @@
</div> </div>
</div> </div>
{{if game === 'sr' && data.dataSource === '喵喵Api'}}
<div class="copyright ad">关注微信公众号“星铁工具箱”获得更多信息</div>
{{/if}}
{{/block}} {{/block}}

View File

@ -1,6 +1,7 @@
import { Data, Version } from '#miao' import { Data, Version } from '#miao'
import fs from 'node:fs' import fs from 'node:fs'
import Trans from './trans.js' import Trans from './trans.js'
import { rootPath } from '#miao.path'
let relpyPrivate = async function () { let relpyPrivate = async function () {
} }
@ -33,7 +34,7 @@ const Index = {
if (!Version.isV3 && !Version.isAlemonjs) { if (!Version.isV3 && !Version.isAlemonjs) {
console.log('警告miao-plugin需要V3 Yunzai请升级至最新版Miao-Yunzai以使用miao-plugin') console.log('警告miao-plugin需要V3 Yunzai请升级至最新版Miao-Yunzai以使用miao-plugin')
} }
if (!fs.existsSync(process.cwd() + '/lib/plugins/runtime.js')) { if (!fs.existsSync(rootPath + '/lib/plugins/runtime.js')) {
let msg = '警告未检测到runtimemiao-plugin可能无法正常工作。请升级至最新版Miao-Yunzai以使用miao-plugin' let msg = '警告未检测到runtimemiao-plugin可能无法正常工作。请升级至最新版Miao-Yunzai以使用miao-plugin'
if (!await redis.get('miao:runtime-warning')) { if (!await redis.get('miao:runtime-warning')) {
await relpyPrivate(msg.qq, msg) await relpyPrivate(msg.qq, msg)

15
tools/path.js Normal file
View File

@ -0,0 +1,15 @@
/**
* 根据文件路径计算miao-plugin路径及yunzai路径
* 规避在外部import在非yunzai根目录执行时使用process.cwd()查找文件有可能错误的问题
*/
import * as path from 'path'
import { fileURLToPath } from 'url'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
// miao-plugin根路径
const miaoPath = path.join(__dirname, '..')
// yunzai根路径
const rootPath = path.join(miaoPath, '..', '..')
export { miaoPath, rootPath }

View File

@ -2,6 +2,7 @@ import express from 'express'
import template from 'express-art-template' import template from 'express-art-template'
import fs from 'node:fs' import fs from 'node:fs'
import lodash from 'lodash' import lodash from 'lodash'
import { rootPath } from '#miao.path'
/* /*
* npm run app web-debug开启Bot后 * npm run app web-debug开启Bot后
@ -15,7 +16,7 @@ import lodash from 'lodash'
let app = express() let app = express()
let _path = process.cwd() let _path = rootPath
app.engine('html', template) app.engine('html', template)
app.set('views', _path + '/resources/') app.set('views', _path + '/resources/')