|
| 1 | +/** |
| 2 | + * External dependencies |
| 3 | + */ |
| 4 | +const { spawn } = require( 'child_process' ); |
| 5 | +const fs = require( 'fs' ); |
| 6 | + |
| 7 | +// Function to start the server and wait until it is provisioned |
| 8 | +function startWPServer() { |
| 9 | + return new Promise( ( resolve, reject ) => { |
| 10 | + const wpServer = spawn( |
| 11 | + 'npx', |
| 12 | + [ |
| 13 | + '@wp-playground/cli', |
| 14 | + 'server', |
| 15 | + '--wp=nightly', |
| 16 | + '--mount=./:/wordpress/wp-content/plugins/gutenberg', |
| 17 | + '--mount=./test/emptytheme:/wordpress/wp-content/themes/emptytheme', |
| 18 | + '--mount=./test/gutenberg-test-themes:/wordpress/wp-content/themes/gutenberg-test-themes', |
| 19 | + '--mount=./packages/e2e-tests/mu-plugins:/wordpress/wp-content/mu-plugins', |
| 20 | + '--mount=./packages/e2e-tests/plugins:/wordpress/wp-content/plugins/gutenberg-test-plugins', |
| 21 | + '--blueprint=./test/e2e/playground.blueprint.json', |
| 22 | + ], |
| 23 | + { |
| 24 | + detached: true, |
| 25 | + stdio: [ 'ignore', 'pipe', 'pipe' ], // Capture stdout and stderr |
| 26 | + } |
| 27 | + ); |
| 28 | + |
| 29 | + // Write the process ID to a file for later management |
| 30 | + fs.writeFileSync( 'wp-server.pid', wpServer.pid.toString(), 'utf-8' ); |
| 31 | + |
| 32 | + // Listen to stdout and wait for the "WordPress is running" message |
| 33 | + wpServer.stdout.on( 'data', ( data ) => { |
| 34 | + const output = data.toString(); |
| 35 | + // eslint-disable-next-line no-console |
| 36 | + console.log( output ); // Optional: log the output |
| 37 | + |
| 38 | + if ( |
| 39 | + output.includes( |
| 40 | + 'WordPress is running on http://127.0.0.1:9400' |
| 41 | + ) |
| 42 | + ) { |
| 43 | + resolve( wpServer ); // Resolve the promise when server is ready |
| 44 | + } |
| 45 | + } ); |
| 46 | + |
| 47 | + // Handle errors from stderr |
| 48 | + wpServer.stderr.on( 'data', ( data ) => { |
| 49 | + reject( new Error( `Server error: ${ data.toString() }` ) ); |
| 50 | + } ); |
| 51 | + |
| 52 | + // Handle close event |
| 53 | + wpServer.on( 'close', ( code ) => { |
| 54 | + if ( code !== 0 ) { |
| 55 | + reject( |
| 56 | + new Error( |
| 57 | + `WP Playground server exited with code ${ code }` |
| 58 | + ) |
| 59 | + ); |
| 60 | + } |
| 61 | + } ); |
| 62 | + |
| 63 | + // Detach the process |
| 64 | + wpServer.unref(); |
| 65 | + } ); |
| 66 | +} |
| 67 | + |
| 68 | +// Usage of the promisified function |
| 69 | +startWPServer() |
| 70 | + .then( () => { |
| 71 | + process.exit( 0 ); // Exit the Node.js process once the server is provisioned |
| 72 | + } ) |
| 73 | + .catch( ( error ) => { |
| 74 | + // eslint-disable-next-line no-console |
| 75 | + console.error( error ); // Log the error |
| 76 | + } ); |
0 commit comments