@@ -31,7 +31,7 @@ import {
3131} from '@salesforce/lwc-dev-mobile-core' ;
3232import { Progress , Spinner } from '@salesforce/sf-plugins-core' ;
3333import fetch from 'node-fetch' ;
34- import { ConfigUtils , LOCAL_DEV_SERVER_DEFAULT_PORT } from './configUtils.js' ;
34+ import { ConfigUtils , LOCAL_DEV_SERVER_DEFAULT_HTTP_PORT } from './configUtils.js' ;
3535
3636Messages . importMessagesDirectoryFromMetaUrl ( import . meta. url ) ;
3737const messages = Messages . loadMessages ( '@salesforce/plugin-lightning-dev' , 'lightning.preview.app' ) ;
@@ -40,14 +40,14 @@ const DevPreviewAuraMode = 'DEVPREVIEW';
4040export class PreviewUtils {
4141 public static generateWebSocketUrlForLocalDevServer (
4242 platform : string ,
43- port : string | number ,
43+ ports : { httpPort : number ; httpsPort : number } ,
4444 logger ?: Logger
4545 ) : string {
46- return LwcDevMobileCorePreviewUtils . generateWebSocketUrlForLocalDevServer ( platform , port . toString ( ) , logger ) ;
46+ return LwcDevMobileCorePreviewUtils . generateWebSocketUrlForLocalDevServer ( platform , ports , logger ) ;
4747 }
4848
4949 /**
50- * Returns a port number to be used by the local dev server.
50+ * Returns a pair of port numbers to be used by the local dev server for http and https .
5151 *
5252 * It starts by checking whether the user has configured a port in their config file.
5353 * If so then we are only allowed to use that port, regardless of whether it is in use
@@ -58,38 +58,19 @@ export class PreviewUtils {
5858 * If it is in use then we increment the port number by 2 and check if it is in use or not.
5959 * This process is repeated until a port that is not in use is found.
6060 *
61- * @returns a port number to be used by the local dev server.
61+ * @returns a pair of port numbers to be used by the local dev server for http and https .
6262 */
63- public static async getNextAvailablePort ( ) : Promise < number > {
64- const userConfiguredPort = await ConfigUtils . getLocalDevServerPort ( ) ;
63+ public static async getNextAvailablePorts ( ) : Promise < { httpPort : number ; httpsPort : number } > {
64+ const userConfiguredPorts = await ConfigUtils . getLocalDevServerPorts ( ) ;
6565
66- if ( userConfiguredPort ) {
67- return Promise . resolve ( userConfiguredPort ) ;
66+ if ( userConfiguredPorts ) {
67+ return Promise . resolve ( userConfiguredPorts ) ;
6868 }
6969
70- let port = LOCAL_DEV_SERVER_DEFAULT_PORT ;
71- let done = false ;
72-
73- while ( ! done ) {
74- const cmd =
75- process . platform === 'win32' ? `netstat -an | find "LISTENING" | find ":${ port } "` : `lsof -i :${ port } ` ;
70+ const httpPort = await this . doGetNextAvailablePort ( LOCAL_DEV_SERVER_DEFAULT_HTTP_PORT ) ;
71+ const httpsPort = await this . doGetNextAvailablePort ( httpPort + 1 ) ;
7672
77- try {
78- const result = CommonUtils . executeCommandSync ( cmd ) ;
79- if ( result . trim ( ) ) {
80- port = port + 2 ; // that port is in use so try another
81- } else {
82- done = true ;
83- }
84- } catch ( error ) {
85- // On some platforms (like mac) if the command doesn't produce
86- // any results then that is considered an error but in our case
87- // that means the port is not in use and is ready for us to use.
88- done = true ;
89- }
90- }
91-
92- return Promise . resolve ( port ) ;
73+ return Promise . resolve ( { httpPort, httpsPort } ) ;
9374 }
9475
9576 /**
@@ -516,4 +497,30 @@ export class PreviewUtils {
516497 return entityId ;
517498 }
518499 }
500+
501+ private static async doGetNextAvailablePort ( startingPort : number ) : Promise < number > {
502+ let port = startingPort ;
503+ let done = false ;
504+
505+ while ( ! done ) {
506+ const cmd =
507+ process . platform === 'win32' ? `netstat -an | find "LISTENING" | find ":${ port } "` : `lsof -i :${ port } ` ;
508+
509+ try {
510+ const result = CommonUtils . executeCommandSync ( cmd ) ;
511+ if ( result . trim ( ) ) {
512+ port = port + 2 ; // that port is in use so try another
513+ } else {
514+ done = true ;
515+ }
516+ } catch ( error ) {
517+ // On some platforms (like mac) if the command doesn't produce
518+ // any results then that is considered an error but in our case
519+ // that means the port is not in use and is ready for us to use.
520+ done = true ;
521+ }
522+ }
523+
524+ return Promise . resolve ( port ) ;
525+ }
519526}
0 commit comments