Skip to content

Commit

Permalink
Provision server with node
Browse files Browse the repository at this point in the history
  • Loading branch information
WunderBart committed Oct 3, 2024
1 parent 49cfa8d commit 5603487
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 10 deletions.
12 changes: 2 additions & 10 deletions .github/workflows/end2end-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,8 @@ jobs:
npx playwright install chromium firefox webkit --with-deps
- name: Start the server
run: |
npx @wp-playground/cli server \
--wp=nightly \
--mount=./:/wordpress/wp-content/plugins/gutenberg \
--mount=./test/emptytheme:/wordpress/wp-content/themes/emptytheme \
--mount=./test/gutenberg-test-themes:/wordpress/wp-content/themes/gutenberg-test-themes \
--mount=./packages/e2e-tests/mu-plugins:/wordpress/wp-content/mu-plugins \
--mount=./packages/e2e-tests/plugins:/wordpress/wp-content/plugins/gutenberg-test-plugins \
--blueprint=./test/e2e/playground.blueprint.json \
& while ! (jobs -p | xargs -r tail -n 0 -f 2>/dev/null | grep -q "WordPress is running"); do sleep 1; done
run: node playground-server.js

- name: Run the tests
env:
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: 1
Expand Down
76 changes: 76 additions & 0 deletions playground-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* External dependencies
*/
const { spawn } = require( 'child_process' );
const fs = require( 'fs' );

// Function to start the server and wait until it is provisioned
function startWPServer() {
return new Promise( ( resolve, reject ) => {
const wpServer = spawn(
'npx',
[
'@wp-playground/cli',
'server',
'--wp=nightly',
'--mount=./:/wordpress/wp-content/plugins/gutenberg',
'--mount=./test/emptytheme:/wordpress/wp-content/themes/emptytheme',
'--mount=./test/gutenberg-test-themes:/wordpress/wp-content/themes/gutenberg-test-themes',
'--mount=./packages/e2e-tests/mu-plugins:/wordpress/wp-content/mu-plugins',
'--mount=./packages/e2e-tests/plugins:/wordpress/wp-content/plugins/gutenberg-test-plugins',
'--blueprint=./test/e2e/playground.blueprint.json',
],
{
detached: true,
stdio: [ 'ignore', 'pipe', 'pipe' ], // Capture stdout and stderr
}
);

// Write the process ID to a file for later management
fs.writeFileSync( 'wp-server.pid', wpServer.pid.toString(), 'utf-8' );

// Listen to stdout and wait for the "WordPress is running" message
wpServer.stdout.on( 'data', ( data ) => {
const output = data.toString();
// eslint-disable-next-line no-console
console.log( output ); // Optional: log the output

if (
output.includes(
'WordPress is running on http://127.0.0.1:9400'
)
) {
resolve( wpServer ); // Resolve the promise when server is ready
}
} );

// Handle errors from stderr
wpServer.stderr.on( 'data', ( data ) => {
reject( new Error( `Server error: ${ data.toString() }` ) );
} );

// Handle close event
wpServer.on( 'close', ( code ) => {
if ( code !== 0 ) {
reject(
new Error(
`WP Playground server exited with code ${ code }`
)
);
}
} );

// Detach the process
wpServer.unref();
} );
}

// Usage of the promisified function
startWPServer()
.then( () => {
process.exit( 0 ); // Exit the Node.js process once the server is provisioned
} )
.catch( ( error ) => {
// eslint-disable-next-line no-console
console.error( error ); // Log the error
} );

0 comments on commit 5603487

Please sign in to comment.