Skip to content

Commit 6aaaafa

Browse files
committed
Config add: Browser open
1 parent eb6cfed commit 6aaaafa

File tree

5 files changed

+58
-10
lines changed

5 files changed

+58
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.env
12
core/config.yaml
23
core/db.yaml
34
dist/

core/client.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ window.addEventListener('unload', (event) => {
106106
})
107107

108108
buttonClose.addEventListener('click', () => {
109+
socket.send(JSON.stringify({ action: 'stopCode' }))
109110
// Closes the current window or tab
110111
window.close()
111112
})

package-lock.json

Lines changed: 18 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"dependencies": {
1111
"axios": "^1.5.1",
1212
"child_process": "^1.0.2",
13+
"dotenv": "^16.3.1",
1314
"express": "^4.18.2",
1415
"fontkit": "^2.0.2",
1516
"fs-extra": "^11.1.1",

server.js

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ const WebSocket = require('ws')
44
const path = require('path')
55
const fs = require('fs-extra')
66
const open = require('openurl')
7+
const { exec } = require('child_process')
8+
9+
require('dotenv').config() // Load environment variables from .env
710

811
const argsv = process.argv.slice(2)
912

@@ -111,7 +114,7 @@ const actions = {
111114
getVariables: (ws, data) => sendVariableData(ws, GlobalVariables, Config, data.classElement),
112115
createData: (ws, data) => createData(data),
113116
removeData: (ws, data) => removeData(data),
114-
stopCode: () => process.exit()
117+
stopCode: () => stopCode()
115118
}
116119

117120
function sendToAllClients (data) {
@@ -289,6 +292,11 @@ function removeData (data) {
289292
saveVariablesToYAML(GlobalVariables)
290293
}
291294

295+
function stopCode () {
296+
console.log('The code has stopped successfully')
297+
process.exit()
298+
}
299+
292300
// WebSocket connections handling
293301
wss.on('connection', (ws) => {
294302
ws.send(JSON.stringify({ fonts: fontOptions }))
@@ -408,10 +416,35 @@ app.get('/:classElement/control&:request', (req, res) => {
408416

409417
function openUrlWhenConfigExists () {
410418
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+
}
412445
} 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
415448
}
416449
}
417450

0 commit comments

Comments
 (0)