Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions cli/__mocks__/pm2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class API {
connect( callback: ( error: Error | null ) => void ) {
setTimeout( () => {
callback( null );
}, 0 );
}
disconnect( callback: ( error: Error | null ) => void ) {
setTimeout( () => {
callback( null );
}, 0 );
}
list( callback: ( error: Error | null, processes: any[] ) => void ) {
setTimeout( () => {
callback( null, [] );
}, 0 );
}
launchBus( callback: ( error: Error | null, bus: any ) => void ) {
setTimeout( () => {
callback( null, {} );
}, 0 );
}
sendDataToProcessId( id: string, data: any, callback: ( error: Error | null ) => void ) {
setTimeout( () => {
callback( null );
}, 0 );
}
start( config: any, callback: ( error: Error | null, apps: any[] ) => void ) {
setTimeout( () => {
callback( null, [] );
}, 0 );
}
delete( callback: ( error: Error | null ) => void ) {
setTimeout( () => {
callback( null );
}, 0 );
}
}

exports.custom = API;
46 changes: 6 additions & 40 deletions cli/lib/pm2-manager.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import fs from 'fs';
import os from 'os';
import path from 'path';
import { cacheFunctionTTL } from 'common/lib/cache-function-ttl';
import { StartOptions } from 'pm2';
import { custom as PM2, StartOptions } from 'pm2';
import { getAppdataPath } from 'cli/lib/appdata';
import { ProcessDescription } from 'cli/lib/types/pm2';
import { ManagerMessage } from './types/wordpress-server-ipc';
Expand All @@ -15,40 +14,7 @@ const DAEMON_TIMEOUT = 10000;
// This ensures all Studio CLI commands use the same PM2 daemon
const STUDIO_PM2_HOME = path.join( os.homedir(), '.studio', 'pm2' );

process.env.PM2_HOME = STUDIO_PM2_HOME;

function resolvePm2(): typeof import('pm2') {
try {
return require( 'pm2' );
} catch ( error ) {
const possiblePaths: string[] = [
path.join( __dirname, 'node_modules', 'pm2' ),
path.join( path.dirname( __dirname ), 'node_modules', 'pm2' ),
path.join( __dirname, '..', 'node_modules', 'pm2' ),
path.join( __dirname, '..', '..', 'node_modules', 'pm2' ),
path.join( __dirname, '..', '..', '..', 'node_modules', 'pm2' ),
path.resolve( process.cwd(), 'node_modules', 'pm2' ),
];

for ( const pm2Path of possiblePaths ) {
if ( fs.existsSync( pm2Path ) ) {
try {
return require( pm2Path );
} catch {
continue;
}
}
}

throw new Error(
`pm2 module not found. Please ensure pm2 is installed in the CLI dependencies. Tried paths: ${ possiblePaths.join(
', '
) }`
);
}
}

const pm2 = resolvePm2();
const pm2 = new PM2( { pm2_home: STUDIO_PM2_HOME } );

let isConnected = false;

Expand Down Expand Up @@ -101,7 +67,7 @@ const listProcesses = cacheFunctionTTL( () => {

const processDescriptions = ( processes || [] ).map( ( p ) => ( {
name: p.name || '',
pmId: p.pm_id || -1,
pmId: p.pm_id ?? -1,
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unrelated to the core purpose of this PR, but it's an issue I ran into recently: if p.pm_id is 0, we would assign it a value of -1, which is incorrect.

status: p.pm2_env?.status || 'unknown',
pid: p.pid,
} ) );
Expand Down Expand Up @@ -188,7 +154,7 @@ export async function isProcessRunning(
export async function startProcess(
processName: string,
scriptPath: string,
env?: Record< string, string >
env: Record< string, string > = {}
): Promise< ProcessDescription > {
return new Promise( ( resolve, reject ) => {
const processConfig: StartOptions = {
Expand All @@ -197,7 +163,7 @@ export async function startProcess(
script: scriptPath,
exec_mode: 'fork',
autorestart: false,
env: env || {},
env: env,
};

pm2.start( processConfig, async ( error, apps ) => {
Expand All @@ -206,7 +172,7 @@ export async function startProcess(
return;
}

if ( apps && apps.length > 0 ) {
if ( apps.length > 0 ) {
const app = apps[ 0 ] as ( typeof apps )[ 0 ] & {
pm2_env?: { pm_id?: number; status?: string };
pid?: number;
Expand Down
Loading