From 7acbecb4573e9ebc890c8e6e5fa2718f577ec569 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Wed, 20 Dec 2023 00:28:39 +0100 Subject: [PATCH 1/2] Fix a cross-device mv error by using a tmp directory inside document root --- .../playground/blueprints/src/lib/steps/install-asset.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/playground/blueprints/src/lib/steps/install-asset.ts b/packages/playground/blueprints/src/lib/steps/install-asset.ts index 0f93b1968f..4b574eb522 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'; @@ -33,8 +34,11 @@ export async function installAsset( 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()); + await playground.mkdir(tmpDir); + const tmpZipPath = joinPaths(tmpDir, zipFileName); + const tmpUnzippedFilesPath = joinPaths(tmpDir, 'assets', assetNameGuess); const removeTmpFolder = () => playground.rmdir(tmpUnzippedFilesPath, { From dd471da369f1fe6c534d2b6648e93f2cf51bb57c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Wed, 20 Dec 2023 09:32:49 +0100 Subject: [PATCH 2/2] Cleanup the entire tmp directory after installing a plugin --- .../blueprints/src/lib/steps/install-asset.ts | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/packages/playground/blueprints/src/lib/steps/install-asset.ts b/packages/playground/blueprints/src/lib/steps/install-asset.ts index 4b574eb522..3ea3c3b56e 100644 --- a/packages/playground/blueprints/src/lib/steps/install-asset.ts +++ b/packages/playground/blueprints/src/lib/steps/install-asset.ts @@ -30,33 +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 wpContent = joinPaths(await playground.documentRoot, 'wp-content'); const tmpDir = joinPaths(wpContent, crypto.randomUUID()); - await playground.mkdir(tmpDir); 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, @@ -88,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, + }); } }