fix: 修复函数checkRun不规范写法
This commit is contained in:
parent
15a5bbd349
commit
9da3312f43
|
@ -1,22 +1,44 @@
|
||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
import { exec } from 'child_process'
|
import { exec } from 'child_process'
|
||||||
|
import { join } from 'path'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* 校验运行
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export async function checkRun() {
|
export async function checkRun() {
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
if (process.argv[1].includes('pm2')) return
|
if (process.argv[1].includes('pm2')) return
|
||||||
if (process.argv[1].includes('test')) 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
|
* @param cmd
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
async function execSync(cmd) {
|
function execAsync(cmd: string): Promise<{
|
||||||
|
stdout: string,
|
||||||
|
stderr: string
|
||||||
|
}> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
exec(cmd, (error, stdout, stderr) => {
|
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
|
* @returns
|
||||||
*/
|
*/
|
||||||
function pm2Cfg() {
|
function pm2Cfg() {
|
||||||
let cfg = fs.readFileSync('./config/pm2/pm2.json')
|
try {
|
||||||
cfg = JSON.parse(cfg)
|
const cfg = fs.readFileSync(join(process.cwd(), './config/pm2/pm2.json'), 'utf-8')
|
||||||
return cfg
|
return JSON.parse(cfg)
|
||||||
|
} catch {
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue