File size: 760 Bytes
330c0d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import fs from 'node:fs'
import { exec } from 'node:child_process'

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}`)

  if (status.stdout.includes('online')) {
    logger.mark('检测到后台正在运行')
    logger.mark('已停止后台进程,防止重复运行')
    execSync(`pm2 stop ${cfg.apps[0].name}`)
  }
}

async function execSync (cmd) {
  return new Promise((resolve, reject) => {
    exec(cmd, (error, stdout, stderr) => {
      resolve({ error, stdout, stderr })
    })
  })
}

function pm2Cfg () {
  let cfg = fs.readFileSync('pm2.json')
  cfg = JSON.parse(cfg)
  return cfg
}