@@ -18,7 +18,15 @@ Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
18
18
const messages = Messages . loadMessages ( '@salesforce/plugin-lightning-dev' , 'lightning.dev.component' ) ;
19
19
const sharedMessages = Messages . loadMessages ( '@salesforce/plugin-lightning-dev' , 'shared.utils' ) ;
20
20
21
- export default class LightningDevComponent extends SfCommand < void > {
21
+ export type ComponentPreviewResult = {
22
+ instanceUrl : string ;
23
+ ldpServerUrl : string ;
24
+ ldpServerId : string ;
25
+ componentName : string ;
26
+ previewUrl : string ;
27
+ } ;
28
+
29
+ export default class LightningDevComponent extends SfCommand < ComponentPreviewResult > {
22
30
public static readonly summary = messages . getMessage ( 'summary' ) ;
23
31
public static readonly description = messages . getMessage ( 'description' ) ;
24
32
public static readonly examples = messages . getMessages ( 'examples' ) ;
@@ -29,6 +37,7 @@ export default class LightningDevComponent extends SfCommand<void> {
29
37
char : 'n' ,
30
38
requiredOrDefaulted : false ,
31
39
} ) ,
40
+ 'api-version' : Flags . orgApiVersion ( ) ,
32
41
'client-select' : Flags . boolean ( {
33
42
summary : messages . getMessage ( 'flags.client-select.summary' ) ,
34
43
char : 'c' ,
@@ -37,7 +46,7 @@ export default class LightningDevComponent extends SfCommand<void> {
37
46
'target-org' : Flags . requiredOrg ( ) ,
38
47
} ;
39
48
40
- public async run ( ) : Promise < void > {
49
+ public async run ( ) : Promise < ComponentPreviewResult > {
41
50
const { flags } = await this . parse ( LightningDevComponent ) ;
42
51
const logger = await Logger . child ( this . ctor . name ) ;
43
52
const project = await SfProject . resolve ( ) ;
@@ -54,6 +63,7 @@ export default class LightningDevComponent extends SfCommand<void> {
54
63
let componentName = flags [ 'name' ] ;
55
64
const clientSelect = flags [ 'client-select' ] ;
56
65
const targetOrg = flags [ 'target-org' ] ;
66
+ const apiVersion = flags [ 'api-version' ] ;
57
67
58
68
const { ldpServerId, ldpServerToken } = await PreviewUtils . initializePreviewConnection ( targetOrg ) ;
59
69
@@ -65,41 +75,41 @@ export default class LightningDevComponent extends SfCommand<void> {
65
75
const ldpServerUrl = PreviewUtils . generateWebSocketUrlForLocalDevServer ( Platform . desktop , serverPorts , logger ) ;
66
76
logger . debug ( `Local Dev Server url is ${ ldpServerUrl } ` ) ;
67
77
68
- const namespacePaths = await ComponentUtils . getNamespacePaths ( project ) ;
69
- const componentPaths = await ComponentUtils . getAllComponentPaths ( namespacePaths ) ;
70
- if ( ! componentPaths ) {
71
- throw new Error ( messages . getMessage ( 'error.directory' ) ) ;
72
- }
78
+ if ( ! clientSelect ) {
79
+ const namespacePaths = await ComponentUtils . getNamespacePaths ( project ) ;
80
+ const componentPaths = await ComponentUtils . getAllComponentPaths ( namespacePaths ) ;
81
+ if ( ! componentPaths ) {
82
+ throw new Error ( messages . getMessage ( 'error.directory' ) ) ;
83
+ }
73
84
74
- const components = (
75
- await Promise . all (
76
- componentPaths . map ( async ( componentPath ) => {
77
- let xml ;
78
-
79
- try {
80
- xml = await ComponentUtils . getComponentMetadata ( componentPath ) ;
81
- } catch ( err ) {
82
- this . warn ( messages . getMessage ( 'error.component-metadata' , [ componentPath ] ) ) ;
83
- }
84
-
85
- // components must have meta xml to be previewed
86
- if ( ! xml ) {
87
- return undefined ;
88
- }
89
-
90
- const name = path . basename ( componentPath ) ;
91
- const label = ComponentUtils . componentNameToTitleCase ( name ) ;
92
-
93
- return {
94
- name,
95
- label : xml . LightningComponentBundle . masterLabel ?? label ,
96
- description : xml . LightningComponentBundle . description ?? '' ,
97
- } ;
98
- } )
99
- )
100
- ) . filter ( ( component ) => ! ! component ) ;
85
+ const components = (
86
+ await Promise . all (
87
+ componentPaths . map ( async ( componentPath ) => {
88
+ let xml ;
89
+
90
+ try {
91
+ xml = await ComponentUtils . getComponentMetadata ( componentPath ) ;
92
+ } catch ( err ) {
93
+ this . warn ( messages . getMessage ( 'error.component-metadata' , [ componentPath ] ) ) ;
94
+ }
95
+
96
+ // components must have meta xml to be previewed
97
+ if ( ! xml ) {
98
+ return undefined ;
99
+ }
100
+
101
+ const name = path . basename ( componentPath ) ;
102
+ const label = ComponentUtils . componentNameToTitleCase ( name ) ;
103
+
104
+ return {
105
+ name,
106
+ label : xml . LightningComponentBundle . masterLabel ?? label ,
107
+ description : xml . LightningComponentBundle . description ?? '' ,
108
+ } ;
109
+ } )
110
+ )
111
+ ) . filter ( ( component ) => ! ! component ) ;
101
112
102
- if ( ! clientSelect ) {
103
113
if ( componentName ) {
104
114
// validate that the component exists before launching the server
105
115
const match = components . find (
@@ -119,7 +129,9 @@ export default class LightningDevComponent extends SfCommand<void> {
119
129
}
120
130
}
121
131
122
- await startLWCServer ( logger , sfdxProjectRootPath , ldpServerToken , Platform . desktop , serverPorts ) ;
132
+ if ( process . env . LAUNCH_SERVER !== 'false' ) {
133
+ await startLWCServer ( logger , sfdxProjectRootPath , ldpServerToken , Platform . desktop , serverPorts ) ;
134
+ }
123
135
124
136
const targetOrgArg = PreviewUtils . getTargetOrgFromArguments ( this . argv ) ;
125
137
const launchArguments = PreviewUtils . generateComponentPreviewLaunchArguments (
@@ -130,22 +142,30 @@ export default class LightningDevComponent extends SfCommand<void> {
130
142
) ;
131
143
132
144
// Construct and log the full URL that will be opened
133
- const connection = targetOrg . getConnection ( ) ;
145
+ const connection = targetOrg . getConnection ( apiVersion ) ;
134
146
135
- const decodedFullUrl = PreviewUtils . generateComponentPreviewUrl (
147
+ const previewUrl = PreviewUtils . generateComponentPreviewUrl (
136
148
connection . instanceUrl ,
137
149
ldpServerUrl ,
138
150
ldpServerId ,
139
151
componentName ,
140
152
false
141
153
) ;
142
154
155
+ // Prepare the result for JSON output
156
+ const result : ComponentPreviewResult = {
157
+ instanceUrl : connection . instanceUrl ,
158
+ ldpServerUrl,
159
+ ldpServerId,
160
+ componentName : componentName ?? '' ,
161
+ previewUrl,
162
+ } ;
163
+
143
164
// Open the browser and navigate to the right page (unless OPEN_BROWSER is set to true)
144
165
if ( process . env . OPEN_BROWSER !== 'false' ) {
145
166
await this . config . runCommand ( 'org:open' , launchArguments ) ;
146
- } else {
147
- // Otherwise, log the URL to the console
148
- this . log ( `PreviewURL: ${ decodedFullUrl } ` ) ;
149
167
}
168
+
169
+ return result ;
150
170
}
151
171
}
0 commit comments