Skip to content

Commit b8b1425

Browse files
committed
Use spawnSync to ensure args with spaces are passed properly
1 parent 7a66e62 commit b8b1425

File tree

3 files changed

+58
-10
lines changed

3 files changed

+58
-10
lines changed

tools/local-env/scripts/docker.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const dotenv = require( 'dotenv' );
44
const dotenvExpand = require( 'dotenv-expand' );
5-
const { execSync } = require( 'child_process' );
5+
const { spawnSync } = require( 'child_process' );
66
const local_env_utils = require( './utils' );
77

88
dotenvExpand.expand( dotenv.config() );
@@ -17,7 +17,15 @@ if (process.argv.includes('--coverage-html')) {
1717
// This try-catch prevents the superfluous Node.js debugging information from being shown if the command fails.
1818
try {
1919
// Execute any Docker compose command passed to this script.
20-
execSync( 'docker compose ' + composeFiles + ' ' + process.argv.slice( 2 ).join( ' ' ), { stdio: 'inherit' } );
20+
spawnSync(
21+
'docker',
22+
[
23+
'compose',
24+
...composeFiles.map( ( composeFile ) => [ '-f', composeFile ] ).flat(),
25+
...process.argv.slice( 2 )
26+
],
27+
{ stdio: 'inherit' }
28+
);
2129
} catch ( error ) {
2230
process.exit( 1 );
2331
}

tools/local-env/scripts/start.js

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const dotenv = require( 'dotenv' );
44
const dotenvExpand = require( 'dotenv-expand' );
5-
const { execSync } = require( 'child_process' );
5+
const { execSync, spawnSync } = require( 'child_process' );
66
const local_env_utils = require( './utils' );
77
const { constants, copyFile } = require( 'node:fs' );
88

@@ -34,15 +34,34 @@ const containers = [ 'wordpress-develop', 'cli' ];
3434
if ( process.env.LOCAL_PHP_MEMCACHED === 'true' ) {
3535
containers.push( 'memcached' );
3636
}
37-
execSync( `docker compose ${composeFiles} up --quiet-pull -d ${containers.join( ' ' )}`, { stdio: 'inherit' } );
37+
38+
spawnSync(
39+
'docker',
40+
[
41+
'compose',
42+
...composeFiles.map( ( composeFile ) => [ '-f', composeFile ] ).flat(),
43+
'up',
44+
'--quiet-pull',
45+
'-d',
46+
...containers,
47+
],
48+
{ stdio: 'inherit' }
49+
);
3850

3951
// If Docker Toolbox is being used, we need to manually forward LOCAL_PORT to the Docker VM.
4052
if ( process.env.DOCKER_TOOLBOX_INSTALL_PATH ) {
4153
// VBoxManage is added to the PATH on every platform except Windows.
4254
const vboxmanage = process.env.VBOX_MSI_INSTALL_PATH ? `${ process.env.VBOX_MSI_INSTALL_PATH }/VBoxManage` : 'VBoxManage';
4355

4456
// Check if the port forwarding is already configured for this port.
45-
const vminfoBuffer = execSync( `"${ vboxmanage }" showvminfo "${ process.env.DOCKER_MACHINE_NAME }" --machinereadable` );
57+
const vminfoBuffer = spawnSync(
58+
vboxmanage,
59+
[
60+
'showvminfo',
61+
process.env.DOCKER_MACHINE_NAME,
62+
'--machinereadable'
63+
]
64+
).stdout;
4665
const vminfo = vminfoBuffer.toString().split( /[\r\n]+/ );
4766

4867
vminfo.forEach( ( info ) => {
@@ -56,10 +75,29 @@ if ( process.env.DOCKER_TOOLBOX_INSTALL_PATH ) {
5675

5776
// Delete rules that are using the port we need.
5877
if ( rule[ 3 ] === process.env.LOCAL_PORT || rule[ 5 ] === process.env.LOCAL_PORT ) {
59-
execSync( `"${ vboxmanage }" controlvm "${ process.env.DOCKER_MACHINE_NAME }" natpf1 delete ${ rule[ 0 ] }`, { stdio: 'inherit' } );
78+
spawnSync(
79+
vboxmanage,
80+
[
81+
'controlvm',
82+
process.env.DOCKER_MACHINE_NAME,
83+
'natpf1',
84+
'delete',
85+
rule[ 0 ]
86+
],
87+
{ stdio: 'inherit' }
88+
);
6089
}
6190
} );
6291

6392
// Add our port forwarding rule.
64-
execSync( `"${ vboxmanage }" controlvm "${ process.env.DOCKER_MACHINE_NAME }" natpf1 "tcp-port${ process.env.LOCAL_PORT },tcp,127.0.0.1,${ process.env.LOCAL_PORT },,${ process.env.LOCAL_PORT }"`, { stdio: 'inherit' } );
93+
spawnSync(
94+
vboxmanage,
95+
[
96+
'controlvm',
97+
process.env.DOCKER_MACHINE_NAME,
98+
'natpf1',
99+
`tcp-port${ process.env.LOCAL_PORT },tcp,127.0.0.1,${ process.env.LOCAL_PORT },,${ process.env.LOCAL_PORT }`
100+
],
101+
{ stdio: 'inherit' }
102+
);
65103
}

tools/local-env/scripts/utils.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ const local_env_utils = {
1212
*
1313
* When PHP 7.2 or 7.3 is used in combination with MySQL 8.4, an override file will also be returned to ensure
1414
* that the mysql_native_password plugin authentication plugin is on and available for use.
15+
*
16+
* @return {string[]} Compose files.
1517
*/
1618
get_compose_files: function() {
17-
var composeFiles = '-f docker-compose.yml';
19+
const composeFiles = [ 'docker-compose.yml' ];
1820

1921
if ( existsSync( 'docker-compose.override.yml' ) ) {
20-
composeFiles = composeFiles + ' -f docker-compose.override.yml';
22+
composeFiles.push( 'docker-compose.override.yml' );
2123
}
2224

2325
if ( process.env.LOCAL_DB_TYPE !== 'mysql' ) {
@@ -30,7 +32,7 @@ const local_env_utils = {
3032

3133
// PHP 7.2/7.3 in combination with MySQL 8.4 requires additional configuration to function properly.
3234
if ( process.env.LOCAL_DB_VERSION === '8.4' ) {
33-
composeFiles = composeFiles + ' -f tools/local-env/old-php-mysql-84.override.yml';
35+
composeFiles.push( 'tools/local-env/old-php-mysql-84.override.yml' );
3436
}
3537

3638
return composeFiles;

0 commit comments

Comments
 (0)