优化pnpm log的名称获取方法,防止多开用户修改名称导致冲突

This commit is contained in:
SmallK111407 2023-08-21 09:26:31 +08:00
parent 717a7bad87
commit 24d78587c9
2 changed files with 36 additions and 1 deletions

35
lib/tools/name.js Normal file
View File

@ -0,0 +1,35 @@
import fs from "node:fs"
import childProcess from "child_process"
const _path = process.cwd()
fs.readFile(`${_path}/config/pm2/pm2.json`, `utf8`, (err, data) => {
if (err) {
console.log('pm2.json文件读取错误:', err)
return
}
try {
const config = JSON.parse(data)
if (config.apps && config.apps.length > 0 && config.apps[0].name) {
const appName = config.apps[0].name
console.log(config.apps[0].name)
runPm2Logs(appName)
} else {
console.log('读取失败无法在pm2.json中找到name数组')
}
} catch (parseError) {
console.log('读取失败json文件解析发生了错误', parseError)
}
})
function runPm2Logs(appName) {
const command = process.platform === 'win32' ? 'pm2.cmd' : 'pm2'
const args = ['logs', '--lines', '400', appName]
const pm2LogsProcess = childProcess.spawn(command, args, { stdio: 'inherit' })
pm2LogsProcess.on('exit', (code) => {
if (code !== 0) {
console.error(`pm2 logs process exited with code ${code}`)
}
})
}

View File

@ -14,7 +14,7 @@
"start": "pm2 start ./config/pm2/pm2.json", "start": "pm2 start ./config/pm2/pm2.json",
"stop": "pm2 stop ./config/pm2/pm2.json", "stop": "pm2 stop ./config/pm2/pm2.json",
"restart": "pm2 restart ./config/pm2/pm2.json", "restart": "pm2 restart ./config/pm2/pm2.json",
"log": "pm2 logs --lines 400 Miao-Yunzai" "log": "node ./lib/tools/name.js"
}, },
"dependencies": { "dependencies": {
"art-template": "^4.13.2", "art-template": "^4.13.2",