1- import fs from 'fs' ;
21import os from 'os' ;
32import path from 'path' ;
43import { cacheFunctionTTL } from 'common/lib/cache-function-ttl' ;
5- import { StartOptions } from 'pm2' ;
4+ import { custom as PM2 , StartOptions } from 'pm2' ;
65import { getAppdataPath } from 'cli/lib/appdata' ;
76import { ProcessDescription } from 'cli/lib/types/pm2' ;
87import { ManagerMessage } from './types/wordpress-server-ipc' ;
@@ -15,40 +14,7 @@ const DAEMON_TIMEOUT = 10000;
1514// This ensures all Studio CLI commands use the same PM2 daemon
1615const STUDIO_PM2_HOME = path . join ( os . homedir ( ) , '.studio' , 'pm2' ) ;
1716
18- process . env . PM2_HOME = STUDIO_PM2_HOME ;
19-
20- function resolvePm2 ( ) : typeof import ( 'pm2' ) {
21- try {
22- return require ( 'pm2' ) ;
23- } catch ( error ) {
24- const possiblePaths : string [ ] = [
25- path . join ( __dirname , 'node_modules' , 'pm2' ) ,
26- path . join ( path . dirname ( __dirname ) , 'node_modules' , 'pm2' ) ,
27- path . join ( __dirname , '..' , 'node_modules' , 'pm2' ) ,
28- path . join ( __dirname , '..' , '..' , 'node_modules' , 'pm2' ) ,
29- path . join ( __dirname , '..' , '..' , '..' , 'node_modules' , 'pm2' ) ,
30- path . resolve ( process . cwd ( ) , 'node_modules' , 'pm2' ) ,
31- ] ;
32-
33- for ( const pm2Path of possiblePaths ) {
34- if ( fs . existsSync ( pm2Path ) ) {
35- try {
36- return require ( pm2Path ) ;
37- } catch {
38- continue ;
39- }
40- }
41- }
42-
43- throw new Error (
44- `pm2 module not found. Please ensure pm2 is installed in the CLI dependencies. Tried paths: ${ possiblePaths . join (
45- ', '
46- ) } `
47- ) ;
48- }
49- }
50-
51- const pm2 = resolvePm2 ( ) ;
17+ const pm2 = new PM2 ( { pm2_home : STUDIO_PM2_HOME } ) ;
5218
5319let isConnected = false ;
5420
@@ -101,7 +67,7 @@ const listProcesses = cacheFunctionTTL( () => {
10167
10268 const processDescriptions = ( processes || [ ] ) . map ( ( p ) => ( {
10369 name : p . name || '' ,
104- pmId : p . pm_id || - 1 ,
70+ pmId : p . pm_id ?? - 1 ,
10571 status : p . pm2_env ?. status || 'unknown' ,
10672 pid : p . pid ,
10773 } ) ) ;
@@ -188,7 +154,7 @@ export async function isProcessRunning(
188154export async function startProcess (
189155 processName : string ,
190156 scriptPath : string ,
191- env ? : Record < string , string >
157+ env : Record < string , string > = { }
192158) : Promise < ProcessDescription > {
193159 return new Promise ( ( resolve , reject ) => {
194160 const processConfig : StartOptions = {
@@ -197,7 +163,7 @@ export async function startProcess(
197163 script : scriptPath ,
198164 exec_mode : 'fork' ,
199165 autorestart : false ,
200- env : env || { } ,
166+ env : env ,
201167 } ;
202168
203169 pm2 . start ( processConfig , async ( error , apps ) => {
@@ -206,7 +172,7 @@ export async function startProcess(
206172 return ;
207173 }
208174
209- if ( apps && apps . length > 0 ) {
175+ if ( apps . length > 0 ) {
210176 const app = apps [ 0 ] as ( typeof apps ) [ 0 ] & {
211177 pm2_env ?: { pm_id ?: number ; status ?: string } ;
212178 pid ?: number ;
0 commit comments