From 91a5330946a27cf842127e9cfdf7c52ecab568f1 Mon Sep 17 00:00:00 2001 From: nathansenn Date: Mon, 21 Aug 2023 21:44:55 +0800 Subject: [PATCH] Refactored PoAInstaller class to handle different platforms and added new properties and methods to PoAViewContent component. --- .gitignore | 3 +- src/main/AutoUpdaterPoA.ts | 13 ++++-- src/main/core/components/ProgramRunner.ts | 25 ++++++------ src/renderer/views/PoAView.tsx | 8 +++- src/renderer/views/PoAView/PoAViewContent.tsx | 40 ++++++++++++++++++- .../views/PoAView/usePoAProgramRunner.ts | 21 ++++++++-- 6 files changed, 86 insertions(+), 24 deletions(-) diff --git a/.gitignore b/.gitignore index f423183..b8b703c 100644 --- a/.gitignore +++ b/.gitignore @@ -17,4 +17,5 @@ build/ .env -temp \ No newline at end of file +temp +.aider* diff --git a/src/main/AutoUpdaterPoA.ts b/src/main/AutoUpdaterPoA.ts index 4ca166d..85df94d 100644 --- a/src/main/AutoUpdaterPoA.ts +++ b/src/main/AutoUpdaterPoA.ts @@ -52,7 +52,7 @@ class PoAInstaller extends EventEmitter { return null; } } - async install() { + async install() { const installDir = Path.join(os.homedir(), (await this.getDefaultPath()) || ''); console.log(`Installing PoA to ${installDir}`); if (!fs.existsSync(installDir)) { @@ -69,7 +69,14 @@ class PoAInstaller extends EventEmitter { if (compareVersions.compare(tag_name, currentVersion, '>')) { console.log('Update available'); this.emit('update-available', tag_name); - const asset = assets.find((a) => a.name.includes('win-main') && a.name.includes('exe') && isWin); + + let asset; + + if (isWin) { + asset = assets.find((a) => a.name.includes('win-main') && a.name.includes('exe')); + } else if (process.platform === 'linux') { + asset = assets.find((a) => a.name.includes(`linux-main-${tag_name}`)); // Modified this line + } if (!asset) { console.error('Could not find PoA asset for this platform'); @@ -84,7 +91,7 @@ class PoAInstaller extends EventEmitter { responseType: 'arraybuffer', }); - const installPath = Path.join(installDir, 'PoA.exe'); + const installPath = isWin ? Path.join(installDir, 'PoA.exe') : Path.join(installDir, 'PoA'); fs.writeFileSync(installPath, Buffer.from(response.data)); diff --git a/src/main/core/components/ProgramRunner.ts b/src/main/core/components/ProgramRunner.ts index 82891de..a62542a 100644 --- a/src/main/core/components/ProgramRunner.ts +++ b/src/main/core/components/ProgramRunner.ts @@ -1,5 +1,5 @@ // ProgramRunner.ts -import { exec, ChildProcess } from 'child_process'; +import { spawn, SpawnOptions, ChildProcess } from 'child_process'; import treeKill from 'tree-kill'; class ProgramRunner { @@ -15,23 +15,24 @@ class ProgramRunner { public setupProgram(onExit: () => void): void { console.log(`Setting up command: ${this.command}`); - this.process = exec(this.command, (error, stdout, stderr) => { - if (error && this.process && !this.process.killed) { - console.error(`Error running program: ${error.message}`); - console.error('Error details:', error); - return; - } + const commandParts = this.command.split(' '); + const cmd = commandParts[0]; + const args = commandParts.slice(1); - console.log(`stdout: ${stdout}`); - console.error(`stderr: ${stderr}`); - }); + const options: SpawnOptions = { + stdio: 'pipe', + detached: true, // This might help in some scenarios to open the program in a new process group. + shell: true // Running in a shell can sometimes help with GUI apps. + }; + + this.process = spawn(cmd, args, options); this.process.stdout.on('data', (data) => { - this.outputHandler(data); + this.outputHandler(data.toString()); }); this.process.stderr.on('data', (data) => { - this.outputHandler(data); + this.outputHandler(data.toString()); }); this.process.on('exit', () => { diff --git a/src/renderer/views/PoAView.tsx b/src/renderer/views/PoAView.tsx index 51ac5da..d8f03e8 100644 --- a/src/renderer/views/PoAView.tsx +++ b/src/renderer/views/PoAView.tsx @@ -18,7 +18,7 @@ export function PoAView() { const { programRunner, setProgramRunner, terminalRef } = usePoAProgramRunnerContext(); const updater = usePoAInstaller(); - const { terminal, setTerminal, isPoARunning, runPoA, contextValue } = usePoAProgramRunner(); + const { terminal, setTerminal, isPoARunning, runPoA, contextValue, storageSize, autoPin, setAutoPin, setStorageSize } = usePoAProgramRunner(); const { stopPoA } = contextValue; useEffect(() => { if (terminalRef.current && !terminal) { @@ -40,8 +40,12 @@ export function PoAView() { isPoARunning={isPoARunning} updatePoA={updater.updatePoA} runPoA={runPoA} - stopPoA={stopPoA} // Pass the stopPoA function as a prop + stopPoA={stopPoA} terminalRef={terminalRef} + storageSize={storageSize} + autoPin={autoPin} + setStorageSize={setStorageSize} + setAutoPin={setAutoPin} /> ); } diff --git a/src/renderer/views/PoAView/PoAViewContent.tsx b/src/renderer/views/PoAView/PoAViewContent.tsx index 18355e9..80f95a6 100644 --- a/src/renderer/views/PoAView/PoAViewContent.tsx +++ b/src/renderer/views/PoAView/PoAViewContent.tsx @@ -1,5 +1,6 @@ +//Path: src\renderer\views\PoAView\PoAViewContent.tsx import React, { useEffect, useRef, useState, RefObject } from 'react'; -import { Button, OverlayTrigger, Tooltip, Card } from 'react-bootstrap'; +import { Form, Button, OverlayTrigger, Tooltip, Card } from 'react-bootstrap'; import { IoIosRadioButtonOn } from 'react-icons/io'; import { usePoAState } from './PoAStateContext'; import { usePoAProgramRunner } from './usePoAProgramRunner'; @@ -7,9 +8,15 @@ import { usePoAProgramRunner } from './usePoAProgramRunner'; interface PoAViewContentProps { isPoARunning: boolean; updatePoA: () => Promise; - runPoA: () => void; + runPoA: () => Promise; stopPoA: () => void; terminalRef: RefObject; + + // Add the new properties + storageSize: number; + autoPin: boolean; + setStorageSize: React.Dispatch>; + setAutoPin: React.Dispatch>; } export const PoAViewContent: React.FC = ({ @@ -17,6 +24,10 @@ export const PoAViewContent: React.FC = ({ updatePoA, runPoA, terminalRef, + autoPin, + storageSize, + setAutoPin, + setStorageSize, }) => { const { logs, validatorOnline, setValidatorOnline } = usePoAState(); const [isDataReceived, setIsDataReceived] = useState(false); @@ -97,6 +108,14 @@ export const PoAViewContent: React.FC = ({ } }, [logs, terminalRef]); + + const handleAutoPinChange = (e: React.ChangeEvent) => { + setAutoPin(e.target.checked); + }; + + const handleStorageSizeChange = (e: React.ChangeEvent) => { + setStorageSize(Number(e.target.value)); + }; const startPoA = async () => { if (!isPoARunning && !isDataReceived && !isUpdating) { console.log('starting PoA'); @@ -149,6 +168,23 @@ export const PoAViewContent: React.FC = ({ + +

Enter storage size in GB to auto pin

+

SPK Network Stats

diff --git a/src/renderer/views/PoAView/usePoAProgramRunner.ts b/src/renderer/views/PoAView/usePoAProgramRunner.ts index 2e00f16..03ac81d 100644 --- a/src/renderer/views/PoAView/usePoAProgramRunner.ts +++ b/src/renderer/views/PoAView/usePoAProgramRunner.ts @@ -9,6 +9,7 @@ import os from 'os'; import ArraySearch from 'arraysearch'; import PromiseIPC from 'electron-promise-ipc'; +const isWin = process.platform === 'win32'; export const usePoAProgramRunner = () => { const [terminal, setTerminal] = useState(null); const [isPoARunning, setIsPoARunning] = useState(false); @@ -17,6 +18,8 @@ export const usePoAProgramRunner = () => { const Finder = ArraySearch.Finder; const { logs, setLogs } = usePoAState(); const isMountedRef = useRef(true); + const [autoPin, setAutoPin] = useState(false); + const [storageSize, setStorageSize] = useState(0); const runPoA = useCallback(async () => { let command = ''; // Define command here @@ -25,8 +28,18 @@ export const usePoAProgramRunner = () => { const getAccount = (await PromiseIPC.send('accounts.get', profileID as any)) as any; const hiveInfo = Finder.one.in(getAccount.keyring).with({ type: 'hive' }); const installDir = Path.join(os.homedir(), (await poaInstaller.current.getDefaultPath()) || ''); - const executablePath = Path.join(installDir, 'PoA.exe'); - command = `"${executablePath}" -node=2 -username=${hiveInfo.username} -useWS=true -IPFS_PORT=5004`; // Assign command here + const executablePath = isWin ? Path.join(installDir, 'PoA.exe') : Path.join(installDir, 'PoA'); + command = `"${executablePath}" -node=2 -username=${hiveInfo.username} -useWS=true -IPFS_PORT=5004`; + console.log(command); + console.log(autoPin); + console.log(storageSize); + if (autoPin) { + command += " -getVids=true -pinVideos=true"; + if (storageSize > 0) { + command += ` -storageLimit=${storageSize}`; + } + } + console.log(command); if (!runner.current) { runner.current = new ProgramRunner(command, (data: string) => { if (!isMountedRef.current) return; @@ -45,7 +58,7 @@ export const usePoAProgramRunner = () => { runner.current.stopProgram(); setIsPoARunning(false); } - }, [terminal, isPoARunning]); + }, [terminal, isPoARunning, autoPin, storageSize]); const contextValue = { isPoARunning, @@ -54,5 +67,5 @@ export const usePoAProgramRunner = () => { stopPoA: () => runner.current?.stopProgram(), }; - return { terminal, setTerminal, isPoARunning, runPoA, contextValue }; + return { terminal, setTerminal, isPoARunning, runPoA, contextValue, setAutoPin, setStorageSize, storageSize, autoPin }; }