diff --git a/packages/playground/blueprints/src/lib/steps/install-asset.ts b/packages/playground/blueprints/src/lib/steps/install-asset.ts index 0f93b1968f..3ea3c3b56e 100644 --- a/packages/playground/blueprints/src/lib/steps/install-asset.ts +++ b/packages/playground/blueprints/src/lib/steps/install-asset.ts @@ -1,4 +1,5 @@ import type { UniversalPHP } from '@php-wasm/universal'; +import { joinPaths } from '@php-wasm/util'; import { writeFile } from './write-file'; import { unzip } from './unzip'; @@ -29,30 +30,26 @@ export async function installAsset( assetFolderName: string; }> { // Extract to temporary folder so we can find asset folder name - const zipFileName = zipFile.name; const assetNameGuess = zipFileName.replace(/\.zip$/, ''); - const tmpUnzippedFilesPath = `/tmp/assets/${assetNameGuess}`; - const tmpZipPath = `/tmp/${zipFileName}`; + const wpContent = joinPaths(await playground.documentRoot, 'wp-content'); + const tmpDir = joinPaths(wpContent, crypto.randomUUID()); + const tmpZipPath = joinPaths(tmpDir, zipFileName); + const tmpUnzippedFilesPath = joinPaths(tmpDir, 'assets', assetNameGuess); - const removeTmpFolder = () => - playground.rmdir(tmpUnzippedFilesPath, { + if (await playground.fileExists(tmpUnzippedFilesPath)) { + await playground.rmdir(tmpDir, { recursive: true, }); - - if (await playground.fileExists(tmpUnzippedFilesPath)) { - await removeTmpFolder(); } + await playground.mkdir(tmpDir); await writeFile(playground, { path: tmpZipPath, data: zipFile, }); - const cleanup = () => - Promise.all([removeTmpFolder, () => playground.unlink(tmpZipPath)]); - try { await unzip(playground, { zipPath: tmpZipPath, @@ -84,14 +81,14 @@ export async function installAsset( // Move asset folder to target path const assetFolderPath = `${targetPath}/${assetFolderName}`; await playground.mv(tmpAssetPath, assetFolderPath); - await cleanup(); return { assetFolderPath, assetFolderName, }; - } catch (error) { - await cleanup(); - throw error; + } finally { + await playground.rmdir(tmpDir, { + recursive: true, + }); } }