Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 16 additions & 9 deletions packages/common/lib/mu-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,25 @@ async function getExistingNativePhpMuPluginsDir(
return null;
}

const expectedFiles = getStandardMuPlugins( options )
.map( ( plugin ) => plugin.filename )
.sort();
const actualFiles = existingFiles.filter( ( file ) => file.endsWith( '.php' ) ).sort();

if (
expectedFiles.length !== actualFiles.length ||
expectedFiles.some( ( filename, index ) => filename !== actualFiles[ index ] )
) {
const expectedPlugins = getStandardMuPlugins( options );
const actualFiles = existingFiles.filter( ( file ) => file.endsWith( '.php' ) );

if ( expectedPlugins.length !== actualFiles.length ) {
return null;
}

for ( const plugin of expectedPlugins ) {
let content: string;
try {
content = await readFile( path.join( muPluginsDir, plugin.filename ), 'utf8' );
} catch {
return null;
}
if ( content !== plugin.content ) {
return null;
}
}

return muPluginsDir;
}

Expand Down
21 changes: 21 additions & 0 deletions packages/common/lib/tests/mu-plugins.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,27 @@ describe( 'writeStudioMuPluginsForNativePhpRuntime', () => {
expect( generatedPlugins ).not.toContain( '0-disable-auto-updates.php' );
} );

it( 'should reuse the existing mu-plugins directory when contents are up to date', async () => {
const firstDir = await writeStudioMuPluginsForNativePhpRuntime( sitePath, false );
const secondDir = await writeStudioMuPluginsForNativePhpRuntime( sitePath, false );

expect( secondDir ).toBe( firstDir );
} );

it( 'should regenerate mu-plugins when an existing file has stale content', async () => {
const firstDir = await writeStudioMuPluginsForNativePhpRuntime( sitePath, false );

const pluginFilename = '0-deactivate-jetpack-modules.php';
const expectedContent = await readFile( join( firstDir, pluginFilename ), 'utf8' );
writeFileSync( join( firstDir, pluginFilename ), '<?php // stale content from older Studio' );

const secondDir = await writeStudioMuPluginsForNativePhpRuntime( sitePath, false );
const regeneratedContent = await readFile( join( secondDir, pluginFilename ), 'utf8' );

expect( secondDir ).not.toBe( firstDir );
expect( regeneratedContent ).toBe( expectedContent );
} );

it( 'should disable Jetpack modules that affect local development', async () => {
await writeStudioMuPluginsForNativePhpRuntime( sitePath, false );

Expand Down
Loading