diff --git a/.gitignore b/.gitignore index b136339..ec76903 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,7 @@ dist out -bin node_modules .next .DS_Store -main .env +bin diff --git a/hostd/electron-src/asset.ts b/hostd/main/asset.ts similarity index 74% rename from hostd/electron-src/asset.ts rename to hostd/main/asset.ts index 1c5b92d..1b192f4 100644 --- a/hostd/electron-src/asset.ts +++ b/hostd/main/asset.ts @@ -1,8 +1,8 @@ import path from 'path' -import { system } from './state' +import { env } from './env' export function getAsset(name: string) { - return system.isDev + return env.isDev ? path.join(process.cwd(), 'assets', name) : path.join(__dirname, '../assets', name) } diff --git a/hostd/main/binary.ts b/hostd/main/binary.ts new file mode 100644 index 0000000..34bcb5b --- /dev/null +++ b/hostd/main/binary.ts @@ -0,0 +1,10 @@ +import * as path from 'path' + +export function getBinaryDirectoryPath(): string { + return path.join(__dirname, '../bin') +} + +export function getBinaryFilePath(): string { + const binaryName = process.platform === 'win32' ? 'hostd.exe' : 'hostd' + return path.join(getBinaryDirectoryPath(), binaryName) +} diff --git a/hostd/electron-src/config.ts b/hostd/main/config.ts similarity index 79% rename from hostd/electron-src/config.ts rename to hostd/main/config.ts index 3a4d914..a406905 100644 --- a/hostd/electron-src/config.ts +++ b/hostd/main/config.ts @@ -76,7 +76,7 @@ export async function saveConfig(config: Config): Promise { } await fs.promises.mkdir(config.directory, { recursive: true }) - await fs.promises.mkdir(getConfigAndBinaryDirectoryPath(), { + await fs.promises.mkdir(getConfigDirectoryPath(), { recursive: true, }) @@ -98,24 +98,14 @@ export function getConfig(): Config { } } -export function getConfigAndBinaryDirectoryPath(): string { +export function getConfigDirectoryPath(): string { return path.join(app.getPath('userData'), 'data') } export function getConfigFilePath(): string { - return path.join(getConfigAndBinaryDirectoryPath(), 'config.yaml') -} - -export function getBinaryFilePath(): string { - const binaryName = process.platform === 'win32' ? 'hostd.exe' : 'hostd' - return path.join(getConfigAndBinaryDirectoryPath(), 'bin', binaryName) -} - -export function doesBinaryExist() { - const binaryFilePath = getBinaryFilePath() - return fs.existsSync(binaryFilePath) + return path.join(getConfigDirectoryPath(), 'config.yaml') } export function getDefaultDataPath(): string { - return getConfigAndBinaryDirectoryPath() + return getConfigDirectoryPath() } diff --git a/hostd/electron-src/daemon.ts b/hostd/main/daemon.ts similarity index 83% rename from hostd/electron-src/daemon.ts rename to hostd/main/daemon.ts index a6ae1ed..21c3d77 100644 --- a/hostd/electron-src/daemon.ts +++ b/hostd/main/daemon.ts @@ -1,8 +1,8 @@ import { spawn } from 'child_process' import { state } from './state' -import { getBinaryFilePath, getConfig, getConfigFilePath } from './config' +import { getConfig, getConfigFilePath } from './config' import axios from 'axios' -import { Octokit } from '@octokit/rest' +import { getBinaryFilePath } from './binary' export function startDaemon(): Promise { return new Promise(async (resolve, reject) => { @@ -88,17 +88,3 @@ export async function getInstalledVersion(): Promise { return '' } } - -export async function getLatestVersion(): Promise { - try { - const octokit = new Octokit() - const response = await octokit.repos.getLatestRelease({ - owner: 'SiaFoundation', - repo: 'hostd', - }) - return response.data.tag_name - } catch (err) { - console.error(err) - return '' - } -} diff --git a/hostd/electron-src/download.ts b/hostd/main/download.ts similarity index 80% rename from hostd/electron-src/download.ts rename to hostd/main/download.ts index d9bb7ff..1d6b60a 100644 --- a/hostd/electron-src/download.ts +++ b/hostd/main/download.ts @@ -2,11 +2,27 @@ import fs from 'fs' import path from 'path' import { Octokit } from '@octokit/rest' import admZip from 'adm-zip' -import { getBinaryFilePath, getConfigAndBinaryDirectoryPath } from './config' import { promisify } from 'util' import stream from 'stream' import axios from 'axios' -import { system } from './state' +import { system } from './system' +import { getBinaryDirectoryPath, getBinaryFilePath } from './binary' + +downloadRelease() + +// export async function getLatestVersion(): Promise { +// try { +// const octokit = new Octokit() +// const response = await octokit.repos.getLatestRelease({ +// owner: 'SiaFoundation', +// repo: 'hostd', +// }) +// return response.data.tag_name +// } catch (err) { +// console.error(err) +// return '' +// } +// } export async function downloadRelease(): Promise { try { @@ -24,7 +40,10 @@ export async function downloadRelease(): Promise { console.log('Release name:', release.name) console.log('Release tag:', release.tag_name) await downloadFile(asset.browser_download_url) - extractBinary() // Assuming binary is zipped + await extractBinary() + // write a file called version into the bin directory with the release tag + const versionFilePath = path.join(getBinaryDirectoryPath(), 'version') + await fs.promises.writeFile(versionFilePath, release.tag_name) } else { throw new Error('Failed to find release asset') } @@ -79,7 +98,7 @@ async function extractBinary(): Promise { } function getTempDownloadsPath(): string { - return path.join(getConfigAndBinaryDirectoryPath(), 'download') + return path.join(getBinaryDirectoryPath(), 'download') } function getBinaryZipStagingPath(): string { diff --git a/hostd/main/env.ts b/hostd/main/env.ts new file mode 100644 index 0000000..ecd2042 --- /dev/null +++ b/hostd/main/env.ts @@ -0,0 +1,3 @@ +import isDev from 'electron-is-dev' + +export const env = { isDev } diff --git a/hostd/electron-src/index.ts b/hostd/main/index.ts similarity index 100% rename from hostd/electron-src/index.ts rename to hostd/main/index.ts diff --git a/hostd/electron-src/ipc.ts b/hostd/main/ipc.ts similarity index 85% rename from hostd/electron-src/ipc.ts rename to hostd/main/ipc.ts index c4fe045..a7ad204 100644 --- a/hostd/electron-src/ipc.ts +++ b/hostd/main/ipc.ts @@ -3,7 +3,6 @@ import { ipcMain, shell } from 'electron' import { getInstalledVersion, getIsDaemonRunning, - getLatestVersion, startDaemon, stopDaemon, } from './daemon' @@ -14,7 +13,6 @@ import { getIsConfigured, saveConfig, } from './config' -import { downloadRelease } from './download' import { closeWindow } from './window' export function initIpc() { @@ -33,9 +31,6 @@ export function initIpc() { ipcMain.handle('daemon-is-running', (_) => { return getIsDaemonRunning() }) - ipcMain.handle('daemon-update', async (_) => { - await downloadRelease() - }) ipcMain.handle('config-get', (_) => { const config = getConfig() return config @@ -54,9 +49,6 @@ export function initIpc() { ipcMain.handle('get-installed-version', (_) => { return getInstalledVersion() }) - ipcMain.handle('get-latest-version', (_) => { - return getLatestVersion() - }) ipcMain.handle('config-save', async (_, config: Config) => { await saveConfig(config) return true diff --git a/hostd/electron-src/next.ts b/hostd/main/next.ts similarity index 100% rename from hostd/electron-src/next.ts rename to hostd/main/next.ts diff --git a/hostd/electron-src/preload.ts b/hostd/main/preload.ts similarity index 100% rename from hostd/electron-src/preload.ts rename to hostd/main/preload.ts diff --git a/hostd/electron-src/shortcuts.ts b/hostd/main/shortcuts.ts similarity index 88% rename from hostd/electron-src/shortcuts.ts rename to hostd/main/shortcuts.ts index d84900a..ebb4e24 100644 --- a/hostd/electron-src/shortcuts.ts +++ b/hostd/main/shortcuts.ts @@ -1,5 +1,6 @@ import { globalShortcut } from 'electron' -import { state, system } from './state' +import { state } from './state' +import { system } from './system' export function initShortcuts() { // Register a global shortcut listener for the developer tools diff --git a/hostd/electron-src/startup.ts b/hostd/main/startup.ts similarity index 85% rename from hostd/electron-src/startup.ts rename to hostd/main/startup.ts index df6a892..76de1a9 100644 --- a/hostd/electron-src/startup.ts +++ b/hostd/main/startup.ts @@ -1,6 +1,7 @@ import { startDaemon } from './daemon' import { getIsConfigured } from './config' -import { state, system } from './state' +import { state } from './state' +import { env } from './env' export function startup() { // If the app is already configured, start the daemon and open browser @@ -10,7 +11,7 @@ export function startup() { state.mainWindow?.close() } - if (system.isDev) { + if (env.isDev) { state.mainWindow?.setMaximumSize(2000, 2000) state.mainWindow?.setSize(1000, 800) state.mainWindow?.webContents.openDevTools() diff --git a/hostd/electron-src/state.ts b/hostd/main/state.ts similarity index 51% rename from hostd/electron-src/state.ts rename to hostd/main/state.ts index b27989c..444b1b2 100644 --- a/hostd/electron-src/state.ts +++ b/hostd/main/state.ts @@ -1,6 +1,5 @@ import { ChildProcess } from 'child_process' import { BrowserWindow, Tray } from 'electron' -import isDev from 'electron-is-dev' export let state: { mainWindow: BrowserWindow | null @@ -13,15 +12,3 @@ export let state: { isQuitting: false, daemon: null, } - -export const system: { - isDev: boolean - isDarwin: boolean - isLinux: boolean - isWindows: boolean -} = { - isDev, - isDarwin: process.platform === 'darwin', - isLinux: process.platform === 'linux', - isWindows: process.platform === 'win32', -} diff --git a/hostd/main/system.ts b/hostd/main/system.ts new file mode 100644 index 0000000..0e3b989 --- /dev/null +++ b/hostd/main/system.ts @@ -0,0 +1,9 @@ +export const system: { + isDarwin: boolean + isLinux: boolean + isWindows: boolean +} = { + isDarwin: process.platform === 'darwin', + isLinux: process.platform === 'linux', + isWindows: process.platform === 'win32', +} diff --git a/renterd/electron-src/tray.ts b/hostd/main/tray.ts similarity index 90% rename from renterd/electron-src/tray.ts rename to hostd/main/tray.ts index 1472d49..ea36999 100644 --- a/renterd/electron-src/tray.ts +++ b/hostd/main/tray.ts @@ -1,5 +1,6 @@ import { app, Tray, Menu } from 'electron' -import { state, system } from './state' +import { state } from './state' +import { system } from './system' import { getAsset } from './asset' export function initTray() { diff --git a/renterd/electron-src/tsconfig.json b/hostd/main/tsconfig.json similarity index 95% rename from renterd/electron-src/tsconfig.json rename to hostd/main/tsconfig.json index 06743f5..a569c53 100644 --- a/renterd/electron-src/tsconfig.json +++ b/hostd/main/tsconfig.json @@ -17,7 +17,7 @@ "skipLibCheck": true, "strict": true, "target": "esnext", - "outDir": "../main" + "outDir": "../dist/main" }, "exclude": ["node_modules"], "include": ["**/*.ts", "**/*.tsx", "**/*.js"] diff --git a/renterd/electron-src/window.ts b/hostd/main/window.ts similarity index 85% rename from renterd/electron-src/window.ts rename to hostd/main/window.ts index 81a5eb3..c58f38f 100644 --- a/renterd/electron-src/window.ts +++ b/hostd/main/window.ts @@ -1,8 +1,10 @@ import path, { join } from 'path' import { BrowserWindow, app } from 'electron' import { format } from 'url' -import { state, system } from './state' +import { state } from './state' +import { system } from './system' import { getAsset } from './asset' +import { env } from './env' export function initWindow() { state.mainWindow = new BrowserWindow({ @@ -25,10 +27,10 @@ export function initWindow() { // Hide the main window instead of closing it, to keep the app running in the tray state.mainWindow.on('close', closeWindow) - const url = system.isDev + const url = env.isDev ? 'http://localhost:8000/' : format({ - pathname: path.join(__dirname, '../renderer/out/index.html'), + pathname: path.join(__dirname, '../dist/renderer/index.html'), protocol: 'file:', slashes: true, }) diff --git a/hostd/package.json b/hostd/package.json index 4b73514..5a95b09 100644 --- a/hostd/package.json +++ b/hostd/package.json @@ -12,14 +12,15 @@ "type": "git", "url": "https://github.com/SiaFoundation/desktop.git" }, - "main": "main/index.js", + "main": "dist/main/index.js", "scripts": { - "clean": "rimraf out main renderer/out renderer/.next", + "clean": "rimraf out dist renderer/.next", "dev": "npm run build-electron && electron .", "build-renderer": "next build renderer", - "build-electron": "tsc -p electron-src", + "build-electron": "tsc -p main", + "download-daemon": "npm run build-electron && node main/download.js", "build": "npm run build-renderer && npm run build-electron", - "type-check": "tsc -p ./renderer/tsconfig.json && tsc -p ./electron-src/tsconfig.json", + "type-check": "tsc -p ./renderer/tsconfig.json && tsc -p ./main/tsconfig.json", "start": "electron-forge start", "package": "electron-forge package", "make": "electron-forge make", @@ -85,9 +86,9 @@ "icon": "assets/appicon.png", "asar": true, "files": [ - "main", - "renderer/out", - "assets" + "dist", + "assets", + "bin" ] } } diff --git a/hostd/renderer/components/App.tsx b/hostd/renderer/components/App.tsx index d399319..2ad6060 100644 --- a/hostd/renderer/components/App.tsx +++ b/hostd/renderer/components/App.tsx @@ -8,7 +8,6 @@ import { import { ConfigForm } from './ConfigForm' import { useConfig } from '../contexts/config' import { Header } from './Header' -import { UpdateBanner } from './UpdateBanner' export function App() { const { onSubmit } = useConfig() @@ -16,7 +15,6 @@ export function App() {
-
diff --git a/hostd/renderer/components/Header.tsx b/hostd/renderer/components/Header.tsx index 44c1c24..9171466 100644 --- a/hostd/renderer/components/Header.tsx +++ b/hostd/renderer/components/Header.tsx @@ -10,7 +10,6 @@ import { Launch16, Reset16 } from '@siafoundation/react-icons' import { useDaemon } from './useDaemon' import { useConfig } from '../contexts/config' import { useConfigData } from './useConfigData' -import { useLatestVersion } from './useLatestVersion' import { useInstalledVersion } from './useInstalledVersion' export function Header() { @@ -18,9 +17,7 @@ export function Header() { const { form, isConfigured, changeCount, revalidateAndResetForm } = useConfig() const config = useConfigData() - const latestVersion = useLatestVersion() const installedVersion = useInstalledVersion() - console.log(config.data) return ( { - try { - setIsUpdating(true) - await window.electron.daemonUpdate() - } catch (e) { - console.log(e) - triggerErrorToast('Error downloading update. Please try again.') - setIsUpdating(false) - setIsRestarting(false) - return - } - try { - setIsRestarting(true) - await window.electron.daemonStart() - } catch (e) { - console.log(e) - triggerErrorToast('Error restarting daemon. Please try again.') - setIsUpdating(false) - setIsRestarting(false) - return - } - triggerSuccessToast(`Updated to hostd version ${installedVersion.data}.`) - setIsUpdating(false) - setIsRestarting(false) - }, [setIsUpdating, installedVersion.data]) - - if (latestVersion.data === installedVersion.data) { - return null - } - - return ( -
- {isRestarting ? ( - <> - - - Restarting hostd daemon {latestVersion.data} - - - ) : isUpdating ? ( - <> - - - Updating to hostd {latestVersion.data} - - - ) : ( - <> - - - - - An update to hostd {latestVersion.data} is available. - - - )} -
- ) -} diff --git a/hostd/renderer/components/useLatestVersion.tsx b/hostd/renderer/components/useLatestVersion.tsx deleted file mode 100644 index 56cb13f..0000000 --- a/hostd/renderer/components/useLatestVersion.tsx +++ /dev/null @@ -1,17 +0,0 @@ -'use client' - -import { hoursInMilliseconds } from '@siafoundation/design-system' -import useSWR from 'swr' - -export function useLatestVersion() { - return useSWR( - 'latestVersion', - async () => { - return await window.electron.getLatestVersion() - }, - { - refreshInterval: hoursInMilliseconds(1), - revalidateOnFocus: false, - } - ) -} diff --git a/hostd/renderer/renderer.d.ts b/hostd/renderer/renderer.d.ts index 75d9bb1..d8971c1 100644 --- a/hostd/renderer/renderer.d.ts +++ b/hostd/renderer/renderer.d.ts @@ -6,13 +6,11 @@ export interface API { openBrowser: (url: string) => Promise daemonStart: () => Promise daemonStop: () => Promise - daemonUpdate: () => Promise getConfig: () => Promise openDataDirectory: () => Promise getIsConfigured: () => Promise getDefaultDataDirectory: () => Promise getInstalledVersion: () => Promise - getLatestVersion: () => Promise saveConfig: (config: Config) => Promise } diff --git a/renterd/electron-src/asset.ts b/renterd/main/asset.ts similarity index 74% rename from renterd/electron-src/asset.ts rename to renterd/main/asset.ts index 1c5b92d..1b192f4 100644 --- a/renterd/electron-src/asset.ts +++ b/renterd/main/asset.ts @@ -1,8 +1,8 @@ import path from 'path' -import { system } from './state' +import { env } from './env' export function getAsset(name: string) { - return system.isDev + return env.isDev ? path.join(process.cwd(), 'assets', name) : path.join(__dirname, '../assets', name) } diff --git a/renterd/main/binary.ts b/renterd/main/binary.ts new file mode 100644 index 0000000..db96b80 --- /dev/null +++ b/renterd/main/binary.ts @@ -0,0 +1,10 @@ +import * as path from 'path' + +export function getBinaryDirectoryPath(): string { + return path.join(__dirname, '../bin') +} + +export function getBinaryFilePath(): string { + const binaryName = process.platform === 'win32' ? 'renterd.exe' : 'renterd' + return path.join(getBinaryDirectoryPath(), binaryName) +} diff --git a/renterd/electron-src/config.ts b/renterd/main/config.ts similarity index 78% rename from renterd/electron-src/config.ts rename to renterd/main/config.ts index e44f9eb..6ffdc1f 100644 --- a/renterd/electron-src/config.ts +++ b/renterd/main/config.ts @@ -64,7 +64,7 @@ export async function saveConfig(config: Config): Promise { } await fs.promises.mkdir(config.directory, { recursive: true }) - await fs.promises.mkdir(getConfigAndBinaryDirectoryPath(), { + await fs.promises.mkdir(getConfigDirectoryPath(), { recursive: true, }) @@ -86,24 +86,14 @@ export function getConfig(): Config { } } -export function getConfigAndBinaryDirectoryPath(): string { +export function getConfigDirectoryPath(): string { return path.join(app.getPath('userData'), 'data') } export function getConfigFilePath(): string { - return path.join(getConfigAndBinaryDirectoryPath(), 'config.yaml') -} - -export function getBinaryFilePath(): string { - const binaryName = process.platform === 'win32' ? 'renterd.exe' : 'renterd' - return path.join(getConfigAndBinaryDirectoryPath(), 'bin', binaryName) -} - -export function doesBinaryExist() { - const binaryFilePath = getBinaryFilePath() - return fs.existsSync(binaryFilePath) + return path.join(getConfigDirectoryPath(), 'config.yaml') } export function getDefaultDataPath(): string { - return getConfigAndBinaryDirectoryPath() + return getConfigDirectoryPath() } diff --git a/renterd/electron-src/daemon.ts b/renterd/main/daemon.ts similarity index 82% rename from renterd/electron-src/daemon.ts rename to renterd/main/daemon.ts index 1c4e13c..bd29788 100644 --- a/renterd/electron-src/daemon.ts +++ b/renterd/main/daemon.ts @@ -1,8 +1,8 @@ import { spawn } from 'child_process' import { state } from './state' -import { getBinaryFilePath, getConfig, getConfigFilePath } from './config' +import { getConfig, getConfigFilePath } from './config' +import { getBinaryFilePath } from './binary' import axios from 'axios' -import { Octokit } from '@octokit/rest' export function startDaemon(): Promise { return new Promise(async (resolve, reject) => { @@ -86,17 +86,3 @@ export async function getInstalledVersion(): Promise { return '' } } - -export async function getLatestVersion(): Promise { - 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 '' - } -} diff --git a/renterd/electron-src/download.ts b/renterd/main/download.ts similarity index 79% rename from renterd/electron-src/download.ts rename to renterd/main/download.ts index 1c28afa..8ed18b8 100644 --- a/renterd/electron-src/download.ts +++ b/renterd/main/download.ts @@ -2,13 +2,29 @@ import fs from 'fs' import path from 'path' import { Octokit } from '@octokit/rest' import admZip from 'adm-zip' -import { getBinaryFilePath, getConfigAndBinaryDirectoryPath } from './config' import { promisify } from 'util' import stream from 'stream' import axios from 'axios' -import { system } from './state' - -export async function downloadRelease(): Promise { +import { system } from './system' +import { getBinaryDirectoryPath, getBinaryFilePath } from './binary' + +downloadRelease() + +// async function getLatestVersion(): Promise { +// 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 '' +// } +// } + +async function downloadRelease(): Promise { try { const octokit = new Octokit() const releaseData = await octokit.repos.getLatestRelease({ @@ -24,7 +40,10 @@ export async function downloadRelease(): Promise { console.log('Release name:', release.name) console.log('Release tag:', release.tag_name) await downloadFile(asset.browser_download_url) - extractBinary() // Assuming binary is zipped + await extractBinary() + // write a file called version into the bin directory with the release tag + const versionFilePath = path.join(getBinaryDirectoryPath(), 'version') + await fs.promises.writeFile(versionFilePath, release.tag_name) } else { throw new Error('Failed to find release asset') } @@ -79,7 +98,7 @@ async function extractBinary(): Promise { } function getTempDownloadsPath(): string { - return path.join(getConfigAndBinaryDirectoryPath(), 'download') + return path.join(getBinaryDirectoryPath(), 'download') } function getBinaryZipStagingPath(): string { diff --git a/renterd/main/env.ts b/renterd/main/env.ts new file mode 100644 index 0000000..ecd2042 --- /dev/null +++ b/renterd/main/env.ts @@ -0,0 +1,3 @@ +import isDev from 'electron-is-dev' + +export const env = { isDev } diff --git a/renterd/electron-src/index.ts b/renterd/main/index.ts similarity index 100% rename from renterd/electron-src/index.ts rename to renterd/main/index.ts diff --git a/renterd/electron-src/ipc.ts b/renterd/main/ipc.ts similarity index 85% rename from renterd/electron-src/ipc.ts rename to renterd/main/ipc.ts index c4fe045..a7ad204 100644 --- a/renterd/electron-src/ipc.ts +++ b/renterd/main/ipc.ts @@ -3,7 +3,6 @@ import { ipcMain, shell } from 'electron' import { getInstalledVersion, getIsDaemonRunning, - getLatestVersion, startDaemon, stopDaemon, } from './daemon' @@ -14,7 +13,6 @@ import { getIsConfigured, saveConfig, } from './config' -import { downloadRelease } from './download' import { closeWindow } from './window' export function initIpc() { @@ -33,9 +31,6 @@ export function initIpc() { ipcMain.handle('daemon-is-running', (_) => { return getIsDaemonRunning() }) - ipcMain.handle('daemon-update', async (_) => { - await downloadRelease() - }) ipcMain.handle('config-get', (_) => { const config = getConfig() return config @@ -54,9 +49,6 @@ export function initIpc() { ipcMain.handle('get-installed-version', (_) => { return getInstalledVersion() }) - ipcMain.handle('get-latest-version', (_) => { - return getLatestVersion() - }) ipcMain.handle('config-save', async (_, config: Config) => { await saveConfig(config) return true diff --git a/renterd/electron-src/next.ts b/renterd/main/next.ts similarity index 100% rename from renterd/electron-src/next.ts rename to renterd/main/next.ts diff --git a/renterd/electron-src/preload.ts b/renterd/main/preload.ts similarity index 100% rename from renterd/electron-src/preload.ts rename to renterd/main/preload.ts diff --git a/renterd/electron-src/shortcuts.ts b/renterd/main/shortcuts.ts similarity index 88% rename from renterd/electron-src/shortcuts.ts rename to renterd/main/shortcuts.ts index d84900a..ebb4e24 100644 --- a/renterd/electron-src/shortcuts.ts +++ b/renterd/main/shortcuts.ts @@ -1,5 +1,6 @@ import { globalShortcut } from 'electron' -import { state, system } from './state' +import { state } from './state' +import { system } from './system' export function initShortcuts() { // Register a global shortcut listener for the developer tools diff --git a/renterd/electron-src/startup.ts b/renterd/main/startup.ts similarity index 87% rename from renterd/electron-src/startup.ts rename to renterd/main/startup.ts index 83b160f..6be3fcc 100644 --- a/renterd/electron-src/startup.ts +++ b/renterd/main/startup.ts @@ -1,7 +1,8 @@ import { shell } from 'electron' import { startDaemon } from './daemon' import { getConfig, getIsConfigured } from './config' -import { state, system } from './state' +import { state } from './state' +import { env } from './env' export function startup() { // If the app is already configured, start the daemon and open browser @@ -12,7 +13,7 @@ export function startup() { shell.openExternal(`http://${getConfig().http.address}`) } - if (system.isDev) { + if (env.isDev) { state.mainWindow?.setMaximumSize(2000, 2000) state.mainWindow?.setSize(1000, 800) state.mainWindow?.webContents.openDevTools() diff --git a/renterd/electron-src/state.ts b/renterd/main/state.ts similarity index 50% rename from renterd/electron-src/state.ts rename to renterd/main/state.ts index f8f7e8c..444b1b2 100644 --- a/renterd/electron-src/state.ts +++ b/renterd/main/state.ts @@ -1,6 +1,5 @@ import { ChildProcess } from 'child_process' import { BrowserWindow, Tray } from 'electron' -import isDev from 'electron-is-dev' export let state: { mainWindow: BrowserWindow | null @@ -13,15 +12,3 @@ export let state: { isQuitting: false, daemon: null, } - -export const system: { - isDev: boolean - isDarwin: boolean - isLinux: boolean - isWindows: boolean -} = { - isDev: isDev, - isDarwin: process.platform === 'darwin', - isLinux: process.platform === 'linux', - isWindows: process.platform === 'win32', -} diff --git a/renterd/main/system.ts b/renterd/main/system.ts new file mode 100644 index 0000000..0e3b989 --- /dev/null +++ b/renterd/main/system.ts @@ -0,0 +1,9 @@ +export const system: { + isDarwin: boolean + isLinux: boolean + isWindows: boolean +} = { + isDarwin: process.platform === 'darwin', + isLinux: process.platform === 'linux', + isWindows: process.platform === 'win32', +} diff --git a/hostd/electron-src/tray.ts b/renterd/main/tray.ts similarity index 90% rename from hostd/electron-src/tray.ts rename to renterd/main/tray.ts index 1472d49..ea36999 100644 --- a/hostd/electron-src/tray.ts +++ b/renterd/main/tray.ts @@ -1,5 +1,6 @@ import { app, Tray, Menu } from 'electron' -import { state, system } from './state' +import { state } from './state' +import { system } from './system' import { getAsset } from './asset' export function initTray() { diff --git a/hostd/electron-src/tsconfig.json b/renterd/main/tsconfig.json similarity index 95% rename from hostd/electron-src/tsconfig.json rename to renterd/main/tsconfig.json index 06743f5..a569c53 100644 --- a/hostd/electron-src/tsconfig.json +++ b/renterd/main/tsconfig.json @@ -17,7 +17,7 @@ "skipLibCheck": true, "strict": true, "target": "esnext", - "outDir": "../main" + "outDir": "../dist/main" }, "exclude": ["node_modules"], "include": ["**/*.ts", "**/*.tsx", "**/*.js"] diff --git a/hostd/electron-src/window.ts b/renterd/main/window.ts similarity index 85% rename from hostd/electron-src/window.ts rename to renterd/main/window.ts index 81a5eb3..c58f38f 100644 --- a/hostd/electron-src/window.ts +++ b/renterd/main/window.ts @@ -1,8 +1,10 @@ import path, { join } from 'path' import { BrowserWindow, app } from 'electron' import { format } from 'url' -import { state, system } from './state' +import { state } from './state' +import { system } from './system' import { getAsset } from './asset' +import { env } from './env' export function initWindow() { state.mainWindow = new BrowserWindow({ @@ -25,10 +27,10 @@ export function initWindow() { // Hide the main window instead of closing it, to keep the app running in the tray state.mainWindow.on('close', closeWindow) - const url = system.isDev + const url = env.isDev ? 'http://localhost:8000/' : format({ - pathname: path.join(__dirname, '../renderer/out/index.html'), + pathname: path.join(__dirname, '../dist/renderer/index.html'), protocol: 'file:', slashes: true, }) diff --git a/renterd/package.json b/renterd/package.json index b754e55..539ba76 100644 --- a/renterd/package.json +++ b/renterd/package.json @@ -12,14 +12,15 @@ "type": "git", "url": "https://github.com/SiaFoundation/desktop.git" }, - "main": "main/index.js", + "main": "dist/main/index.js", "scripts": { - "clean": "rimraf out main renderer/out renderer/.next", + "clean": "rimraf out dist renderer/.next", "dev": "npm run build-electron && electron .", "build-renderer": "next build renderer", - "build-electron": "tsc -p electron-src", + "build-electron": "tsc -p main", + "download-daemon": "npm run build-electron && node main/download.js", "build": "npm run build-renderer && npm run build-electron", - "type-check": "tsc -p ./renderer/tsconfig.json && tsc -p ./electron-src/tsconfig.json", + "type-check": "tsc -p ./renderer/tsconfig.json && tsc -p ./main/tsconfig.json", "start": "electron-forge start", "package": "electron-forge package", "make": "electron-forge make", @@ -85,9 +86,9 @@ "icon": "assets/appicon.png", "asar": true, "files": [ - "main", - "renderer/out", - "assets" + "dist", + "assets", + "bin" ] } } diff --git a/renterd/renderer/components/App.tsx b/renterd/renderer/components/App.tsx index d399319..2ad6060 100644 --- a/renterd/renderer/components/App.tsx +++ b/renterd/renderer/components/App.tsx @@ -8,7 +8,6 @@ import { import { ConfigForm } from './ConfigForm' import { useConfig } from '../contexts/config' import { Header } from './Header' -import { UpdateBanner } from './UpdateBanner' export function App() { const { onSubmit } = useConfig() @@ -16,7 +15,6 @@ export function App() {
-
diff --git a/renterd/renderer/components/Header.tsx b/renterd/renderer/components/Header.tsx index 44c1c24..87f0c4a 100644 --- a/renterd/renderer/components/Header.tsx +++ b/renterd/renderer/components/Header.tsx @@ -10,7 +10,6 @@ import { Launch16, Reset16 } from '@siafoundation/react-icons' import { useDaemon } from './useDaemon' import { useConfig } from '../contexts/config' import { useConfigData } from './useConfigData' -import { useLatestVersion } from './useLatestVersion' import { useInstalledVersion } from './useInstalledVersion' export function Header() { @@ -18,7 +17,6 @@ export function Header() { const { form, isConfigured, changeCount, revalidateAndResetForm } = useConfig() const config = useConfigData() - const latestVersion = useLatestVersion() const installedVersion = useInstalledVersion() console.log(config.data) return ( diff --git a/renterd/renderer/components/UpdateBanner.tsx b/renterd/renderer/components/UpdateBanner.tsx deleted file mode 100644 index 30ba31a..0000000 --- a/renterd/renderer/components/UpdateBanner.tsx +++ /dev/null @@ -1,81 +0,0 @@ -'use client' - -import { - LoadingDots, - Text, - triggerErrorToast, - triggerSuccessToast, -} from '@siafoundation/design-system' -import { Upgrade16 } from '@siafoundation/react-icons' -import { useLatestVersion } from './useLatestVersion' -import { useInstalledVersion } from './useInstalledVersion' -import { useCallback, useState } from 'react' - -export function UpdateBanner() { - const latestVersion = useLatestVersion() - const installedVersion = useInstalledVersion() - const [isUpdating, setIsUpdating] = useState(false) - const [isRestarting, setIsRestarting] = useState(false) - - const update = useCallback(async () => { - try { - setIsUpdating(true) - await window.electron.daemonUpdate() - } catch (e) { - console.log(e) - triggerErrorToast('Error downloading update. Please try again.') - setIsUpdating(false) - setIsRestarting(false) - return - } - try { - setIsRestarting(true) - await window.electron.daemonStart() - } catch (e) { - console.log(e) - triggerErrorToast('Error restarting daemon. Please try again.') - setIsUpdating(false) - setIsRestarting(false) - return - } - triggerSuccessToast(`Updated to renterd version ${installedVersion.data}.`) - setIsUpdating(false) - setIsRestarting(false) - }, [setIsUpdating, installedVersion.data]) - - if (latestVersion.data === installedVersion.data) { - return null - } - - return ( -
- {isRestarting ? ( - <> - - - Restarting renterd daemon {latestVersion.data} - - - ) : isUpdating ? ( - <> - - - Updating to renterd {latestVersion.data} - - - ) : ( - <> - - - - - An update to renterd {latestVersion.data} is available. - - - )} -
- ) -} diff --git a/renterd/renderer/components/useLatestVersion.tsx b/renterd/renderer/components/useLatestVersion.tsx deleted file mode 100644 index 56cb13f..0000000 --- a/renterd/renderer/components/useLatestVersion.tsx +++ /dev/null @@ -1,17 +0,0 @@ -'use client' - -import { hoursInMilliseconds } from '@siafoundation/design-system' -import useSWR from 'swr' - -export function useLatestVersion() { - return useSWR( - 'latestVersion', - async () => { - return await window.electron.getLatestVersion() - }, - { - refreshInterval: hoursInMilliseconds(1), - revalidateOnFocus: false, - } - ) -} diff --git a/renterd/renderer/renderer.d.ts b/renterd/renderer/renderer.d.ts index 75d9bb1..d8971c1 100644 --- a/renterd/renderer/renderer.d.ts +++ b/renterd/renderer/renderer.d.ts @@ -6,13 +6,11 @@ export interface API { openBrowser: (url: string) => Promise daemonStart: () => Promise daemonStop: () => Promise - daemonUpdate: () => Promise getConfig: () => Promise openDataDirectory: () => Promise getIsConfigured: () => Promise getDefaultDataDirectory: () => Promise getInstalledVersion: () => Promise - getLatestVersion: () => Promise saveConfig: (config: Config) => Promise }