1- import { chmodSync , copyFileSync , mkdtempSync , rmSync , writeFileSync } from 'node:fs'
1+ import { chmodSync , mkdtempSync , rmSync , writeFileSync } from 'node:fs'
22import { dirname , resolve } from 'node:path'
33import { fileURLToPath } from 'node:url'
44import { tmpdir } from 'node:os'
@@ -7,12 +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'
1011
1112const repositoryRoot = resolve ( dirname ( fileURLToPath ( import . meta. url ) ) , '..' )
1213const salesforceAppDir = resolve ( repositoryRoot , 'test/apps/sf-lwc-app' )
13- const bundlePath = resolve ( repositoryRoot , 'packages/browser-rum-slim/bundle/datadog-rum-slim.js' )
14- const stableStaticResourcePath = resolve ( salesforceAppDir , 'force-app/main/default/staticresources/datadog_rum_slim.js' )
15- const salesforceHomePath = '/lightning/app/c__SF_LWC_App/page/home'
1614const defaultTargetOrg = 'sf-lwc-ci'
1715
1816runMain ( ( ) => {
@@ -50,7 +48,7 @@ runMain(() => {
5048 break
5149 // Get the authenticated URL of the app with the RUM configuration.
5250 case 'get-url' :
53- process . stdout . write ( `with the following URL: ${ buildOpenUrl ( values . proxy ) } \n` )
51+ process . stdout . write ( `with the following URL: ${ buildSalesforceLwcUrl ( values . proxy ) } \n` )
5452 break
5553 default :
5654 throw new Error ( `Unknown command "${ commandName ?? '' } ". Expected: auth|deploy-app|get-url` )
@@ -85,62 +83,12 @@ function authenticate() {
8583}
8684
8785function deployApp ( ) {
88- const targetOrg = getTargetOrg ( )
86+ const targetOrg = process . env . SF_TARGET_ORG ?? defaultTargetOrg
8987
9088 printLog ( `Deploying Salesforce LWC app to ${ targetOrg } ...` )
91- // Deploy the app to the Salesforce org. To be done only when the app is updated.
92- copyFileSync ( bundlePath , stableStaticResourcePath )
93-
94- // Clear stale source-tracking state so the deploy doesn't skip files it thinks are already in sync.
95- resetSourceTracking ( targetOrg )
9689 command `sf project deploy start --target-org ${ targetOrg } --source-dir force-app --ignore-conflicts --concise`
9790 . withCurrentWorkingDirectory ( salesforceAppDir )
9891 . withLogs ( )
9992 . run ( )
10093 printLog ( 'Salesforce LWC app deployed.' )
10194}
102-
103- function resetSourceTracking ( targetOrg : string ) {
104- command `sf project reset tracking --target-org ${ targetOrg } --no-prompt`
105- . withCurrentWorkingDirectory ( salesforceAppDir )
106- . withLogs ( )
107- . run ( )
108- }
109-
110- function buildOpenUrl ( proxy ?: string ) : string {
111- const targetOrg = getTargetOrg ( )
112- const path = new URL ( salesforceHomePath , 'https://salesforce.local' )
113-
114- // c__datadogInitConfiguration must be part of the path (not a top-level frontdoor.jsp param)
115- // so that Salesforce passes it through to the Lightning app after authentication.
116- if ( proxy ) {
117- path . searchParams . set (
118- 'c__datadogInitConfiguration' ,
119- JSON . stringify ( {
120- applicationId : '37fe52bf-b3d5-4ac7-ad9b-44882d479ec8' ,
121- clientToken : 'pubf2099de38f9c85797d20d64c7d632a69' ,
122- defaultPrivacyLevel : 'allow' ,
123- trackResources : true ,
124- trackLongTasks : true ,
125- proxy,
126- } )
127- )
128- }
129-
130- const output = command `sf org open --target-org ${ targetOrg } --path ${ path . pathname } ${ path . search } --url-only`
131- . withCurrentWorkingDirectory ( salesforceAppDir )
132- . run ( )
133-
134- // The sf CLI appends ANSI reset codes (\x1b[39m etc.) directly to the URL on stdout.
135- // Excluding control characters (0x00–0x1f, which includes ESC/0x1b) strips them cleanly.
136- // eslint-disable-next-line no-control-regex
137- const url = output . match ( / h t t p s : \/ \/ [ ^ \s \x00 - \x1f ] + / g) ?. at ( - 1 )
138- if ( ! url ) {
139- throw new Error ( `Unable to find Salesforce URL in command output:\n${ output } ` )
140- }
141- return url
142- }
143-
144- function getTargetOrg ( ) : string {
145- return process . env . SF_TARGET_ORG || defaultTargetOrg
146- }
0 commit comments