Skip to content

Commit

Permalink
feat: refactor to one single method
Browse files Browse the repository at this point in the history
  • Loading branch information
flavioislima committed Nov 1, 2023
1 parent 3f2cfe0 commit 2ef264b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 38 deletions.
22 changes: 11 additions & 11 deletions src/backend/tools/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ExecResult, GameSettings, Runner, WineCommandArgs } from 'common/types'
import axios from 'axios'

import {
existsSync,
readFileSync,
Expand All @@ -10,12 +11,13 @@ import {
rm
} from 'graceful-fs'

import decompress from '@xhmikosr/decompress'
import decompressTargz from '@xhmikosr/decompress-targz'
import decompressTarxz from '@felipecrs/decompress-tarxz'

import { exec, spawn } from 'child_process'
import { downloadFile, execAsync, getWineFromProton } from '../utils'
import {
downloadFile,
execAsync,
extractFiles,
getWineFromProton
} from '../utils'
import {
execOptions,
toolsPath,
Expand Down Expand Up @@ -65,25 +67,21 @@ export const DXVK = {
{
name: 'vkd3d',
url: getVkd3dUrl(),
extractPlugin: decompressTarxz(),
os: 'linux'
},
{
name: 'dxvk',
url: getDxvkUrl(),
extractPlugin: decompressTargz(),
os: 'linux'
},
{
name: 'dxvk-nvapi',
url: 'https://api.github.com/repos/jp7677/dxvk-nvapi/releases/latest',
extractPlugin: decompressTargz(),
os: 'linux'
},
{
name: 'dxvk-macOS',
url: 'https://api.github.com/repos/Gcenx/DXVK-macOS/releases/latest',
extractPlugin: decompressTargz(),
os: 'darwin'
}
]
Expand Down Expand Up @@ -144,8 +142,10 @@ export const DXVK = {
logInfo(`downloaded ${tool.name}`, LogPrefix.DXVKInstaller)
logInfo(`extracting ${tool.name}`, LogPrefix.DXVKInstaller)
exec(echoCommand)
await decompress(latestVersion, destination, {
plugins: [tool.extractPlugin]
await extractFiles({
path: latestVersion,
destination,
strip: 0
})
.then(() => {
logInfo(`${tool.name} updated!`, LogPrefix.DXVKInstaller)
Expand Down
20 changes: 19 additions & 1 deletion src/backend/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ import { getHeroicVersion } from './utils/systeminfo/heroicVersion'
import { wikiGameInfoStore } from './wiki_game_info/electronStore'
import EasyDl from 'easydl'

import decompress from '@xhmikosr/decompress'
import decompressTargz from '@xhmikosr/decompress-targz'
import decompressTarxz from '@felipecrs/decompress-tarxz'

const execAsync = promisify(exec)

const { showMessageBox } = dialog
Expand Down Expand Up @@ -1337,6 +1341,19 @@ function calculateEta(
return eta
}

interface ExtractOptions {
path: string
destination: string
strip: number
}

async function extractFiles({ path, destination, strip = 0 }: ExtractOptions) {
return decompress(path, destination, {
plugins: [decompressTargz(), decompressTarxz()],
strip
})
}

export {
errorHandler,
execAsync,
Expand Down Expand Up @@ -1368,7 +1385,8 @@ export {
getFileSize,
memoryLog,
removeFolder,
calculateEta
calculateEta,
extractFiles
}

// Exported only for testing purpose
Expand Down
18 changes: 2 additions & 16 deletions src/backend/wine/manager/downloader/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import { isMac } from '../../../constants'
import * as axios from 'axios'
import { existsSync, statSync, unlinkSync } from 'graceful-fs'
import { spawnSync } from 'child_process'
import decompress from '@xhmikosr/decompress'
import decompressTargz from '@xhmikosr/decompress-targz'
import decompressTarxz from '@felipecrs/decompress-tarxz'

import { ProgressInfo, State, VersionInfo, Type } from 'common/types'
import { extractFiles } from 'backend/utils'

interface fetchProps {
url: string
Expand Down Expand Up @@ -152,19 +150,7 @@ async function unzipFile({
reject(error.message)
}

let extractPlugin
if (filePath.endsWith('tar.gz')) {
extractPlugin = decompressTargz()
} else if (filePath.endsWith('tar.xz')) {
extractPlugin = decompressTarxz()
} else {
reject(`Archive type ${filePath.split('.').pop()} not supported!`)
}

decompress(filePath, unzipDir, {
plugins: [extractPlugin],
strip: 1
})
extractFiles({ path: filePath, destination: unzipDir, strip: 1 })
.then(() => {
onProgress('idle')
resolve(`Succesfully unzip ${filePath} to ${unzipDir}.`)
Expand Down
16 changes: 6 additions & 10 deletions src/backend/wine/runtimes/util.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import decompress from '@xhmikosr/decompress'
import axios from 'axios'
import { extractFiles } from 'backend/utils'
import { existsSync, mkdirSync, writeFile } from 'graceful-fs'
import decompressTargz from '@xhmikosr/decompress-targz'
import decompressTarxz from '@felipecrs/decompress-tarxz'

interface GithubAssetMetadata {
url: string
Expand Down Expand Up @@ -93,13 +91,11 @@ async function extractTarFile(
mkdirSync(extractedPath, { recursive: true })

const strip = options?.strip
return decompress(filePath, extractedPath, {
plugins: [
contentType === 'application/x-gzip'
? decompressTargz()
: decompressTarxz()
],
strip: strip ? 1 : 0

return extractFiles({
path: filePath,
destination: extractedPath,
strip: strip || 0
})
}

Expand Down

0 comments on commit 2ef264b

Please sign in to comment.