From 24d78587c99b5424d7bdc5c18f0a4a48e5a76dd6 Mon Sep 17 00:00:00 2001 From: SmallK111407 <3399280843@qq.com> Date: Mon, 21 Aug 2023 09:26:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96pnpm=20log=E7=9A=84=E5=90=8D?= =?UTF-8?q?=E7=A7=B0=E8=8E=B7=E5=8F=96=E6=96=B9=E6=B3=95=EF=BC=8C=E9=98=B2?= =?UTF-8?q?=E6=AD=A2=E5=A4=9A=E5=BC=80=E7=94=A8=E6=88=B7=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E5=90=8D=E7=A7=B0=E5=AF=BC=E8=87=B4=E5=86=B2=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tools/name.js | 35 +++++++++++++++++++++++++++++++++++ package.json | 2 +- 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 lib/tools/name.js diff --git a/lib/tools/name.js b/lib/tools/name.js new file mode 100644 index 0000000..d4ff2f2 --- /dev/null +++ b/lib/tools/name.js @@ -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}`) + } + }) +} \ No newline at end of file diff --git a/package.json b/package.json index cb7a268..5191cd4 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "start": "pm2 start ./config/pm2/pm2.json", "stop": "pm2 stop ./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": { "art-template": "^4.13.2",