diff --git a/packages/playground/client/src/index.ts b/packages/playground/client/src/index.ts index 26b2de4b3c..896ca01989 100644 --- a/packages/playground/client/src/index.ts +++ b/packages/playground/client/src/index.ts @@ -67,7 +67,11 @@ export interface StartPlaygroundOptions { */ onBeforeBlueprint?: () => Promise; mounts?: Array; - shouldInstallWordPress?: boolean; + /** + * Whether to install WordPress. Value may be boolean or 'auto'. + * If 'auto', WordPress will be installed if it is not already installed. + */ + shouldInstallWordPress?: boolean | 'auto'; } /** diff --git a/packages/playground/remote/src/lib/index.ts b/packages/playground/remote/src/lib/index.ts index 518d8848a8..2f71323d63 100644 --- a/packages/playground/remote/src/lib/index.ts +++ b/packages/playground/remote/src/lib/index.ts @@ -1,5 +1,6 @@ export * from './boot-playground-remote'; export * from './playground-client'; +export { looksLikePlaygroundDirectory } from './worker-utils'; export { MinifiedWordPressVersions, MinifiedWordPressVersionsList, diff --git a/packages/playground/remote/src/lib/worker-thread.ts b/packages/playground/remote/src/lib/worker-thread.ts index d5100dd27a..574c588569 100644 --- a/packages/playground/remote/src/lib/worker-thread.ts +++ b/packages/playground/remote/src/lib/worker-thread.ts @@ -21,6 +21,7 @@ import { spawnHandlerFactory, backfillStaticFilesRemovedFromMinifiedBuild, hasCachedStaticFilesRemovedFromMinifiedBuild, + looksLikePlaygroundDirectory, } from './worker-utils'; import { EmscriptenDownloadMonitor } from '@php-wasm/progress'; import { createMemoizedFetch } from './create-memoized-fetch'; @@ -72,7 +73,7 @@ export type WorkerBootOptions = { scope: string; withNetworking: boolean; mounts?: Array; - shouldInstallWordPress?: boolean; + shouldInstallWordPress?: boolean | 'auto'; }; /** @inheritDoc PHPClient */ @@ -188,6 +189,24 @@ export class PlaygroundWorkerEndpoint extends PHPWorker { } try { + if (shouldInstallWordPress === 'auto') { + // Default to installing WordPress unless we detect + // it in one of the mounts. + shouldInstallWordPress = true; + + // NOTE: This check is insufficient if a complete WordPress + // installation is composed of multiple mounts. + for (const mount of mounts) { + const dirHandle = await directoryHandleFromMountDevice( + mount.device + ); + if (await looksLikePlaygroundDirectory(dirHandle)) { + shouldInstallWordPress = false; + break; + } + } + } + // Start downloading WordPress if needed let wordPressRequest = null; if (shouldInstallWordPress) { diff --git a/packages/playground/remote/src/lib/worker-utils.ts b/packages/playground/remote/src/lib/worker-utils.ts index 711d8ed931..ed37e106fe 100644 --- a/packages/playground/remote/src/lib/worker-utils.ts +++ b/packages/playground/remote/src/lib/worker-utils.ts @@ -243,3 +243,47 @@ export async function getWordPressStaticZipUrl(php: PHP) { } return joinPaths('/', staticAssetsDirectory, 'wordpress-static.zip'); } + +/** + * Check if the given directory handle directory is a Playground directory. + * + * @TODO: Create a shared package like @wp-playground/wordpress for such utilities + * and bring in the context detection logic from wp-now – only express it in terms of + * either abstract FS operations or isomorphic PHP FS operations. + * (we can't just use Node.js require('fs') in the browser, for example) + * + * @TODO: Reuse the "isWordPressInstalled" logic implemented in the boot protocol. + * Perhaps mount OPFS first, and only then check for the presence of the + * WordPress installation? Or, if not, perhaps implement a shared file access + * abstraction that can be used both with the PHP module and OPFS directory handles? + * + * @param dirHandle + */ +export async function looksLikePlaygroundDirectory( + dirHandle: FileSystemDirectoryHandle +) { + // Run this loop just to trigger an exception if the directory handle is no good. + // eslint-disable-next-line @typescript-eslint/no-unused-vars + for await (const _ of dirHandle.keys()) { + break; + } + + try { + /** + * Assume it's a Playground directory if these files exist: + * - wp-config.php + * - wp-content/database/.ht.sqlite + */ + await dirHandle.getFileHandle('wp-config.php', { create: false }); + const wpContent = await dirHandle.getDirectoryHandle('wp-content', { + create: false, + }); + const database = await wpContent.getDirectoryHandle('database', { + create: false, + }); + await database.getFileHandle('.ht.sqlite', { create: false }); + } catch (e) { + return false; + } + return true; +} diff --git a/packages/playground/website/src/lib/state/redux/boot-site-client.ts b/packages/playground/website/src/lib/state/redux/boot-site-client.ts index aabb8c3bbc..476a13ce8b 100644 --- a/packages/playground/website/src/lib/state/redux/boot-site-client.ts +++ b/packages/playground/website/src/lib/state/redux/boot-site-client.ts @@ -15,7 +15,10 @@ import { Blueprint, StepDefinition } from '@wp-playground/blueprints'; import { logger } from '@php-wasm/logger'; import { setupPostMessageRelay } from '@php-wasm/web'; import { startPlaygroundWeb } from '@wp-playground/client'; -import { PlaygroundClient } from '@wp-playground/remote'; +import { + PlaygroundClient, + looksLikePlaygroundDirectory, +} from '@wp-playground/remote'; import { getRemoteUrl } from '../../config'; import { setActiveModal, setActiveSiteError } from './slice-ui'; import { PlaygroundDispatch, PlaygroundReduxState } from './store'; @@ -72,7 +75,7 @@ export function bootSiteClient( let isWordPressInstalled = false; if (mountDescriptor) { try { - isWordPressInstalled = await playgroundAvailableInOpfs( + isWordPressInstalled = await looksLikePlaygroundDirectory( await directoryHandleFromMountDevice(mountDescriptor.device) ); } catch (e) { @@ -206,47 +209,3 @@ export function bootSiteClient( signal.onabort = null; }; } - -/** - * Check if the given directory handle directory is a Playground directory. - * - * @TODO: Create a shared package like @wp-playground/wordpress for such utilities - * and bring in the context detection logic from wp-now – only express it in terms of - * either abstract FS operations or isomorphic PHP FS operations. - * (we can't just use Node.js require('fs') in the browser, for example) - * - * @TODO: Reuse the "isWordPressInstalled" logic implemented in the boot protocol. - * Perhaps mount OPFS first, and only then check for the presence of the - * WordPress installation? Or, if not, perhaps implement a shared file access - * abstraction that can be used both with the PHP module and OPFS directory handles? - * - * @param dirHandle - */ -export async function playgroundAvailableInOpfs( - dirHandle: FileSystemDirectoryHandle -) { - // Run this loop just to trigger an exception if the directory handle is no good. - // eslint-disable-next-line @typescript-eslint/no-unused-vars - for await (const _ of dirHandle.keys()) { - break; - } - - try { - /** - * Assume it's a Playground directory if these files exist: - * - wp-config.php - * - wp-content/database/.ht.sqlite - */ - await dirHandle.getFileHandle('wp-config.php', { create: false }); - const wpContent = await dirHandle.getDirectoryHandle('wp-content', { - create: false, - }); - const database = await wpContent.getDirectoryHandle('database', { - create: false, - }); - await database.getFileHandle('.ht.sqlite', { create: false }); - } catch (e) { - return false; - } - return true; -}