Skip to content

Commit

Permalink
built with binary pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Jan 31, 2024
1 parent a58c4d6 commit ce49c9d
Show file tree
Hide file tree
Showing 47 changed files with 151 additions and 357 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
dist
out
bin
node_modules
.next
.DS_Store
main
.env
bin
4 changes: 2 additions & 2 deletions hostd/electron-src/asset.ts → hostd/main/asset.ts
Original file line number Diff line number Diff line change
@@ -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)
}
10 changes: 10 additions & 0 deletions hostd/main/binary.ts
Original file line number Diff line number Diff line change
@@ -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)
}
18 changes: 4 additions & 14 deletions hostd/electron-src/config.ts → hostd/main/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export async function saveConfig(config: Config): Promise<void> {
}

await fs.promises.mkdir(config.directory, { recursive: true })
await fs.promises.mkdir(getConfigAndBinaryDirectoryPath(), {
await fs.promises.mkdir(getConfigDirectoryPath(), {
recursive: true,
})

Expand All @@ -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()
}
18 changes: 2 additions & 16 deletions hostd/electron-src/daemon.ts → hostd/main/daemon.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
return new Promise(async (resolve, reject) => {
Expand Down Expand Up @@ -88,17 +88,3 @@ 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: 'hostd',
})
return response.data.tag_name
} catch (err) {
console.error(err)
return ''
}
}
27 changes: 23 additions & 4 deletions hostd/electron-src/download.ts → hostd/main/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
// 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<void> {
try {
Expand All @@ -24,7 +40,10 @@ export async function downloadRelease(): Promise<void> {
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')
}
Expand Down Expand Up @@ -79,7 +98,7 @@ async function extractBinary(): Promise<void> {
}

function getTempDownloadsPath(): string {
return path.join(getConfigAndBinaryDirectoryPath(), 'download')
return path.join(getBinaryDirectoryPath(), 'download')
}

function getBinaryZipStagingPath(): string {
Expand Down
3 changes: 3 additions & 0 deletions hostd/main/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import isDev from 'electron-is-dev'

export const env = { isDev }
File renamed without changes.
8 changes: 0 additions & 8 deletions hostd/electron-src/ipc.ts → hostd/main/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { ipcMain, shell } from 'electron'
import {
getInstalledVersion,
getIsDaemonRunning,
getLatestVersion,
startDaemon,
stopDaemon,
} from './daemon'
Expand All @@ -14,7 +13,6 @@ import {
getIsConfigured,
saveConfig,
} from './config'
import { downloadRelease } from './download'
import { closeWindow } from './window'

export function initIpc() {
Expand All @@ -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
Expand All @@ -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
Expand Down
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion hostd/electron-src/shortcuts.ts → hostd/main/shortcuts.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 3 additions & 2 deletions hostd/electron-src/startup.ts → hostd/main/startup.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Expand Down
13 changes: 0 additions & 13 deletions hostd/electron-src/state.ts → hostd/main/state.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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',
}
9 changes: 9 additions & 0 deletions hostd/main/system.ts
Original file line number Diff line number Diff line change
@@ -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',
}
3 changes: 2 additions & 1 deletion renterd/electron-src/tray.ts → hostd/main/tray.ts
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"skipLibCheck": true,
"strict": true,
"target": "esnext",
"outDir": "../main"
"outDir": "../dist/main"
},
"exclude": ["node_modules"],
"include": ["**/*.ts", "**/*.tsx", "**/*.js"]
Expand Down
8 changes: 5 additions & 3 deletions renterd/electron-src/window.ts → hostd/main/window.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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,
})
Expand Down
15 changes: 8 additions & 7 deletions hostd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -85,9 +86,9 @@
"icon": "assets/appicon.png",
"asar": true,
"files": [
"main",
"renderer/out",
"assets"
"dist",
"assets",
"bin"
]
}
}
2 changes: 0 additions & 2 deletions hostd/renderer/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ import {
import { ConfigForm } from './ConfigForm'
import { useConfig } from '../contexts/config'
import { Header } from './Header'
import { UpdateBanner } from './UpdateBanner'

export function App() {
const { onSubmit } = useConfig()
return (
<form onSubmit={onSubmit}>
<AppBackdrop />
<div className="flex flex-col w-full h-screen justify-center items-center">
<UpdateBanner />
<ScrollArea className="flex-1">
<div className="flex flex-col gap-3 w-full justify-center items-center pb-4">
<Header />
Expand Down
3 changes: 0 additions & 3 deletions hostd/renderer/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@ 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() {
const { isRunning, startDaemon, stopDaemon } = useDaemon()
const { form, isConfigured, changeCount, revalidateAndResetForm } =
useConfig()
const config = useConfigData()
const latestVersion = useLatestVersion()
const installedVersion = useInstalledVersion()
console.log(config.data)
return (
<Panel
className="sticky z-10 top-0 w-full h-10 flex gap-2 justify-center items-center px-3 max-w-[500px] rounded-t-none"
Expand Down
Loading

0 comments on commit ce49c9d

Please sign in to comment.