Skip to content

Commit

Permalink
fix(chore): handle wallet version download
Browse files Browse the repository at this point in the history
  • Loading branch information
gabaldon committed Jun 8, 2022
1 parent 477dcd5 commit be2fe5d
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 85 deletions.
2 changes: 1 addition & 1 deletion src/background/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@ export const WITNET_RUST_VERSION = '1.5.0'

export const DEFAULT_WALLET_LOG_LEVEL = 'error'

export const RELEASE_URL = `https://api.github.com/repos/witnet/witnet-rust/releases/tags/${WITNET_RUST_VERSION}`
export const RELEASE_BASE_URL = `https://api.github.com/repos/witnet/witnet-rust/releases/tags/`
174 changes: 90 additions & 84 deletions src/background/walletManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
ARCH,
PLATFORM,
OS_ARCH,
RELEASE_URL,
RELEASE_BASE_URL,
} from './constants'
import { AppManager } from './appManager'
import getVersionFromName from './utils/getVersionFromName'
Expand All @@ -36,18 +36,19 @@ interface GithubTagInfo {
assets: Array<GithubReleaseAsset>
}

const decompressWallet = {
win32: decompressWin32Wallet,
darwin: decompressDarwinWallet,
linux: decompressLinuxWallet,
}[PLATFORM]

export class WalletManager {
public app: AppManager
public isUpdating: boolean = false
public walletProcess: cp.ChildProcessWithoutNullStreams | null = null
private existDirectory: boolean
private needToDownloadWallet: boolean = true
private latestWitnetRustVersion: string = WITNET_RUST_VERSION
private witnetRustVersion: string = WITNET_RUST_VERSION
private decompressWallet = {
win32: this.decompressWin32Wallet,
darwin: this.decompressDarwinWallet,
linux: this.decompressLinuxWallet,
}[PLATFORM]

constructor(appManager: AppManager) {
this.app = appManager
Expand All @@ -65,23 +66,28 @@ export class WalletManager {
'utf8',
)
const installedVersion = getVersionFromName(versionName)
const latestWitnetRustVersion = await getLatestWitnetRustRelease()
this.latestWitnetRustVersion = await getLatestWitnetRustRelease()
const isLatestVersionInstalled =
installedVersion === latestWitnetRustVersion
installedVersion === this.latestWitnetRustVersion
const isCompatibleRelease = semver.satisfies(
WITNET_RUST_VERSION,
`~${latestWitnetRustVersion}`,
this.latestWitnetRustVersion,
`~${WITNET_RUST_VERSION}`,
)
this.needToDownloadWallet =
isCompatibleRelease && !isLatestVersionInstalled
} catch (err) {
console.error('An error occured trying to read version file', err)
}
}
console.info(`Fetching release from: ${RELEASE_URL}`)
this.witnetRustVersion = this.needToDownloadWallet
? this.latestWitnetRustVersion
: WITNET_RUST_VERSION
console.info(
`Fetching release from: ${RELEASE_BASE_URL}${this.witnetRustVersion}`,
)

const downloadUrl: string | undefined = await fetchReleaseDownloadUrl(
RELEASE_URL,
`${RELEASE_BASE_URL}${this.witnetRustVersion}`,
ARCH,
PLATFORM,
)
Expand Down Expand Up @@ -139,6 +145,75 @@ export class WalletManager {
})
}

// Decompress downloaded wallet release on macOS
private async decompressDarwinWallet(file: string) {
try {
const currentCwd = process.cwd()
process.chdir(SHEIKAH_PATH)
cp.execSync(`tar -xvf ${file}`)
process.chdir(currentCwd)

await sleep(3000)
overwriteWitnetNodeConfiguration({
sheikahPath: SHEIKAH_PATH,
witnetConfigFileName: WITNET_CONFIG_FILE_NAME,
publicNodeUrls: URLS_PUBLIC_WITNET_NODES,
})

fs.writeFileSync(
path.join(SHEIKAH_PATH, VERSION_FILE_NAME),
this.witnetRustVersion,
)
} catch (err) {
console.error(err)
}
}

// Decompress downloaded wallet release on windows
private async decompressWin32Wallet(file: string) {
tar.x({ file, sync: true })
fs.copyFileSync(WITNET_FILE_NAME, path.join(SHEIKAH_PATH, WITNET_FILE_NAME))
fs.copyFileSync(
'witnet.toml',
path.join(SHEIKAH_PATH, WITNET_CONFIG_FILE_NAME),
)
await sleep(3000)

overwriteWitnetNodeConfiguration({
sheikahPath: SHEIKAH_PATH,
witnetConfigFileName: WITNET_CONFIG_FILE_NAME,
publicNodeUrls: URLS_PUBLIC_WITNET_NODES,
})

fs.writeFileSync(
path.join(SHEIKAH_PATH, VERSION_FILE_NAME),
this.witnetRustVersion,
)
}

// Decompress downloaded wallet release on linux
private async decompressLinuxWallet(file: string) {
tar.x({ file, sync: true })
fs.copyFileSync('witnet', path.join(SHEIKAH_PATH, WITNET_FILE_NAME))
fs.copyFileSync(
'witnet.toml',
path.join(SHEIKAH_PATH, WITNET_CONFIG_FILE_NAME),
)

await sleep(3000)
overwriteWitnetNodeConfiguration({
sheikahPath: SHEIKAH_PATH,
witnetConfigFileName: WITNET_CONFIG_FILE_NAME,
publicNodeUrls: URLS_PUBLIC_WITNET_NODES,
})

cp.execSync(`chmod 777 ${path.join(SHEIKAH_PATH, WITNET_FILE_NAME)}`)
fs.writeFileSync(
path.join(SHEIKAH_PATH, VERSION_FILE_NAME),
this.witnetRustVersion,
)
}

private async handleDownloadWalletResponse(response: AxiosResponse) {
const walletCompressPath = path.join(
SHEIKAH_PATH,
Expand All @@ -165,7 +240,7 @@ export class WalletManager {
}

console.info('Decompressing wallet release...')
decompressWallet(walletCompressPath)
this.decompressWallet(walletCompressPath)
// remove compressed file
fs.unlinkSync(walletCompressPath)
}
Expand Down Expand Up @@ -213,7 +288,7 @@ async function getLatestWitnetRustRelease(): Promise<string> {
const result: AxiosResponse<any> = await axios.get(
'https://api.github.com/repos/witnet/witnet-rust/releases/latest',
)
return await result.data.tag_name
return (await result.data.tag_name) || ''
} catch (err) {
console.log(
'There was an error getting the latest Witnet Rust Release name:',
Expand Down Expand Up @@ -241,72 +316,3 @@ async function fetchReleaseDownloadUrl(

return release?.browser_download_url
}

// Decompress downloaded wallet release on macOS
async function decompressDarwinWallet(file: string) {
try {
const currentCwd = process.cwd()
process.chdir(SHEIKAH_PATH)
cp.execSync(`tar -xvf ${file}`)
process.chdir(currentCwd)

await sleep(3000)
overwriteWitnetNodeConfiguration({
sheikahPath: SHEIKAH_PATH,
witnetConfigFileName: WITNET_CONFIG_FILE_NAME,
publicNodeUrls: URLS_PUBLIC_WITNET_NODES,
})

fs.writeFileSync(
path.join(SHEIKAH_PATH, VERSION_FILE_NAME),
WITNET_RUST_VERSION,
)
} catch (err) {
console.error(err)
}
}

// Decompress downloaded wallet release on windows
async function decompressWin32Wallet(file: string) {
tar.x({ file, sync: true })
fs.copyFileSync(WITNET_FILE_NAME, path.join(SHEIKAH_PATH, WITNET_FILE_NAME))
fs.copyFileSync(
'witnet.toml',
path.join(SHEIKAH_PATH, WITNET_CONFIG_FILE_NAME),
)
await sleep(3000)

overwriteWitnetNodeConfiguration({
sheikahPath: SHEIKAH_PATH,
witnetConfigFileName: WITNET_CONFIG_FILE_NAME,
publicNodeUrls: URLS_PUBLIC_WITNET_NODES,
})

fs.writeFileSync(
path.join(SHEIKAH_PATH, VERSION_FILE_NAME),
WITNET_RUST_VERSION,
)
}

// Decompress downloaded wallet release on linux
async function decompressLinuxWallet(file: string) {
tar.x({ file, sync: true })
fs.copyFileSync('witnet', path.join(SHEIKAH_PATH, WITNET_FILE_NAME))
fs.copyFileSync(
'witnet.toml',
path.join(SHEIKAH_PATH, WITNET_CONFIG_FILE_NAME),
)

await sleep(3000)
overwriteWitnetNodeConfiguration({
sheikahPath: SHEIKAH_PATH,
witnetConfigFileName: WITNET_CONFIG_FILE_NAME,
publicNodeUrls: URLS_PUBLIC_WITNET_NODES,
})

cp.execSync(`chmod 777 ${path.join(SHEIKAH_PATH, WITNET_FILE_NAME)}`)
fs.writeFileSync(
path.join(SHEIKAH_PATH, VERSION_FILE_NAME),
WITNET_RUST_VERSION,
)
}

0 comments on commit be2fe5d

Please sign in to comment.