@@ -4,6 +4,9 @@ const WebSocket = require('ws')
4
4
const path = require ( 'path' )
5
5
const fs = require ( 'fs-extra' )
6
6
const open = require ( 'openurl' )
7
+ const { exec } = require ( 'child_process' )
8
+
9
+ require ( 'dotenv' ) . config ( ) // Load environment variables from .env
7
10
8
11
const argsv = process . argv . slice ( 2 )
9
12
@@ -111,7 +114,7 @@ const actions = {
111
114
getVariables : ( ws , data ) => sendVariableData ( ws , GlobalVariables , Config , data . classElement ) ,
112
115
createData : ( ws , data ) => createData ( data ) ,
113
116
removeData : ( ws , data ) => removeData ( data ) ,
114
- stopCode : ( ) => process . exit ( )
117
+ stopCode : ( ) => stopCode ( )
115
118
}
116
119
117
120
function sendToAllClients ( data ) {
@@ -289,6 +292,11 @@ function removeData (data) {
289
292
saveVariablesToYAML ( GlobalVariables )
290
293
}
291
294
295
+ function stopCode ( ) {
296
+ console . log ( 'The code has stopped successfully' )
297
+ process . exit ( )
298
+ }
299
+
292
300
// WebSocket connections handling
293
301
wss . on ( 'connection' , ( ws ) => {
294
302
ws . send ( JSON . stringify ( { fonts : fontOptions } ) )
@@ -408,10 +416,35 @@ app.get('/:classElement/control&:request', (req, res) => {
408
416
409
417
function openUrlWhenConfigExists ( ) {
410
418
if ( Config ) {
411
- open . open ( `http://localhost:${ PORT } ` )
419
+ const customBrowser = process . env . BROWSER // Get the value of the BROWSER environment variable
420
+
421
+ if ( customBrowser ) {
422
+ try {
423
+ // If the BROWSER environment variable is defined, try to open that browser
424
+ const url = `http://localhost:${ PORT } `
425
+ switch ( process . platform ) {
426
+ case 'darwin' : // macOS
427
+ exec ( `open -a "${ customBrowser } " ${ url } ` )
428
+ break
429
+ case 'win32' : // Windows
430
+ exec ( `start ${ customBrowser } ${ url } ` )
431
+ break
432
+ case 'linux' : // Linux
433
+ exec ( `${ customBrowser } ${ url } ` )
434
+ break
435
+ default :
436
+ console . error ( 'Unsupported operating system.' )
437
+ }
438
+ } catch {
439
+ open . open ( `http://localhost:${ PORT } ` )
440
+ }
441
+ } else {
442
+ // If the BROWSER environment variable is not defined, open the default browser
443
+ open . open ( `http://localhost:${ PORT } ` )
444
+ }
412
445
} else {
413
- // Config todavía no existe, espera y vuelve a verificar en un momento
414
- setTimeout ( openUrlWhenConfigExists , 1000 ) // Espera 1 segundo antes de verificar nuevamente
446
+ // Configuration doesn't exist yet, wait and check again in a moment
447
+ setTimeout ( openUrlWhenConfigExists , 1000 ) // Wait for 1 second before checking again
415
448
}
416
449
}
417
450
0 commit comments