-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
49cfa8d
commit 5603487
Showing
2 changed files
with
78 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} ); |