From bb96bff1312a2c55022904edb43485745a9bea83 Mon Sep 17 00:00:00 2001 From: Kubat <mael.martin31@gmail.com> Date: Thu, 11 Feb 2021 19:45:11 +0100 Subject: [PATCH] First 'attach' mode, works fine --- main.js | 52 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/main.js b/main.js index f43be82..61d04ce 100644 --- a/main.js +++ b/main.js @@ -13,17 +13,53 @@ var client; /* Sub process for the express server */ class Lektor { constructor() { - this.closed = false; - this.process = spawn('lektord', ['-F']); + this.closed = true; - this.process.stderr.on('data', data => { - logger.lektord(data); + this.isRunning('lektord.exe', 'lektord', 'lektord').then(v => { + if (v && config.attachmode) { + logger.info('main', `Lektord is running! Attach to it`); + } else if (v) { + logger.error('error', `Lektord is running and attach mode is disable, exit!`); + app.quit(); + } else { + logger.debug('main', `Lektord not running, start it!`); + this.closed = false; + this.process = spawn('lektord', ['-F']); + + this.process.stderr.on('data', data => { + logger.lektord(data); + }); + + this.process.on('close', code => { + logger.warn('main', `Lektor exited with code ${code}`); + this.lektor_closed = true; + app.quit(); + }); + } }); + } - this.process.on('close', code => { - logger.warn('main', `Lektor exited with code ${code}`); - this.lektor_closed = true; - app.quit(); + isRunning(win, mac, linux) { + return new Promise(function (resolve, reject) { + //var ps = require('ps-node'); + const exec = require('child_process').exec; + + const plat = process.platform; + const cmd = + plat == 'win32' + ? 'tasklist' + : plat == 'darwin' + ? 'ps -ax | grep ' + mac + : plat == 'linux' + ? 'ps -A' + : ''; + const proc = plat == 'win32' ? win : plat == 'darwin' ? mac : plat == 'linux' ? linux : ''; + if (cmd === '' || proc === '') { + resolve(false); + } + exec(cmd, function (err, stdout, stderr) { + resolve(stdout.toLowerCase().indexOf(proc.toLowerCase()) > -1); + }); }); } -- GitLab