Skip to content

Commit aa0cbd6

Browse files
Playground CLI Allow /wordpress subdirs to be mounted before WP install (#2382)
1 parent f1e1eaa commit aa0cbd6

File tree

1 file changed

+28
-29
lines changed
  • packages/playground/wordpress/src

1 file changed

+28
-29
lines changed

packages/playground/wordpress/src/index.ts

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import type { PHP, UniversalPHP } from '@php-wasm/universal';
22
import { joinPaths, phpVar } from '@php-wasm/util';
33
import { unzipFile, createMemoizedFetch } from '@wp-playground/common';
4+
import { logger } from '@php-wasm/logger';
5+
46
export {
57
bootWordPress,
68
bootRequestHandler,
@@ -529,20 +531,34 @@ export async function unzipWordPress(php: PHP, wpZip: File) {
529531
}
530532
}
531533

532-
if (
533-
php.isDir(php.documentRoot) &&
534-
isCleanDirContainingSiteMetadata(php.documentRoot, php)
535-
) {
536-
// We cannot mv the directory over a non-empty directory,
537-
// but we can move the children one by one.
538-
for (const file of php.listFiles(wpPath)) {
539-
const sourcePath = joinPaths(wpPath, file);
540-
const targetPath = joinPaths(php.documentRoot, file);
541-
php.mv(sourcePath, targetPath);
534+
const moveRecursively = (source: string, target: string, php: PHP) => {
535+
if (php.isDir(source) && php.isDir(target)) {
536+
// We cannot move a directory over another directory,
537+
// so we move the children one by one.
538+
for (const file of php.listFiles(source)) {
539+
const sourcePath = joinPaths(source, file);
540+
const targetPath = joinPaths(target, file);
541+
moveRecursively(sourcePath, targetPath, php);
542+
}
543+
} else {
544+
if (php.fileExists(target)) {
545+
// Refuse to overwrite existing files to avoid the chance of data loss.
546+
const wpPath = source.replace(
547+
/^\/tmp\/unzipped-wordpress\//,
548+
'/'
549+
);
550+
logger.warn(
551+
`Skipping ${wpPath} because something exists at the target path.`
552+
);
553+
return;
554+
}
555+
php.mv(source, target);
542556
}
557+
};
558+
moveRecursively(wpPath, php.documentRoot, php);
559+
// Remove any directories left because there were existing dirs at the target path.
560+
if (php.fileExists(wpPath)) {
543561
php.rmdir(wpPath, { recursive: true });
544-
} else {
545-
php.mv(wpPath, php.documentRoot);
546562
}
547563

548564
if (
@@ -558,23 +574,6 @@ export async function unzipWordPress(php: PHP, wpZip: File) {
558574
}
559575
}
560576

561-
function isCleanDirContainingSiteMetadata(path: string, php: PHP) {
562-
const files = php.listFiles(path);
563-
if (files.length === 0) {
564-
return true;
565-
}
566-
567-
if (
568-
files.length === 1 &&
569-
// TODO: use a constant from a site storage package
570-
files[0] === 'playground-site-metadata.json'
571-
) {
572-
return true;
573-
}
574-
575-
return false;
576-
}
577-
578577
const memoizedFetch = createMemoizedFetch(fetch);
579578

580579
/**

0 commit comments

Comments
 (0)