66 */
77
88import path from 'node:path' ;
9- import { Connection , Logger , Messages , SfProject } from '@salesforce/core' ;
9+ import { Logger , Messages , SfProject } from '@salesforce/core' ;
1010import {
1111 AndroidAppPreviewConfig ,
1212 AndroidDevice ,
@@ -20,7 +20,7 @@ import { Flags, SfCommand } from '@salesforce/sf-plugins-core';
2020import { OrgUtils } from '../../../shared/orgUtils.js' ;
2121import { startLWCServer } from '../../../lwc-dev-server/index.js' ;
2222import { PreviewUtils } from '../../../shared/previewUtils.js' ;
23- import { ConfigUtils , IdentityTokenService } from '../../../shared/configUtils .js' ;
23+ import { PromptUtils } from '../../../shared/promptUtils .js' ;
2424
2525Messages . importMessagesDirectoryFromMetaUrl ( import . meta. url ) ;
2626const messages = Messages . loadMessages ( '@salesforce/plugin-lightning-dev' , 'lightning.dev.app' ) ;
@@ -39,22 +39,6 @@ export const androidSalesforceAppPreviewConfig = {
3939
4040const maxInt32 = 2_147_483_647 ; // maximum 32-bit signed integer value
4141
42- class AppServerIdentityTokenService implements IdentityTokenService {
43- private connection : Connection ;
44- public constructor ( connection : Connection ) {
45- this . connection = connection ;
46- }
47-
48- public async saveTokenToServer ( token : string ) : Promise < string > {
49- const sobject = this . connection . sobject ( 'UserLocalWebServerIdentity' ) ;
50- const result = await sobject . insert ( { LocalWebServerIdentityToken : token } ) ;
51- if ( result . success ) {
52- return result . id ;
53- }
54- throw new Error ( 'Could not save the token to the server' ) ;
55- }
56- }
57-
5842export default class LightningDevApp extends SfCommand < void > {
5943 public static readonly summary = messages . getMessage ( 'summary' ) ;
6044 public static readonly description = messages . getMessage ( 'description' ) ;
@@ -71,7 +55,6 @@ export default class LightningDevApp extends SfCommand<void> {
7155 summary : messages . getMessage ( 'flags.device-type.summary' ) ,
7256 char : 't' ,
7357 options : [ Platform . desktop , Platform . ios , Platform . android ] as const ,
74- default : Platform . desktop ,
7558 } ) ( ) ,
7659 'device-id' : Flags . string ( {
7760 summary : messages . getMessage ( 'flags.device-id.summary' ) ,
@@ -83,9 +66,9 @@ export default class LightningDevApp extends SfCommand<void> {
8366 const { flags } = await this . parse ( LightningDevApp ) ;
8467 const logger = await Logger . child ( this . ctor . name ) ;
8568
86- const appName = flags [ 'name' ] ;
87- const platform = flags [ 'device-type' ] ;
8869 const targetOrg = flags [ 'target-org' ] ;
70+ const appName = flags [ 'name' ] ;
71+ const platform = flags [ 'device-type' ] ?? ( await PromptUtils . promptUserToSelectPlatform ( ) ) ;
8972 const deviceId = flags [ 'device-id' ] ;
9073
9174 let sfdxProjectRootPath = '' ;
@@ -107,8 +90,12 @@ export default class LightningDevApp extends SfCommand<void> {
10790 return Promise . reject ( new Error ( sharedMessages . getMessage ( 'error.localdev.not.enabled' ) ) ) ;
10891 }
10992
110- const tokenService = new AppServerIdentityTokenService ( connection ) ;
111- const token = await ConfigUtils . getOrCreateIdentityToken ( username , tokenService ) ;
93+ const appServerIdentity = await PreviewUtils . getOrCreateAppServerIdentity ( connection ) ;
94+ const ldpServerToken = appServerIdentity . identityToken ;
95+ const ldpServerId = appServerIdentity . usernameToServerEntityIdMap [ username ] ;
96+ if ( ! ldpServerId ) {
97+ return Promise . reject ( new Error ( messages . getMessage ( 'error.identitydata.entityid' ) ) ) ;
98+ }
11299
113100 let appId : string | undefined ;
114101 if ( appName ) {
@@ -132,17 +119,23 @@ export default class LightningDevApp extends SfCommand<void> {
132119 const ldpServerUrl = PreviewUtils . generateWebSocketUrlForLocalDevServer ( platform , serverPorts , logger ) ;
133120 logger . debug ( `Local Dev Server url is ${ ldpServerUrl } ` ) ;
134121
135- const entityId = await PreviewUtils . getEntityId ( username ) ;
136-
137122 if ( platform === Platform . desktop ) {
138- await this . desktopPreview ( sfdxProjectRootPath , serverPorts , token , entityId , ldpServerUrl , appId , logger ) ;
123+ await this . desktopPreview (
124+ sfdxProjectRootPath ,
125+ serverPorts ,
126+ ldpServerToken ,
127+ ldpServerId ,
128+ ldpServerUrl ,
129+ appId ,
130+ logger
131+ ) ;
139132 } else {
140133 await this . mobilePreview (
141134 platform ,
142135 sfdxProjectRootPath ,
143136 serverPorts ,
144- token ,
145- entityId ,
137+ ldpServerToken ,
138+ ldpServerId ,
146139 ldpServerUrl ,
147140 appName ,
148141 appId ,
@@ -155,8 +148,8 @@ export default class LightningDevApp extends SfCommand<void> {
155148 private async desktopPreview (
156149 sfdxProjectRootPath : string ,
157150 serverPorts : { httpPort : number ; httpsPort : number } ,
158- token : string ,
159- entityId : string ,
151+ ldpServerToken : string ,
152+ ldpServerId : string ,
160153 ldpServerUrl : string ,
161154 appId : string | undefined ,
162155 logger : Logger
@@ -191,13 +184,13 @@ export default class LightningDevApp extends SfCommand<void> {
191184
192185 const launchArguments = PreviewUtils . generateDesktopPreviewLaunchArguments (
193186 ldpServerUrl ,
194- entityId ,
187+ ldpServerId ,
195188 appId ,
196189 targetOrg
197190 ) ;
198191
199192 // Start the LWC Dev Server
200- await startLWCServer ( logger , sfdxProjectRootPath , token , Platform . desktop , serverPorts ) ;
193+ await startLWCServer ( logger , sfdxProjectRootPath , ldpServerToken , Platform . desktop , serverPorts ) ;
201194
202195 // Open the browser and navigate to the right page
203196 await this . config . runCommand ( 'org:open' , launchArguments ) ;
@@ -207,8 +200,8 @@ export default class LightningDevApp extends SfCommand<void> {
207200 platform : Platform . ios | Platform . android ,
208201 sfdxProjectRootPath : string ,
209202 serverPorts : { httpPort : number ; httpsPort : number } ,
210- token : string ,
211- entityId : string ,
203+ ldpServerToken : string ,
204+ ldpServerId : string ,
212205 ldpServerUrl : string ,
213206 appName : string | undefined ,
214207 appId : string | undefined ,
@@ -304,14 +297,13 @@ export default class LightningDevApp extends SfCommand<void> {
304297 }
305298
306299 // Start the LWC Dev Server
307-
308- await startLWCServer ( logger , sfdxProjectRootPath , token , platform , serverPorts , certData ) ;
300+ await startLWCServer ( logger , sfdxProjectRootPath , ldpServerToken , platform , serverPorts , certData ) ;
309301
310302 // Launch the native app for previewing (launchMobileApp will show its own spinner)
311303 // eslint-disable-next-line camelcase
312304 appConfig . launch_arguments = PreviewUtils . generateMobileAppPreviewLaunchArguments (
313305 ldpServerUrl ,
314- entityId ,
306+ ldpServerId ,
315307 appName ,
316308 appId
317309 ) ;
0 commit comments