Skip to content

Commit 5603487

Browse files
committed
Provision server with node
1 parent 49cfa8d commit 5603487

2 files changed

Lines changed: 78 additions & 10 deletions

File tree

‎.github/workflows/end2end-test.yml‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,8 @@ jobs:
4242
npx playwright install chromium firefox webkit --with-deps
4343
4444
- name: Start the server
45-
run: |
46-
npx @wp-playground/cli server \
47-
--wp=nightly \
48-
--mount=./:/wordpress/wp-content/plugins/gutenberg \
49-
--mount=./test/emptytheme:/wordpress/wp-content/themes/emptytheme \
50-
--mount=./test/gutenberg-test-themes:/wordpress/wp-content/themes/gutenberg-test-themes \
51-
--mount=./packages/e2e-tests/mu-plugins:/wordpress/wp-content/mu-plugins \
52-
--mount=./packages/e2e-tests/plugins:/wordpress/wp-content/plugins/gutenberg-test-plugins \
53-
--blueprint=./test/e2e/playground.blueprint.json \
54-
& while ! (jobs -p | xargs -r tail -n 0 -f 2>/dev/null | grep -q "WordPress is running"); do sleep 1; done
45+
run: node playground-server.js
46+
5547
- name: Run the tests
5648
env:
5749
PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: 1

‎playground-server.js‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)