-
Notifications
You must be signed in to change notification settings - Fork 357
Playground CLI Allow /wordpress subdirs to be mounted before WP install #2382
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ab773cd
Allow /wordpress subdirs to be mounted before WP install
brandonpayton 47a73e6
Refuse to overwrite existing files to avoid the chance of data loss
brandonpayton 50ef850
Remove unnecessary try/catch
brandonpayton 495b11d
Address test failure and hopefully simplify
brandonpayton 9e4b442
Merge branch 'trunk' into fix-unzip-wp-into-non-empty-directory
brandonpayton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| import type { PHP, UniversalPHP } from '@php-wasm/universal'; | ||
| import { joinPaths, phpVar } from '@php-wasm/util'; | ||
| import { unzipFile, createMemoizedFetch } from '@wp-playground/common'; | ||
| import { logger } from '@php-wasm/logger'; | ||
|
|
||
| export { | ||
| bootWordPress, | ||
| bootRequestHandler, | ||
|
|
@@ -529,20 +531,60 @@ export async function unzipWordPress(php: PHP, wpZip: File) { | |
| } | ||
| } | ||
|
|
||
| if ( | ||
| php.isDir(php.documentRoot) && | ||
| isCleanDirContainingSiteMetadata(php.documentRoot, php) | ||
| ) { | ||
| // We cannot mv the directory over a non-empty directory, | ||
| // but we can move the children one by one. | ||
| for (const file of php.listFiles(wpPath)) { | ||
| const sourcePath = joinPaths(wpPath, file); | ||
| const targetPath = joinPaths(php.documentRoot, file); | ||
| php.mv(sourcePath, targetPath); | ||
| const moveRecursively = (source: string, target: string, php: PHP) => { | ||
| if (php.fileExists(target)) { | ||
| /* | ||
| * Something exists at the target path. | ||
| * Let's check to make sure we aren't copying conflicting types. | ||
| * | ||
| * In this context, if the source path points to a file and the | ||
| * target path points to a directory, we do not intend for the file | ||
| * to be moved into the directory. | ||
| */ | ||
|
|
||
| if (!php.isDir(source) && php.isDir(target)) { | ||
| throw new Error( | ||
| `The target ${target} is a directory but the source ${source} is not. This is not supported.` | ||
| ); | ||
| } | ||
| if (php.isDir(source) && !php.isDir(target)) { | ||
| throw new Error( | ||
| `The source ${source} is a directory but the target ${target} is not. This is not supported.` | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| if (isNonEmptyDir(target, php)) { | ||
| // We cannot move a directory over a non-empty directory, | ||
| // so we move the children one by one. | ||
| for (const file of php.listFiles(source)) { | ||
| const sourcePath = joinPaths(source, file); | ||
| const targetPath = joinPaths(target, file); | ||
| moveRecursively(sourcePath, targetPath, php); | ||
| } | ||
| } else { | ||
| if (php.fileExists(target) && !php.isDir(target)) { | ||
| // Refuse to overwrite existing files to avoid the chance of data loss. | ||
| const wpPath = source.replace( | ||
| /^\/tmp\/unzipped-wordpress\//, | ||
| '/' | ||
| ); | ||
| logger.warn( | ||
| `Skipping ${wpPath} because a file exists at the target path.` | ||
| ); | ||
| return; | ||
| } | ||
|
||
| php.mv(source, target); | ||
| } | ||
| }; | ||
| try { | ||
| moveRecursively(wpPath, php.documentRoot, php); | ||
| // Remove any directories left because there were existing dirs at the target path. | ||
| if (php.fileExists(wpPath)) { | ||
| php.rmdir(wpPath, { recursive: true }); | ||
| } | ||
| php.rmdir(wpPath, { recursive: true }); | ||
| } else { | ||
| php.mv(wpPath, php.documentRoot); | ||
| } catch (e) { | ||
| throw e; | ||
| } | ||
|
|
||
| if ( | ||
|
|
@@ -558,21 +600,12 @@ export async function unzipWordPress(php: PHP, wpZip: File) { | |
| } | ||
| } | ||
|
|
||
| function isCleanDirContainingSiteMetadata(path: string, php: PHP) { | ||
| const files = php.listFiles(path); | ||
| if (files.length === 0) { | ||
| return true; | ||
| function isNonEmptyDir(path: string, php: PHP) { | ||
| if (!php.isDir(path)) { | ||
| return false; | ||
| } | ||
|
|
||
| if ( | ||
| files.length === 1 && | ||
| // TODO: use a constant from a site storage package | ||
| files[0] === 'playground-site-metadata.json' | ||
| ) { | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| const files = php.listFiles(path); | ||
| return files.length > 0; | ||
| } | ||
|
|
||
| const memoizedFetch = createMemoizedFetch(fetch); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
So far, I've avoided moving this into FS helpers because it is an odd sort of operation:
Move a single directory, or if a directory already exists at the target path, move its children.
If there are existing files at the target paths, those files will be left intact, and the source files will not be moved. (But they will be deleted as leftovers of the unzipped WP dir later on.)