Skip to content

Commit

Permalink
convert js and mjs to mts
Browse files Browse the repository at this point in the history
  • Loading branch information
YeonV committed Dec 14, 2024
1 parent 7998b3e commit d79c320
Show file tree
Hide file tree
Showing 22 changed files with 257 additions and 136 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
"build:default": "GENERATE_SOURCEMAP=false CI=false react-scripts build",
"prebuild": "node -e \"let pkg=require('./package.json'); pkg.homepage='/'; require('fs').writeFileSync('package.json', JSON.stringify(pkg, null, 2));\"",
"postbuild": "run-script-os",
"postbuild:win32": "node -e \"let pkg=require('./package.json'); pkg.homepage='.'; require('fs').writeFileSync('package.json', JSON.stringify(pkg, null, 2));\" && del /q build\\app && rm -rf build\\preload.js && rm -rf build\\renderer.js && rm -rf build\\electron.js",
"postbuild:win32": "node -e \"let pkg=require('./package.json'); pkg.homepage='.'; require('fs').writeFileSync('package.json', JSON.stringify(pkg, null, 2));\" && del /q build\\app && del /q build\\preload.js && del /q build\\renderer.js && del /q build\\electron.js",
"postbuild:default": "node -e \"let pkg=require('./package.json'); pkg.homepage='.'; require('fs').writeFileSync('package.json', JSON.stringify(pkg, null, 2));\" && rm -rf build/app && rm -rf build/preload.js && rm -rf build/renderer.js && rm -rf build/electron.js",
"buildhass": "GENERATE_SOURCEMAP=false CI=false react-scripts build",
"prebuildhass": "node -e \"let pkg=require('./package.json'); pkg.homepage='./'; require('fs').writeFileSync('package.json', JSON.stringify(pkg, null, 2));\"",
Expand Down Expand Up @@ -195,6 +195,7 @@
"@types/eslint__js": "^8.42.3",
"@types/node": "^22.10.2",
"@types/prop-types": "^15.7.11",
"@types/qrcode": "^1.5.5",
"@types/react": "^18.3.12",
"@types/react-beautiful-dnd": "^13.1.8",
"@types/react-dom": "^18.2.19",
Expand Down Expand Up @@ -287,4 +288,4 @@
]
}
}
}
}
67 changes: 47 additions & 20 deletions src/app/app/handlers.js → src/app/app/handlers.mts
Original file line number Diff line number Diff line change
@@ -1,34 +1,47 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { app, shell, BrowserWindow, nativeTheme } from 'electron'
import path from 'path'
import { generateMfaQr, handleVerifyOTP } from './otp.js'
import { startInstance, stopInstance, sendStatus } from './instances.js'
import {
startInstance,
stopInstance,
sendStatus,
Subprocesses,
IPlatform
} from './instances.mjs'
import coreParams from './utils/coreParams.mjs'
import defaultCoreParams from './utils/defaultCoreParams.mjs'
import store from './utils/store.mjs'
import isCC from './utils/isCC.mjs'

export const handlers = async (wind, subprocesses, event, parameters) => {
export const handlers = async (
wind: BrowserWindow,
subprocesses: Subprocesses,
event: any,
parameters: any
) => {
console.log('ALL PARAMS', parameters)

try {
switch (parameters.command) {
case 'close-others':
BrowserWindow.getAllWindows().forEach(win => {
BrowserWindow.getAllWindows().forEach((win) => {
if (win !== wind) {
win.close();
win.close()
}
});
break;
case 'get-all-windows':
})
break
case 'get-all-windows': {
const allWIndows = BrowserWindow.getAllWindows()
console.log('allWIndows', allWIndows)
wind.webContents.send('fromMain', ['all-windows', allWIndows])
break
}
case 'verify_otp':
handleVerifyOTP(wind, event, parameters)
handleVerifyOTP(wind, parameters)
break
case 'generate-mfa-qr':
generateMfaQr(event, parameters)
generateMfaQr(wind)
break
case 'get-platform':
wind.webContents.send('fromMain', ['platform', process.platform])
Expand All @@ -39,7 +52,7 @@ export const handlers = async (wind, subprocesses, event, parameters) => {
'coreParams',
coreParams[process.platform]
])
sendStatus(wind, subprocesses)
sendStatus(wind, subprocesses, false, parameters.instance)
}
break
case 'start-core':
Expand Down Expand Up @@ -81,12 +94,12 @@ export const handlers = async (wind, subprocesses, event, parameters) => {
case 'delete-core-params':
if (isCC) {
store.set('coreParams', defaultCoreParams)
coreParams.darwin = defaultCoreParams['darwin']
coreParams.win32 = defaultCoreParams['win32']
coreParams.linux = defaultCoreParams['linux']
coreParams.darwin = defaultCoreParams.darwin
coreParams.win32 = defaultCoreParams.win32
coreParams.linux = defaultCoreParams.linux
wind.webContents.send('fromMain', [
'coreParams',
defaultCoreParams[process.platform]
defaultCoreParams[process.platform as IPlatform]
])
app.relaunch()
app.exit()
Expand All @@ -97,20 +110,35 @@ export const handlers = async (wind, subprocesses, event, parameters) => {

if (parameters.instance && parameters.instance !== 'instance1') {
shell.showItemInFolder(
path.join(app.getPath("userData"), '.ledfx-cc', parameters.instance, 'config.json')
path.join(
app.getPath('userData'),
'.ledfx-cc',
parameters.instance,
'config.json'
)
)
shell.showItemInFolder(
path.join(app.getPath("appData"), '.ledfx-cc', parameters.instance, 'config.json')
path.join(
app.getPath('appData'),
'.ledfx-cc',
parameters.instance,
'config.json'
)
)
shell.showItemInFolder(
path.join(app.getPath("home"), '.ledfx-cc', parameters.instance, 'config.json')
path.join(
app.getPath('home'),
'.ledfx-cc',
parameters.instance,
'config.json'
)
)
} else {
shell.showItemInFolder(
path.join(app.getPath("userData"), '.ledfx', 'config.json')
path.join(app.getPath('userData'), '.ledfx', 'config.json')
)
shell.showItemInFolder(
path.join(app.getPath("appData"), '.ledfx', 'config.json')
path.join(app.getPath('appData'), '.ledfx', 'config.json')
)
shell.showItemInFolder(
path.join(app.getPath('home'), '.ledfx', 'config.json')
Expand All @@ -137,4 +165,3 @@ export const handlers = async (wind, subprocesses, event, parameters) => {
console.error(`Error handling command "${parameters.command}": ${error}`)
}
}

98 changes: 69 additions & 29 deletions src/app/app/instances.js → src/app/app/instances.mts
Original file line number Diff line number Diff line change
@@ -1,51 +1,87 @@
import { BrowserWindow } from 'electron'
import coreParams from './utils/coreParams.mjs'
import startCore from './utils/startCore.mjs'
import { ChildProcessWithoutNullStreams } from 'child_process'

export const poll = async (wind, subprocesses, name, p) => {
export interface Subprocess extends ChildProcessWithoutNullStreams {
running?: boolean
}

export interface Subprocesses {
[key: string]: Subprocess
}

export type IPlatform = 'darwin' | 'win32' | 'linux'

export function kills(subprocess: Subprocess | null): void {
if (subprocess !== null) {
subprocess.kill('SIGINT')
}
}

export const poll = async (
wind: BrowserWindow,
subprocesses: Subprocesses,
name: string,
p: string
) => {
console.log('Polling core', name, 'on port', p)
if (!p) return
try {
const response = await fetch(`http://127.0.0.1:${p}/api/info`)
const data = await response.json()
await response.json()
sendStatus(wind, subprocesses, true, name)
console.log('Polling core succeeded')
} catch (err) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (e) {
console.log('Polling core ...')
setTimeout(() => poll(wind, subprocesses, name, p), 1000)
}
}

export function stopInstance(wind, name, subprocesses) {
export function stopInstance(
wind: BrowserWindow,
name: string,
subprocesses: Subprocesses
) {
if (subprocesses[name]) {
subprocesses[name].running = false
sendStatus(wind, subprocesses, false, name)
subprocesses[name].kill()
}
}

export function startInstance(wind, name, subprocesses, port) {
export function startInstance(
wind: BrowserWindow,
name: string,
subprocesses: Subprocesses,
port: string = '8889'
) {
try {
let subpy = startCore(wind, process.platform, name, port)
const subpy = startCore(wind, process.platform as IPlatform, name, port)
if (subpy !== null) {
subprocesses[name] = subpy
subprocesses[name].running = true
sendStatus(wind, subprocesses, false, name)
poll(wind, subprocesses, name, port)
if (!subpy) {
console.error('Error starting subprocess')
return
}
subpy.on('exit', () => {
if (subprocesses[name]) {
subprocesses[name].running = false
}
if (wind && wind.webContents && !wind.isDestroyed() && subprocesses) {
// `subprocesses` is defined, proceed with calling `sendStatus`
try {
sendStatus(wind, subprocesses, false, name);
sendStatus(wind, subprocesses, false, name)
} catch (error) {
console.error(error);
console.error(error)
}
} else {
// `subprocesses` is not defined, handle this case as needed
console.error('subprocesses is not defined');
console.error('subprocesses is not defined')
}
})
subpy.on('error', () => {
Expand All @@ -60,28 +96,33 @@ export function startInstance(wind, name, subprocesses, port) {
}
}

export function sendStatus(wind, subprocesses, connected = false, n) {
let status = {}
let platformParams = coreParams[process.platform]
export function sendStatus(
wind: BrowserWindow,
subprocesses: Subprocesses,
connected = false,
n: string
) {
const status: { [key: string]: string } = {}
const platformParams = coreParams[process.platform]
// Check if `wind` is an instance of `BrowserWindow`
if (!(wind instanceof BrowserWindow)) {
console.error('wind is not an instance of BrowserWindow');
return;
console.error('wind is not an instance of BrowserWindow')
return
}

// Check if `subprocesses` is defined
if (!subprocesses) {
console.error('subprocesses is not defined');
return;
console.error('subprocesses is not defined')
return
}

// Check if `n` is defined
if (!n) {
console.error('n is not defined');
return;
console.error('n is not defined')
return
}

for (let name in platformParams) {
for (const name in platformParams) {
if (subprocesses && subprocesses[name]) {
if (name === n) {
status[name] = connected
Expand All @@ -96,22 +137,21 @@ export function sendStatus(wind, subprocesses, connected = false, n) {
status[name] = 'stopped'
}
}
if (wind && wind.webContents && !wind.isDestroyed() && status) wind.webContents.send('fromMain', ['status', status])
}

export function kills(subprocess) {
if (subprocess !== null) {
subprocess.kill('SIGINT')
}
if (wind && wind.webContents && !wind.isDestroyed() && status)
wind.webContents.send('fromMain', ['status', status])
}

export function closeAllSubs(wind, subpy, subprocesses) {
if (wind && wind.webContents && !wind.isDestroyed()) wind.webContents.send('fromMain', 'shutdown')
export function closeAllSubs(
wind: BrowserWindow,
subpy: Subprocess,
subprocesses: Subprocesses
) {
if (wind && wind.webContents && !wind.isDestroyed())
wind.webContents.send('fromMain', 'shutdown')
if (subpy !== null) kills(subpy)
if (subprocesses && Object.keys(subprocesses).length > 0) {
Object.values(subprocesses).forEach((sub) => {
if (sub) kills(sub)
})
}
}

17 changes: 14 additions & 3 deletions src/app/app/media.js → src/app/app/media.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
const { execFile } = require('child_process');
const path = require('path');
import { fileURLToPath } from 'node:url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const exePath = path.join(__dirname, 'path_to_dist_folder', 'media.exe');

execFile(exePath, (error, stdout, stderr) => {
interface MediaInfo {
title: string;
artist: string;
album: string;
error?: string;
}

execFile(exePath, (error: Error | null, stdout: string, stderr: string) => {
if (error) {
console.error(`Error: ${error.message}`);
return;
Expand All @@ -13,7 +24,7 @@ execFile(exePath, (error, stdout, stderr) => {
return;
}
try {
const mediaInfo = JSON.parse(stdout);
const mediaInfo: MediaInfo = JSON.parse(stdout);
if (mediaInfo.error) {
console.log(mediaInfo.error);
} else {
Expand All @@ -22,6 +33,6 @@ execFile(exePath, (error, stdout, stderr) => {
console.log(`Album: ${mediaInfo.album}`);
}
} catch (parseError) {
console.error(`JSON Parse Error: ${parseError.message}`);
console.error(`JSON Parse Error: ${(parseError as Error).message}`);
}
});
Loading

0 comments on commit d79c320

Please sign in to comment.