fix: 修复函数checkRun不规范写法

This commit is contained in:
ningmengchongshui 2024-06-09 19:23:29 +08:00
parent 15a5bbd349
commit 9da3312f43
1 changed files with 42 additions and 13 deletions

View File

@ -1,22 +1,44 @@
import fs from 'fs'
import { exec } from 'child_process'
import { join } from 'path'
/**
*
*
* @returns
*/
export async function checkRun() {
/**
*
*/
if (process.argv[1].includes('pm2')) return
if (process.argv[1].includes('test')) return
let cfg = pm2Cfg()
let status = await execSync(`pm2 show ${cfg.apps[0].name}`)
/**
*
*/
const cfg = pm2Cfg()
if (!cfg) return
/**
*
*/
execAsync(`pm2 show ${cfg.apps[0].name}`).then((status) => {
/**
*
*/
if (status.stdout.includes('online')) {
//
logger.mark('检测到后台正在运行')
logger.mark('已停止后台进程,防止重复运行')
execAsync(`pm2 stop ${cfg.apps[0].name}`).catch(logger.error)
//
}
}).catch(logger.error)
if (status.stdout.includes('online')) {
logger.mark('检测到后台正在运行')
logger.mark('已停止后台进程,防止重复运行')
execSync(`pm2 stop ${cfg.apps[0].name}`)
}
}
/**
@ -24,10 +46,14 @@ export async function checkRun() {
* @param cmd
* @returns
*/
async function execSync(cmd) {
function execAsync(cmd: string): Promise<{
stdout: string,
stderr: string
}> {
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
resolve({ error, stdout, stderr })
if (error) reject(error)
resolve({ stdout, stderr })
})
})
}
@ -37,7 +63,10 @@ async function execSync(cmd) {
* @returns
*/
function pm2Cfg() {
let cfg = fs.readFileSync('./config/pm2/pm2.json')
cfg = JSON.parse(cfg)
return cfg
try {
const cfg = fs.readFileSync(join(process.cwd(), './config/pm2/pm2.json'), 'utf-8')
return JSON.parse(cfg)
} catch {
return false
}
}