diff --git a/runner/app.js b/runner/app.js index 0708361e3..a2e924364 100644 --- a/runner/app.js +++ b/runner/app.js @@ -1,15 +1,55 @@ const path = require('path'); const fs = require('fs'); - +const crypto = require('crypto'); +const os = require('os'); const express = require('express') const { detect } = require('detect-port') - const spawn = require('child_process').spawn; const remoteMain = require('@electron/remote/main'); remoteMain.initialize(); +function getInstanceLockFilePath(appFilesPath) { + // Create a hash of the app files path to use in the lock file name + const hash = crypto.createHash('md5').update(appFilesPath).digest('hex'); + const ceramicDir = path.join(os.homedir(), '.ceramic'); + + // Ensure .ceramic directory exists + if (!fs.existsSync(ceramicDir)) { + fs.mkdirSync(ceramicDir, { recursive: true }); + } + + return path.join(ceramicDir, `instance-${hash}.lock`); +} + +function setupInstanceManager(appFilesPath) { + const lockFile = getInstanceLockFilePath(appFilesPath); + + // Touch the lock file to signal our presence + fs.writeFileSync(lockFile, String(process.pid), 'utf8'); + + // Watch for changes to the lock file + fs.watchFile(lockFile, { interval: 1000 }, (curr, prev) => { + if (curr.mtime > prev.mtime) { + console.log('New instance detected, shutting down...'); + app.quit(); + } + }); + + // Clean up on exit + app.on('will-quit', () => { + fs.unwatchFile(lockFile); + try { + if (fs.readFileSync(lockFile, 'utf8') === String(process.pid)) { + fs.unlinkSync(lockFile); + } + } catch (err) { + console.error('Error cleaning up lock file:', err); + } + }); +} + // Electron const electron = require('electron'); // Module to control application life. @@ -68,6 +108,9 @@ if (!fs.existsSync(appFiles)) { process.exit(1); } +// Set up instance management immediately after validating appFiles +setupInstanceManager(appFiles); + // Set cwd to appFiles process.chdir(appFiles); diff --git a/runner/electron.cmd b/runner/electron.cmd deleted file mode 100644 index f3c408264..000000000 --- a/runner/electron.cmd +++ /dev/null @@ -1,2 +0,0 @@ -@echo off -%~dp0/../node/node_modules/.bin/node %~dp0/node_modules/electron/cli.js %* \ No newline at end of file diff --git a/tools/src/tools/Helpers.hx b/tools/src/tools/Helpers.hx index 2b2523ff0..5720e69c4 100644 --- a/tools/src/tools/Helpers.hx +++ b/tools/src/tools/Helpers.hx @@ -404,7 +404,7 @@ class Helpers { public static function node(args:Array, ?options:{ ?cwd:String, ?mute:Bool, ?detached:Bool, ?tick:()->Void }) { - var node = Path.join([context.ceramicToolsPath, '../node/node_modules/.bin/node']); + var node = 'node'; if (Sys.systemName() == 'Windows') node += '.cmd'; return command(node, args, options);