77
88import path from 'node:path' ;
99import { SfCommand , Flags } from '@salesforce/sf-plugins-core' ;
10- import { Messages , SfProject } from '@salesforce/core' ;
10+ import { Messages , SfProject , Logger } from '@salesforce/core' ;
11+ import { Platform } from '@salesforce/lwc-dev-mobile-core' ;
1112import { ComponentUtils } from '../../../shared/componentUtils.js' ;
1213import { PromptUtils } from '../../../shared/promptUtils.js' ;
1314import { PreviewUtils } from '../../../shared/previewUtils.js' ;
15+ import { startLWCServer } from '../../../lwc-dev-server/index.js' ;
1416
1517Messages . importMessagesDirectoryFromMetaUrl ( import . meta. url ) ;
1618const messages = Messages . loadMessages ( '@salesforce/plugin-lightning-dev' , 'lightning.dev.component' ) ;
1719
18- // TODO: generate ldp server url
19- const ldpServerUrl = 'http://localhost:3000' ;
20-
2120export default class LightningDevComponent extends SfCommand < void > {
2221 public static readonly summary = messages . getMessage ( 'summary' ) ;
2322 public static readonly description = messages . getMessage ( 'description' ) ;
@@ -39,13 +38,30 @@ export default class LightningDevComponent extends SfCommand<void> {
3938
4039 public async run ( ) : Promise < void > {
4140 const { flags } = await this . parse ( LightningDevComponent ) ;
41+ const logger = await Logger . child ( this . ctor . name ) ;
4242 const project = await SfProject . resolve ( ) ;
4343
44+ let sfdxProjectRootPath = '' ;
45+ try {
46+ sfdxProjectRootPath = await SfProject . resolveProjectPath ( ) ;
47+ } catch ( error ) {
48+ // return Promise.reject(new Error(messages.getMessage('error.no-project', [(error as Error)?.message ?? ''])));
49+ throw new Error ( 'failed to resolve project path' ) ;
50+ }
51+
4452 let componentName = flags [ 'name' ] ;
4553 const clientSelect = flags [ 'client-select' ] ;
4654 const targetOrg = flags [ 'target-org' ] ;
4755
48- const { ldpServerId } = await PreviewUtils . initializePreviewConnection ( targetOrg ) ;
56+ const { ldpServerId, ldpServerToken } = await PreviewUtils . initializePreviewConnection ( targetOrg ) ;
57+
58+ logger . debug ( 'Determining the next available port for Local Dev Server' ) ;
59+ const serverPorts = await PreviewUtils . getNextAvailablePorts ( ) ;
60+ logger . debug ( `Next available ports are http=${ serverPorts . httpPort } , https=${ serverPorts . httpsPort } ` ) ;
61+
62+ logger . debug ( 'Determining Local Dev Server url' ) ;
63+ const ldpServerUrl = PreviewUtils . generateWebSocketUrlForLocalDevServer ( Platform . desktop , serverPorts , logger ) ;
64+ logger . debug ( `Local Dev Server url is ${ ldpServerUrl } ` ) ;
4965
5066 const namespacePaths = await ComponentUtils . getNamespacePaths ( project ) ;
5167 const componentPaths = await ComponentUtils . getAllComponentPaths ( namespacePaths ) ;
@@ -101,7 +117,7 @@ export default class LightningDevComponent extends SfCommand<void> {
101117 }
102118 }
103119
104- // TODO: launch the local dev server
120+ await startLWCServer ( logger , sfdxProjectRootPath , ldpServerToken , Platform . desktop , serverPorts ) ;
105121
106122 const targetOrgArg = PreviewUtils . getTargetOrgFromArguments ( this . argv ) ;
107123 const launchArguments = PreviewUtils . generateComponentPreviewLaunchArguments (
0 commit comments