Skip to content

Commit

Permalink
feat: download and open installer first (#72)
Browse files Browse the repository at this point in the history
* feat: download and open installer first

* feat: add asset readiness to status endpoint

* chore: switch to new static folder version
  • Loading branch information
Cafe137 authored Apr 27, 2022
1 parent baee48c commit c986e3d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
36 changes: 33 additions & 3 deletions src/downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ const platformTable = {
linux: 'linux',
}

export async function waitForInstallerReadiness(): Promise<void> {
return waitForAsset('static')
}

export function isBeeAssetReady(): boolean {
return existsSync(getPath(process.platform === 'win32' ? 'bee.exe' : 'bee'))
}

export async function runDownloader(): Promise<void> {
const archString = Reflect.get(archTable, process.arch)
const platformString = Reflect.get(platformTable, process.platform)
Expand All @@ -35,14 +43,14 @@ export async function runDownloader(): Promise<void> {
throw Error(`Unsupported system: arch=${arch()} platform=${platform()}`)
}
await ensureDir(paths.data)
await ensureAsset('https://github.com/ethersphere/bee-desktop/releases/download/v0.2.2/static.zip', 'static.zip', {
checkTarget: 'static',
})
await ensureAsset(
`https://github.com/ethersphere/bee/releases/download/v1.5.1/bee-${platformString}-${archString}${suffixString}`,
`bee${suffixString}`,
{ chmod: process.platform !== 'win32' },
)
await ensureAsset('https://github.com/ethersphere/bee-desktop/releases/download/v0.1.1/static.zip', 'static.zip', {
checkTarget: 'static',
})
}

async function ensureAsset(url: string, target: string, options?: DownloadOptions): Promise<void> {
Expand Down Expand Up @@ -82,3 +90,25 @@ async function downloadFile(url: string, target: string): Promise<void> {
.then(async x => x.arrayBuffer())
.then(x => writeFileSync(target, Buffer.from(x)))
}

async function waitForAsset(path: string): Promise<void> {
return new Promise(async (resolve, reject) => {
for (let i = 0; i < 600; i++) {
logger.debug(`Waiting for asset ${path}`)

if (existsSync(getPath(path))) {
resolve()

return
}
await wait(1000)
}
reject()
})
}

async function wait(ms: number): Promise<void> {
return new Promise(resolve => {
setTimeout(resolve, ms)
})
}
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { shell } from 'electron'
import { getApiKey } from './api-key'
import { runDownloader } from './downloader'
import { runDownloader, waitForInstallerReadiness } from './downloader'
import { runElectronTray } from './electron'
import { runLauncher } from './launcher'
import { findFreePort, port } from './port'
import { runServer } from './server'
import { getStatus } from './status'

async function main() {
await runDownloader()
await findFreePort()
runDownloader()
await Promise.all([waitForInstallerReadiness(), findFreePort()])
runServer()
runElectronTray()

Expand Down
3 changes: 3 additions & 0 deletions src/status.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { readFileSync } from 'fs'
import { join } from 'path'
import { readConfigYaml } from './config-yaml'
import { isBeeAssetReady } from './downloader'
import { checkPath, getPath } from './path'

interface Status {
status: 0 | 1 | 2
address: string | null
config: Record<string, any>
assetsReady: boolean
}

export function getStatus() {
const statusObject: Status = {
status: 0,
address: null,
config: null,
assetsReady: isBeeAssetReady(),
}

if (!checkPath('config.yaml') || !checkPath('data-dir')) {
Expand Down

0 comments on commit c986e3d

Please sign in to comment.