@@ -7,10 +7,10 @@ import { parseArgs } from 'node:util'
77import { printLog , runMain } from './lib/executionUtils.ts'
88import { getSfLwcClientId , getSfLwcInstanceUrl , getSfLwcJwtPrivateKey , getSfLwcUsername } from './lib/secrets.ts'
99import { command } from './lib/command.ts'
10- import { buildSalesforceLwcUrl } from './lib/buildSalesforceLwcUrl.ts'
1110
1211const repositoryRoot = resolve ( dirname ( fileURLToPath ( import . meta. url ) ) , '..' )
1312const salesforceAppDir = resolve ( repositoryRoot , 'test/apps/sf-lwc-app' )
13+ const salesforceHomePath = '/lightning/app/c__SF_LWC_App/page/home'
1414const defaultTargetOrg = 'sf-lwc-ci'
1515
1616runMain ( ( ) => {
@@ -21,9 +21,6 @@ runMain(() => {
2121 type : 'boolean' ,
2222 short : 'h' ,
2323 } ,
24- proxy : {
25- type : 'string' ,
26- } ,
2724 } ,
2825 } )
2926
@@ -32,31 +29,27 @@ runMain(() => {
3229 }
3330
3431 if ( positionals . length !== 1 ) {
35- throw new Error ( 'Usage: node scripts/salesforce-lwc-app.ts <auth| deploy-app|get-url>' )
32+ throw new Error ( 'Usage: node scripts/salesforce-lwc-app.ts <deploy-app|get-url>' )
3633 }
3734
3835 const commandName = positionals [ 0 ]
3936
4037 switch ( commandName ) {
41- // Authenticate in the Salesforce CLI.
42- case 'auth' :
43- authenticate ( )
44- break
4538 // Deploy the app to the Salesforce org. To be done only when the app is updated.
4639 case 'deploy-app' :
4740 deployApp ( )
4841 break
49- // Get the authenticated URL of the app with the RUM configuration .
42+ // Get the authenticated URL of the app.
5043 case 'get-url' :
51- process . stdout . write ( `with the following URL: ${ buildSalesforceLwcUrl ( values . proxy ) } \n` )
44+ process . stdout . write ( `${ buildSalesforceLwcUrl ( ) } \n` )
5245 break
5346 default :
54- throw new Error ( `Unknown command "${ commandName ?? '' } ". Expected: auth| deploy-app|get-url` )
47+ throw new Error ( `Unknown command "${ commandName ?? '' } ". Expected: deploy-app|get-url` )
5548 }
5649} )
5750
5851function showUsageAndExit ( ) {
59- console . log ( 'Usage: node scripts/salesforce-lwc-app.ts <auth| deploy-app|get-url> [--proxy <url>] ' )
52+ console . log ( 'Usage: node scripts/salesforce-lwc-app.ts <deploy-app|get-url>' )
6053 process . exit ( 0 )
6154}
6255
@@ -85,10 +78,41 @@ function authenticate() {
8578function deployApp ( ) {
8679 const targetOrg = process . env . SF_TARGET_ORG ?? defaultTargetOrg
8780
81+ if ( ! isOrgAuthenticated ( targetOrg ) ) {
82+ authenticate ( )
83+ }
84+
8885 printLog ( `Deploying Salesforce LWC app to ${ targetOrg } ...` )
8986 command `sf project deploy start --target-org ${ targetOrg } --source-dir force-app --ignore-conflicts --concise`
9087 . withCurrentWorkingDirectory ( salesforceAppDir )
9188 . withLogs ( )
9289 . run ( )
9390 printLog ( 'Salesforce LWC app deployed.' )
9491}
92+
93+ function isOrgAuthenticated ( targetOrg : string ) : boolean {
94+ try {
95+ command `sf org display --target-org ${ targetOrg } ` . withCurrentWorkingDirectory ( salesforceAppDir ) . run ( )
96+ return true
97+ } catch {
98+ return false
99+ }
100+ }
101+
102+ function buildSalesforceLwcUrl ( ) : string {
103+ const targetOrg = process . env . SF_TARGET_ORG ?? defaultTargetOrg
104+ const path = new URL ( salesforceHomePath , 'https://salesforce.local' )
105+
106+ const output = command `sf org open --target-org ${ targetOrg } --path ${ path . pathname } ${ path . search } --url-only`
107+ . withCurrentWorkingDirectory ( salesforceAppDir )
108+ . run ( )
109+
110+ // The sf CLI appends ANSI reset codes (\x1b[39m etc.) directly to the URL on stdout.
111+ // Excluding control characters (0x00–0x1f, which includes ESC/0x1b) strips them cleanly.
112+ // eslint-disable-next-line no-control-regex
113+ const url = output . match ( / h t t p s : \/ \/ [ ^ \s \x00 - \x1f ] + / g) ?. at ( - 1 )
114+ if ( ! url ) {
115+ throw new Error ( `Unable to find Salesforce URL in command output:\n${ output } ` )
116+ }
117+ return url
118+ }
0 commit comments