Skip to content

Commit 2f88722

Browse files
committed
feat: refactor cleanUpDownload to use safeRemoveDirectory and update related async calls
1 parent 48c42d2 commit 2f88722

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

src/backend/storeManagers/hyperplay/games.ts

+21-20
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ import {
6565
handleArchAndPlatform,
6666
handlePlatformReversed,
6767
runModPatcher,
68-
sanitizeVersion
68+
sanitizeVersion,
69+
safeRemoveDirectory
6970
} from './utils'
7071
import { getSettings as getSettingsSideload } from 'backend/storeManagers/sideload/games'
7172
import {
@@ -423,11 +424,11 @@ const findExecutables = async (folderPath: string): Promise<string[]> => {
423424
return executables
424425
}
425426

426-
export function cleanUpDownload(appName: string, directory: string) {
427+
export async function cleanUpDownload(appName: string, directory: string) {
427428
inProgressDownloadsMap.delete(appName)
428429
inProgressExtractionsMap.delete(appName)
429430
deleteAbortController(appName)
430-
rmSync(directory, { recursive: true, force: true })
431+
await safeRemoveDirectory(directory)
431432
}
432433

433434
function getDownloadUrl(platformInfo: PlatformConfig, appName: string) {
@@ -523,9 +524,9 @@ async function downloadGame(
523524
res()
524525
}
525526

526-
function onCancel() {
527+
async function onCancel() {
527528
try {
528-
cleanUpDownload(appName, directory)
529+
await cleanUpDownload(appName, directory)
529530
} catch (err) {
530531
rej(err)
531532
}
@@ -1181,7 +1182,7 @@ export async function extract(
11811182
body: `Installed`
11821183
})
11831184

1184-
cleanUpDownload(appName, directory)
1185+
await cleanUpDownload(appName, directory)
11851186

11861187
sendFrontendMessage('refreshLibrary', 'hyperplay')
11871188

@@ -1190,21 +1191,21 @@ export async function extract(
11901191
})
11911192
}
11921193
)
1193-
extractService.once('error', (error: Error) => {
1194+
extractService.once('error', async (error: Error) => {
11941195
logError(`Extracting Error ${error.message}`, LogPrefix.HyperPlay)
11951196

11961197
cancelQueueExtraction()
11971198
callAbortController(appName)
11981199

1199-
cleanUpDownload(appName, directory)
1200+
await cleanUpDownload(appName, directory)
12001201

12011202
sendFrontendMessage('refreshLibrary', 'hyperplay')
12021203

12031204
resolve({
12041205
status: 'error'
12051206
})
12061207
})
1207-
extractService.once('canceled', () => {
1208+
extractService.once('canceled', async () => {
12081209
logInfo(
12091210
`Canceled Extracting: Cancellation completed on ${appName} - Destination ${destinationPath}`,
12101211
LogPrefix.HyperPlay
@@ -1242,7 +1243,7 @@ export async function extract(
12421243
body: 'Installation Stopped'
12431244
})
12441245

1245-
cleanUpDownload(appName, directory)
1246+
await cleanUpDownload(appName, directory)
12461247

12471248
sendFrontendMessage('refreshLibrary', 'hyperplay')
12481249

@@ -1914,13 +1915,10 @@ async function applyPatching(
19141915

19151916
if (signal.aborted) {
19161917
logInfo(`Patching ${appName} aborted`, LogPrefix.HyperPlay)
1917-
rmSync(datastoreDir, { recursive: true })
1918-
return { status: 'abort' }
1919-
}
1920-
1921-
signal.onabort = () => {
1918+
await safeRemoveDirectory(datastoreDir, {
1919+
sizeThresholdMB: blockSize * totalBlocks
1920+
})
19221921
aborted = true
1923-
rmSync(datastoreDir, { recursive: true })
19241922
return { status: 'abort' }
19251923
}
19261924

@@ -2005,7 +2003,9 @@ async function applyPatching(
20052003
}
20062004
// need this to cover 100% of abort cases
20072005
if (aborted) {
2008-
rmSync(datastoreDir, { recursive: true })
2006+
await safeRemoveDirectory(datastoreDir, {
2007+
sizeThresholdMB: blockSize * totalBlocks
2008+
})
20092009
return { status: 'abort' }
20102010
}
20112011

@@ -2020,8 +2020,9 @@ async function applyPatching(
20202020
})
20212021

20222022
logInfo(`Patching ${appName} completed`, LogPrefix.HyperPlay)
2023-
rmSync(datastoreDir, { recursive: true })
2024-
2023+
await safeRemoveDirectory(datastoreDir, {
2024+
sizeThresholdMB: blockSize * totalBlocks
2025+
})
20252026
return { status: 'done' }
20262027
} catch (error) {
20272028
if (error instanceof PatchingError) {
@@ -2061,7 +2062,7 @@ async function applyPatching(
20612062

20622063
// errors can be thrown before datastore dir created. rmSync on nonexistent dir blocks indefinitely
20632064
if (existsSync(datastoreDir)) {
2064-
rmSync(datastoreDir, { recursive: true })
2065+
await safeRemoveDirectory(datastoreDir)
20652066
}
20662067

20672068
return { status: 'error', error: `Error while patching ${error}` }

src/backend/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,7 @@ export async function downloadFile(
983983
abortController: AbortController,
984984
progressCallback?: ProgressCallback,
985985
onCompleted?: (file: File) => void,
986-
onCancel?: (item: DownloadItem) => void
986+
onCancel?: (item: DownloadItem) => Promise<void>
987987
): Promise<DownloadItem> {
988988
let lastProgressUpdateTime = Date.now()
989989
let lastBytesWritten = 0

0 commit comments

Comments
 (0)