@@ -4,7 +4,8 @@ import { menubar } from 'menubar';
44
55import { APPLICATION } from '../shared/constants' ;
66import { namespacedEvent } from '../shared/events' ;
7- import { isMacOS , isWindows } from '../shared/platform' ;
7+ import { logInfo , logWarn } from '../shared/logger' ;
8+ import { isLinux , isMacOS , isWindows } from '../shared/platform' ;
89import { onFirstRunMaybe } from './first-run' ;
910import { TrayIcons } from './icons' ;
1011import MenuBuilder from './menu' ;
@@ -90,6 +91,31 @@ app.whenReady().then(async () => {
9091 } ) ;
9192 } ) ;
9293
94+ /** Prevent second instances */
95+ if ( isWindows ( ) || isLinux ( ) ) {
96+ const gotTheLock = app . requestSingleInstanceLock ( ) ;
97+
98+ if ( ! gotTheLock ) {
99+ logWarn ( 'main:gotTheLock' , 'Second instance detected, quitting' ) ;
100+ app . quit ( ) ; // Quit the second instance
101+ return ;
102+ }
103+
104+ app . on ( 'second-instance' , ( _event , commandLine , _workingDirectory ) => {
105+ logInfo (
106+ 'main:second-instance' ,
107+ 'Second instance was launched. extracting command to forward' ,
108+ ) ;
109+
110+ // Get the URL from the command line arguments
111+ const url = commandLine . find ( ( arg ) => arg . startsWith ( `${ protocol } ://` ) ) ;
112+
113+ if ( url ) {
114+ handleURL ( url ) ;
115+ }
116+ } ) ;
117+ }
118+
93119 /**
94120 * Gitify custom IPC events
95121 */
@@ -183,5 +209,13 @@ ipc.handle(namespacedEvent('safe-storage-decrypt'), (_, settings) => {
183209// Handle gitify:// custom protocol URL events for OAuth 2.0 callback
184210app . on ( 'open-url' , ( event , url ) => {
185211 event . preventDefault ( ) ;
186- mb . window . webContents . send ( namespacedEvent ( 'auth-callback' ) , url ) ;
212+ logInfo ( 'main:open-url' , `URL received ${ url } ` ) ;
213+ handleURL ( url ) ;
187214} ) ;
215+
216+ const handleURL = ( url : string ) => {
217+ if ( url . startsWith ( `${ protocol } ://` ) ) {
218+ logInfo ( 'main:handleUrl' , `forwarding URL ${ url } to renderer process` ) ;
219+ mb . window . webContents . send ( namespacedEvent ( 'auth-callback' ) , url ) ;
220+ }
221+ } ;
0 commit comments