-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathphp-server.js
59 lines (54 loc) · 1.74 KB
/
php-server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const { php } = require( './binaries' );
const { execFile } = require( 'node:child_process' );
const path = require( 'node:path' );
const { getWebRoot } = require( './utils' );
/**
* Finds an open local port between 8888 and 9999 (inclusive).
*
* @returns {Promise<number>}
*/
async function findPort ()
{
// Find an open port between 8888 and 9999. We need to dynamically import `get-port`
// because it's an ES6 module.
const {
default: getPort,
portNumbers,
} = await import( 'get-port' );
return await getPort({
port: portNumbers( 8888, 9999 ),
});
}
module.exports = async ( dir, win ) => {
const port = await findPort();
const url = `http://localhost:${port}`;
const caFile = path.join( __dirname, 'cacert.pem' );
// Start the built-in PHP web server.
const process = execFile(
php,
[
// Explicitly pass the cURL CA bundle so that HTTPS requests from Drupal can
// succeed on Windows.
// @see https://curl.haxx.se/ca/cacert.pem
'-d', `curl.cainfo="${caFile}"`,
'-S', url.substring( 7 ),
'.ht.router.php',
],
{
cwd: getWebRoot( dir ),
},
);
// When the server starts, let the renderer know we're up and running.
const {
stderr: serverOutput,
} = process;
// This callback must be in its own variable so it can refer to itself internally.
const checkForServerStart = ( chunk ) => {
if ( chunk.toString().includes( `(${url}) started` ) ) {
win?.send( 'ready', url );
serverOutput.off( 'data', checkForServerStart );
}
};
serverOutput.on( 'data', checkForServerStart );
return { url, process };
};