1
1
import type { PHP , UniversalPHP } from '@php-wasm/universal' ;
2
2
import { joinPaths , phpVar } from '@php-wasm/util' ;
3
3
import { unzipFile , createMemoizedFetch } from '@wp-playground/common' ;
4
+ import { logger } from '@php-wasm/logger' ;
5
+
4
6
export {
5
7
bootWordPress ,
6
8
bootRequestHandler ,
@@ -529,20 +531,34 @@ export async function unzipWordPress(php: PHP, wpZip: File) {
529
531
}
530
532
}
531
533
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
+ / ^ \/ t m p \/ u n z i p p e d - w o r d p r e s s \/ / ,
548
+ '/'
549
+ ) ;
550
+ logger . warn (
551
+ `Skipping ${ wpPath } because something exists at the target path.`
552
+ ) ;
553
+ return ;
554
+ }
555
+ php . mv ( source , target ) ;
542
556
}
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 ) ) {
543
561
php . rmdir ( wpPath , { recursive : true } ) ;
544
- } else {
545
- php . mv ( wpPath , php . documentRoot ) ;
546
562
}
547
563
548
564
if (
@@ -558,23 +574,6 @@ export async function unzipWordPress(php: PHP, wpZip: File) {
558
574
}
559
575
}
560
576
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
-
578
577
const memoizedFetch = createMemoizedFetch ( fetch ) ;
579
578
580
579
/**
0 commit comments