-
Notifications
You must be signed in to change notification settings - Fork 274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support installing WP as needed in Playground remote #1841
base: trunk
Are you sure you want to change the base?
Changes from all commits
9ebeb51
2f53431
ccde6fe
5d555f9
51dd315
3b1cffd
3b56699
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,10 @@ import { | |
sqliteDatabaseIntegrationModuleDetails, | ||
MinifiedWordPressVersionsList, | ||
} from '@wp-playground/wordpress-builds'; | ||
import { directoryHandleFromMountDevice } from '@wp-playground/storage'; | ||
import { | ||
directoryHandleFromMountDevice, | ||
fileExistsUnderDirectoryHandle, | ||
} from '@wp-playground/storage'; | ||
import { randomString } from '@php-wasm/util'; | ||
import { | ||
spawnHandlerFactory, | ||
|
@@ -45,6 +48,7 @@ import { | |
bootWordPress, | ||
getFileNotFoundActionForWordPress, | ||
getLoadedWordPressVersion, | ||
looksLikePlaygroundDirectory, | ||
} from '@wp-playground/wordpress'; | ||
import { wpVersionToStaticAssetsDirectory } from '@wp-playground/wordpress-builds'; | ||
import { logger } from '@php-wasm/logger'; | ||
|
@@ -72,7 +76,7 @@ export type WorkerBootOptions = { | |
scope: string; | ||
withNetworking: boolean; | ||
mounts?: Array<MountDescriptor>; | ||
shouldInstallWordPress?: boolean; | ||
shouldInstallWordPress?: boolean | 'auto'; | ||
}; | ||
|
||
/** @inheritDoc PHPClient */ | ||
|
@@ -188,6 +192,29 @@ export class PlaygroundWorkerEndpoint extends PHPWorker { | |
} | ||
|
||
try { | ||
if (shouldInstallWordPress === 'auto') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How could we make this reusable for Playground CLI? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Both Playground remote and CLI leverage The CLI's Thanks for drawing my attention to this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I was thinking of saying the following:
But if we want to allow parallel downloads of PHP and WP resources in a web context, we need to check before PHP has been downloaded and loaded. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made a reusable /**
* Check if the given directory handle directory is a Playground directory.
*
* @param fileExists Function A function that checks if a file exists relative to an assumed directory.
* @returns Promise<boolean> Whether the directory looks like a Playground directory.
*/
export async function looksLikePlaygroundDirectory(
fileExists: (relativePath: string) => Promise<boolean>
) {
const results = await Promise.all(
['wp-config.php', 'wp-content/database/.ht.sqlite'].map(fileExists)
);
return results.every(Boolean);
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's something similar in wp-now, you may want to review their "WordPress mode". One assumption this implementation makes is that we're running on SQLite whereas Playground CLI supports MySQL, too |
||
// 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 | ||
); | ||
const fileExistsUnderMount = (relativePath: string) => | ||
fileExistsUnderDirectoryHandle(dirHandle, relativePath); | ||
|
||
if ( | ||
await looksLikePlaygroundDirectory(fileExistsUnderMount) | ||
) { | ||
shouldInstallWordPress = false; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
// Start downloading WordPress if needed | ||
let wordPressRequest = null; | ||
if (shouldInstallWordPress) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like
if-not-installed
better thanauto
but is it really necessary to define a string default here? Can't we do a check againstundefined
to infer "auto" behavior & prevent mixing of type?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ashfame which of these reads clearer to you?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I follow. What I meant was keeping
shouldInstallWordPress?: boolean
as it is. When that property is explicitly specified, do what's being asked (Current behavior) and when not defined at all, just assume the "auto" behavior i.e. install if no WP files are found, otherwise let it be.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, that makes sense and sounds convenient for omitting the
shouldInstallWordPress
option:I was just pointing out that it reads awkward when you want to explicitly declare the
shouldInstallWordPress
key and need to explicitly type inundefined
as a value:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, a user should never have to explicitly pass
undefined
. But I don't see why someone would have to do that with my suggestion? IF they need to specify it, they should know whether it's a true or false. And if they don't they should omit and let the default behavior take place, no?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not uncommon to define object literals like this:
or like this:
or like this:
In which case the
undefined
literal would show up somewhere.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure! Implementation details aside, I think we are on the same page as to how it should behave, which is: Do as explicitly asked and figure out if we need to, as the default behavior. Thought I will reiterate it, in case this PR was waiting on me to close the loop of our conversation. cc: @brandonpayton