Skip to content

Commit

Permalink
try using spawn
Browse files Browse the repository at this point in the history
  • Loading branch information
WunderBart committed Jun 4, 2024
1 parent 78d3b3b commit 9562fca
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 24 deletions.
23 changes: 16 additions & 7 deletions bin/plugin/commands/performance.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ function printStats( m, s ) {
*/
async function runTestSuite( testSuite, testRunnerDir, runKey ) {
await runShellScript(
`npm run test:performance -- ${ testSuite }`,
'npm',
[ 'run', 'test:performance', '--', testSuite ],
testRunnerDir,
{
...process.env,
Expand Down Expand Up @@ -329,7 +330,7 @@ async function runPerformanceTests( branches, options ) {
const testRunnerDir = path.join( baseDir + '/tests' );

logAtIndent( 2, 'Copying source to:', formats.success( testRunnerDir ) );
await runShellScript( `cp -R ${ sourceDir } ${ testRunnerDir }` );
await runShellScript( 'cp', [ '-R', sourceDir, testRunnerDir ] );

logAtIndent(
2,
Expand All @@ -341,7 +342,11 @@ async function runPerformanceTests( branches, options ) {

logAtIndent( 2, 'Installing dependencies and building' );
await runShellScript(
`bash -c "source $HOME/.nvm/nvm.sh && nvm install && npm ci && npx playwright install chromium --with-deps && npm run build:packages"`,
'bash',
[
'-c',
'"source $HOME/.nvm/nvm.sh && nvm install && npm ci && npx playwright install chromium --with-deps && npm run build:packages"',
],
testRunnerDir
);

Expand Down Expand Up @@ -376,15 +381,19 @@ async function runPerformanceTests( branches, options ) {
const buildDir = path.join( envDir, 'plugin' );

logAtIndent( 3, 'Copying source to:', formats.success( buildDir ) );
await runShellScript( `cp -R ${ sourceDir } ${ buildDir }` );
await runShellScript( 'cp', [ '-R', sourceDir, buildDir ] );

logAtIndent( 3, 'Checking out:', formats.success( branch ) );
// @ts-ignore
await SimpleGit( buildDir ).raw( 'checkout', branch );

logAtIndent( 3, 'Installing dependencies and building' );
await runShellScript(
`bash -c "source $HOME/.nvm/nvm.sh && nvm install && npm ci && npm run build"`,
'bash',
[
'-c',
'"source $HOME/.nvm/nvm.sh && nvm install && npm ci && npm run build"',
],
buildDir
);

Expand Down Expand Up @@ -479,13 +488,13 @@ async function runPerformanceTests( branches, options ) {
const envDir = branchDirs[ branch ];

logAtIndent( 3, 'Starting environment' );
await runShellScript( `${ wpEnvPath } start`, envDir );
await runShellScript( wpEnvPath, [ 'start' ], envDir );

logAtIndent( 3, 'Running tests' );
await runTestSuite( testSuite, testRunnerDir, runKey );

logAtIndent( 3, 'Stopping environment' );
await runShellScript( `${ wpEnvPath } stop`, envDir );
await runShellScript( wpEnvPath, [ 'stop' ], envDir );
}
}
}
Expand Down
76 changes: 59 additions & 17 deletions bin/plugin/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
// @ts-ignore
const inquirer = require( 'inquirer' );
const fs = require( 'fs' );
const { promisify } = require( 'util' );
const childProcess = require( 'child_process' );
const { spawn } = require( 'child_process' );
const { v4: uuid } = require( 'uuid' );
const path = require( 'path' );
const os = require( 'os' );
Expand All @@ -20,22 +19,65 @@ const { log, formats } = require( './logger' );
*
* @typedef {NodeJS.ProcessEnv} Env
*
* @param {string} script Script to run.
* @param {string=} cwd Working directory.
* @param {Env=} env Additional environment variables to pass to the script.
* @param {string} command
* @param {string[]} args
* @param {string=} cwd Current working directory.
* @param {Env=} env Environment variables.
* @param {boolean=} shell Use shell.
*/
async function runShellScript( script, cwd, env = {} ) {
const execPromisified = promisify( childProcess.exec );

return await execPromisified( script, {
cwd,
env: {
NO_CHECKS: 'true',
PATH: process.env.PATH,
HOME: process.env.HOME,
USER: process.env.USER,
...env,
},
async function runShellScript( command, args, cwd, env = {}, shell = false ) {
if ( ! command ) {
throw new Error( 'No command provided' );
}

return new Promise( ( resolve, reject ) => {
const child = spawn( command, args, {
cwd,
env: {
NO_CHECKS: 'true',
PATH: process.env.PATH,
HOME: process.env.HOME,
USER: process.env.USER,
...env,
},
shell: command === 'cp' ? true : shell,
stdio: 'inherit',
} );

let stdout = '';
let stderr = '';

if ( child.stdout ) {
child.stdout.on( 'data', ( data ) => {
const dataStr = data.toString();
stdout += dataStr;
process.stdout.write( dataStr ); // Print to console in real-time
} );
}

if ( child.stderr ) {
child.stderr.on( 'data', ( data ) => {
const dataStr = data.toString();
stderr += dataStr;
process.stderr.write( dataStr ); // Print to console in real-time
} );
}

child.on( 'close', ( code ) => {
if ( code === 0 ) {
resolve( { stdout, stderr } );
} else {
reject(
new Error(
`Process exited with code ${ code }: ${ stderr }`
)
);
}
} );

child.on( 'error', ( error ) => {
reject( error );
} );
} );
}

Expand Down

0 comments on commit 9562fca

Please sign in to comment.