diff --git a/src/config/check.ts b/src/config/check.ts index 074bc7a..c01709b 100644 --- a/src/config/check.ts +++ b/src/config/check.ts @@ -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 + } }