Skip to content

Commit bd3b723

Browse files
Remove resolvePm2 and fix pm2 types (#2174)
* Fix pm2 types * Fix tests * Fix -1 pmId * Fix remaining tests
1 parent ab06359 commit bd3b723

4 files changed

Lines changed: 932 additions & 431 deletions

File tree

cli/__mocks__/pm2.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class API {
2+
connect( callback: ( error: Error | null ) => void ) {
3+
setTimeout( () => {
4+
callback( null );
5+
}, 0 );
6+
}
7+
disconnect( callback: ( error: Error | null ) => void ) {
8+
setTimeout( () => {
9+
callback( null );
10+
}, 0 );
11+
}
12+
list( callback: ( error: Error | null, processes: any[] ) => void ) {
13+
setTimeout( () => {
14+
callback( null, [] );
15+
}, 0 );
16+
}
17+
launchBus( callback: ( error: Error | null, bus: any ) => void ) {
18+
setTimeout( () => {
19+
callback( null, {} );
20+
}, 0 );
21+
}
22+
sendDataToProcessId( id: string, data: any, callback: ( error: Error | null ) => void ) {
23+
setTimeout( () => {
24+
callback( null );
25+
}, 0 );
26+
}
27+
start( config: any, callback: ( error: Error | null, apps: any[] ) => void ) {
28+
setTimeout( () => {
29+
callback( null, [] );
30+
}, 0 );
31+
}
32+
delete( callback: ( error: Error | null ) => void ) {
33+
setTimeout( () => {
34+
callback( null );
35+
}, 0 );
36+
}
37+
}
38+
39+
exports.custom = API;

cli/lib/pm2-manager.ts

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import fs from 'fs';
21
import os from 'os';
32
import path from 'path';
43
import { cacheFunctionTTL } from 'common/lib/cache-function-ttl';
5-
import { StartOptions } from 'pm2';
4+
import { custom as PM2, StartOptions } from 'pm2';
65
import { getAppdataPath } from 'cli/lib/appdata';
76
import { ProcessDescription } from 'cli/lib/types/pm2';
87
import { 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
1615
const 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

5319
let 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(
188154
export 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

Comments
 (0)