Skip to content
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

Studio: Fix site url after import #437

Merged
merged 9 commits into from
Aug 13, 2024
33 changes: 33 additions & 0 deletions src/lib/import-export/import/importers/importer.ts
Copy link
Contributor

Choose a reason for hiding this comment

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

Not a blocker for this PR but it would be great if we could cover this functionality with unit tests.

Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,42 @@ abstract class BaseImporter extends EventEmitter implements Importer {
}
}

await this.replaceSiteUrl( siteId );
this.emit( ImportEvents.IMPORT_DATABASE_COMPLETE );
}

private async replaceSiteUrl( siteId: string ) {
const server = SiteServer.get( siteId );
if ( ! server ) {
throw new Error( 'Site not found.' );
}

const { stdout: currentSiteUrl } = await server.executeWpCliCommand( `option get siteurl` );

if ( ! currentSiteUrl ) {
console.error( 'Failed to fetch site URL after import' );
return;
}

const studioUrl = `http://localhost:${ server.details.port }`;

const { stderr, exitCode } = await server.executeWpCliCommand(
`search-replace '${ currentSiteUrl.trim() }' '${ studioUrl.trim() }'`
);

if ( stderr ) {
console.error(
`Warning during replacing siteUrl ${ currentSiteUrl } -> ${ studioUrl }: ${ stderr }`
);
}

if ( exitCode ) {
console.error(
`Error during replacing siteUrl ${ currentSiteUrl } -> ${ studioUrl }, Exit Code: ${ exitCode }`
);
}
}

protected async safelyDeleteFile( filePath: string ): Promise< void > {
try {
await fsPromises.unlink( filePath );
Expand Down
Loading