monerod-gui/app/main.ts

161 lines
4.9 KiB
TypeScript
Raw Normal View History

2024-09-22 13:55:03 +00:00
import {app, BrowserWindow, ipcMain, screen} from 'electron';
import { ChildProcess, ChildProcessWithoutNullStreams, exec, spawn } from 'child_process';
2024-09-17 18:33:24 +00:00
import * as path from 'path';
import * as fs from 'fs';
2024-09-22 13:55:03 +00:00
const monerodFilePath: string = "/home/sidney/Documenti/monero-x86_64-linux-gnu-v0.18.3.4/monerod";
2024-09-17 18:33:24 +00:00
let win: BrowserWindow | null = null;
const args = process.argv.slice(1),
serve = args.some(val => val === '--serve');
function createWindow(): BrowserWindow {
const size = screen.getPrimaryDisplay().workAreaSize;
// Create the browser window.
win = new BrowserWindow({
x: 0,
y: 0,
width: size.width,
height: size.height,
webPreferences: {
nodeIntegration: true,
allowRunningInsecureContent: (serve),
2024-09-22 13:55:03 +00:00
contextIsolation: false
2024-09-17 18:33:24 +00:00
},
});
if (serve) {
const debug = require('electron-debug');
debug();
require('electron-reloader')(module);
win.loadURL('http://localhost:4200');
} else {
// Path when running electron executable
let pathIndex = './index.html';
if (fs.existsSync(path.join(__dirname, '../dist/index.html'))) {
// Path when running electron in local folder
pathIndex = '../dist/index.html';
}
const url = new URL(path.join('file:', __dirname, pathIndex));
win.loadURL(url.href);
}
// Emitted when the window is closed.
win.on('closed', () => {
// Dereference the window object, usually you would store window
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
win = null;
});
return win;
}
2024-09-22 13:55:03 +00:00
function execMoneroDaemon(configFilePath: string): ChildProcess {
const monerodPath = path.resolve(__dirname, 'path/to/monerod'); // Percorso del binario di monerod
//const command = `"${monerodPath}" --config-file "${configFilePath}"`;
const command = `/home/sidney/Documenti/monero-x86_64-linux-gnu-v0.18.3.4/monerod --testnet --fast-block-sync 1 --prune-blockchain --sync-pruned-blocks --confirm-external-bind --max-concurrency 1 --log-level 1 --rpc-access-control-origins=*`;
const monerodProcess = exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`Errore durante l'avvio di monerod: ${error.message}`);
return;
}
if (stderr) {
console.error(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
});
// Gestisci l'output in tempo reale
if (monerodProcess.stdout == null) {
throw new Error("No stdout for monero process")
}
if (monerodProcess.stderr == null) {
throw new Error("No stderr for monero process");
}
monerodProcess.stdout.on('data', (data) => {
console.log(`monerod stdout: ${data}`);
});
monerodProcess.stderr.on('data', (data) => {
console.error(`monerod stderr: ${data}`);
});
return monerodProcess;
}
2024-09-25 21:24:46 +00:00
function startMoneroDaemon(commandOptions: string[], logHandler?: (message: string) => void): ChildProcessWithoutNullStreams {
2024-09-22 13:55:03 +00:00
const monerodPath = path.resolve(__dirname, monerodFilePath);
2024-09-24 20:54:48 +00:00
console.log("Starting monerod daemon with options: " + commandOptions.join(" "));
2024-09-22 13:55:03 +00:00
// Avvia il processo usando spawn
2024-09-24 20:54:48 +00:00
const monerodProcess = spawn(monerodPath, commandOptions);
2024-09-22 13:55:03 +00:00
// Gestisci l'output di stdout in streaming
monerodProcess.stdout.on('data', (data) => {
console.log(`monerod stdout: ${data}`);
2024-09-25 21:24:46 +00:00
win?.webContents.send('monero-stdout', `${data}`);
2024-09-22 13:55:03 +00:00
// Puoi anche inviare i log all'interfaccia utente tramite IPC
});
// Gestisci gli errori in stderr
monerodProcess.stderr.on('data', (data) => {
console.error(`monerod stderr: ${data}`);
2024-09-25 21:24:46 +00:00
win?.webContents.send('monero-stderr', `${data}`);
2024-09-22 13:55:03 +00:00
});
// Gestisci la chiusura del processo
monerodProcess.on('close', (code) => {
console.log(`monerod chiuso con codice: ${code}`);
2024-09-25 21:24:46 +00:00
win?.webContents.send('monero-stdout', `monerod exited with code: ${code}`);
2024-09-22 13:55:03 +00:00
});
return monerodProcess;
}
2024-09-17 18:33:24 +00:00
try {
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
// Added 400 ms to fix the black background issue while using transparent window. More detais at https://github.com/electron/electron/issues/15947
app.on('ready', () => setTimeout(createWindow, 400));
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (win === null) {
createWindow();
}
});
2024-09-25 21:24:46 +00:00
ipcMain.on('start-monerod', (event, configFilePath: string[], logHandler?: (message: string) => void) => {
startMoneroDaemon(configFilePath, logHandler);
2024-09-22 13:55:03 +00:00
})
2024-09-17 18:33:24 +00:00
} catch (e) {
// Catch Error
// throw e;
}