Skip to content

Commit

Permalink
refactor renterd, do not show window on startup if configured
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Jan 25, 2024
1 parent 6511858 commit a9ea86a
Show file tree
Hide file tree
Showing 13 changed files with 275 additions and 241 deletions.
2 changes: 1 addition & 1 deletion hostd/electron-src/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function getBinaryZipStagingPath(): string {
return path.join(getTempDownloadsPath(), binaryName + '.zip')
}

function releaseAsset() {
function releaseAsset(): string {
let goos
switch (process.platform) {
case 'win32':
Expand Down
18 changes: 3 additions & 15 deletions hostd/electron-src/index.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,20 @@
import { app, globalShortcut } from 'electron'
import prepareNext from 'electron-next'
import { startDaemon, stopDaemon } from './daemon'
import { downloadRelease } from './download'
import { doesBinaryExist, getIsConfigured } from './config'
import { stopDaemon } from './daemon'
import { initTray } from './tray'
import { state } from './state'
import { initWindow } from './window'
import { initShortcuts } from './shortcuts'
import { initIpc } from './ipc'
import { startup } from './startup'

app.on('ready', async () => {
await prepareNext('./renderer')
initWindow()
initTray()
initShortcuts()
initIpc()

const needsInitialDownload = !doesBinaryExist()
if (needsInitialDownload) {
await downloadRelease()
}

// If the app is already configured, start the daemon and open browser
// and do not show the configuration window.
if (getIsConfigured()) {
startDaemon()
state.mainWindow?.close()
}
startup()
})

app.on('before-quit', async () => {
Expand Down
12 changes: 12 additions & 0 deletions hostd/electron-src/startup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { startDaemon } from './daemon'
import { getIsConfigured } from './config'
import { state } from './state'

export function startup() {
// If the app is already configured, start the daemon and open browser
// and do not show the configuration window.
if (getIsConfigured()) {
startDaemon()
state.mainWindow?.close()
}
}
Empty file removed hostd/electron-src/utils.ts
Empty file.
37 changes: 26 additions & 11 deletions renterd/electron-src/daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { spawn } from 'child_process'
import { state } from './state'
import { getBinaryFilePath, getConfig, getConfigFilePath } from './config'
import axios from 'axios'
import { Octokit } from '@octokit/rest'

export function startDaemon(): Promise<void> {
return new Promise(async (resolve, reject) => {
Expand All @@ -10,52 +11,52 @@ export function startDaemon(): Promise<void> {
try {
const config = getConfig()
const binaryFilePath = getBinaryFilePath()
state.process = spawn(binaryFilePath, [], {
state.daemon = spawn(binaryFilePath, [], {
env: { ...process.env, RENTERD_CONFIG_FILE: getConfigFilePath() },
cwd: config.directory,
})

state.process.stdout?.on('data', (data) => {
state.daemon.stdout?.on('data', (data) => {
console.log(`stdout: ${data}`)
// Emit events or log data as needed
})

state.process.stderr?.on('data', (data) => {
state.daemon.stderr?.on('data', (data) => {
console.error(`stderr: ${data}`)
// Emit events or log data as needed
})

state.process.on('close', (code) => {
state.daemon.on('close', (code) => {
console.log(`child process exited with code ${code}`)
state.process = null
state.daemon = null
})

resolve()
} catch (err) {
state.process = null
state.daemon = null
reject(err)
}
})
}

export function stopDaemon(): Promise<void> {
return new Promise((resolve) => {
if (!state.process) {
if (!state.daemon) {
resolve()
return
}

state.process.on('close', () => {
state.process = null
state.daemon.on('close', () => {
state.daemon = null
resolve()
})

state.process.kill('SIGINT')
state.daemon.kill('SIGINT')
})
}

export function getIsDaemonRunning(): boolean {
return !!state.process && !state.process.killed
return !!state.daemon && !state.daemon.killed
}

export async function getInstalledVersion(): Promise<string> {
Expand Down Expand Up @@ -85,3 +86,17 @@ export async function getInstalledVersion(): Promise<string> {
return ''
}
}

export async function getLatestVersion(): Promise<string> {
try {
const octokit = new Octokit()
const response = await octokit.repos.getLatestRelease({
owner: 'SiaFoundation',
repo: 'renterd',
})
return response.data.tag_name
} catch (err) {
console.error(err)
return ''
}
}
49 changes: 17 additions & 32 deletions renterd/electron-src/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,7 @@ import { getBinaryFilePath, getConfigAndBinaryDirectoryPath } from './config'
import { promisify } from 'util'
import stream from 'stream'
import axios from 'axios'

export async function getLatestVersion(): Promise<string> {
try {
const octokit = new Octokit()
const response = await octokit.repos.getLatestRelease({
owner: 'SiaFoundation',
repo: 'renterd',
})
return response.data.tag_name
} catch (err) {
console.error(err)
return ''
}
}
import { system } from './state'

export async function downloadRelease(): Promise<void> {
try {
Expand Down Expand Up @@ -69,7 +56,7 @@ async function extractBinary(): Promise<void> {
const zip = new admZip(zipFilePath)
zip.extractAllTo(extractDir, true)

const binaryName = process.platform === 'win32' ? 'renterd.exe' : 'renterd'
const binaryName = system.isWindows ? 'renterd.exe' : 'renterd'
const extractedBinaryPath = path.join(extractDir, binaryName)
const finalBinaryPath = getBinaryFilePath()

Expand Down Expand Up @@ -111,28 +98,26 @@ function releaseAsset(): string {
case 'linux':
goos = 'linux'
break
// Add additional mappings as needed
default:
throw new Error(`Unsupported platform: ${process.platform}`)
}

let goarch
switch (process.arch) {
case 'x64':
goarch = 'amd64'
break
case 'ia32':
goarch = '386'
break
case 'arm':
goarch = 'arm'
break
case 'arm64':
goarch = 'arm64'
break
// Add additional mappings as needed
default:
throw new Error(`Unsupported architecture: ${process.arch}`)
if (process.platform === 'win32') {
// Windows only supports amd64
goarch = 'amd64'
} else {
// For Darwin and Linux, consider both amd64 and arm64
switch (process.arch) {
case 'x64':
goarch = 'amd64'
break
case 'arm64':
goarch = 'arm64'
break
default:
throw new Error(`Unsupported architecture: ${process.arch}`)
}
}

return `renterd_${goos}_${goarch}.zip`
Expand Down
Loading

0 comments on commit a9ea86a

Please sign in to comment.