Skip to content

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

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 48 additions & 27 deletions packages/playground/wordpress/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,20 +529,50 @@ 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);
// TODO: Consider moving this to a shared location and add a unit test if this isn't going to be immediately replaced by Blueprints v2 execution.
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 {
php.mv(source, target);
}
php.rmdir(wpPath, { recursive: true });
} else {
php.mv(wpPath, php.documentRoot);
};
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 });
}
} catch (e) {
throw e;
}
Comment on lines +533 to 576
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would fit nicely into FSHelpers, similarly to how we have copyRecursive.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And mount tests have a bunch of FS related tests, so they could be a good fit for adding tests.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


if (
Expand All @@ -558,21 +588,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);
Expand Down
Loading