Skip to content

Commit b2a05fd

Browse files
committed
feat: enhance LightningDevComponent to return json
1 parent 5111e3b commit b2a05fd

File tree

1 file changed

+61
-41
lines changed

1 file changed

+61
-41
lines changed

src/commands/lightning/dev/component.ts

Lines changed: 61 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,15 @@ Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
1818
const messages = Messages.loadMessages('@salesforce/plugin-lightning-dev', 'lightning.dev.component');
1919
const sharedMessages = Messages.loadMessages('@salesforce/plugin-lightning-dev', 'shared.utils');
2020

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> {
2230
public static readonly summary = messages.getMessage('summary');
2331
public static readonly description = messages.getMessage('description');
2432
public static readonly examples = messages.getMessages('examples');
@@ -29,6 +37,7 @@ export default class LightningDevComponent extends SfCommand<void> {
2937
char: 'n',
3038
requiredOrDefaulted: false,
3139
}),
40+
'api-version': Flags.orgApiVersion(),
3241
'client-select': Flags.boolean({
3342
summary: messages.getMessage('flags.client-select.summary'),
3443
char: 'c',
@@ -37,7 +46,7 @@ export default class LightningDevComponent extends SfCommand<void> {
3746
'target-org': Flags.requiredOrg(),
3847
};
3948

40-
public async run(): Promise<void> {
49+
public async run(): Promise<ComponentPreviewResult> {
4150
const { flags } = await this.parse(LightningDevComponent);
4251
const logger = await Logger.child(this.ctor.name);
4352
const project = await SfProject.resolve();
@@ -54,6 +63,7 @@ export default class LightningDevComponent extends SfCommand<void> {
5463
let componentName = flags['name'];
5564
const clientSelect = flags['client-select'];
5665
const targetOrg = flags['target-org'];
66+
const apiVersion = flags['api-version'];
5767

5868
const { ldpServerId, ldpServerToken } = await PreviewUtils.initializePreviewConnection(targetOrg);
5969

@@ -65,41 +75,41 @@ export default class LightningDevComponent extends SfCommand<void> {
6575
const ldpServerUrl = PreviewUtils.generateWebSocketUrlForLocalDevServer(Platform.desktop, serverPorts, logger);
6676
logger.debug(`Local Dev Server url is ${ldpServerUrl}`);
6777

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+
}
7384

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);
101112

102-
if (!clientSelect) {
103113
if (componentName) {
104114
// validate that the component exists before launching the server
105115
const match = components.find(
@@ -119,7 +129,9 @@ export default class LightningDevComponent extends SfCommand<void> {
119129
}
120130
}
121131

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+
}
123135

124136
const targetOrgArg = PreviewUtils.getTargetOrgFromArguments(this.argv);
125137
const launchArguments = PreviewUtils.generateComponentPreviewLaunchArguments(
@@ -130,22 +142,30 @@ export default class LightningDevComponent extends SfCommand<void> {
130142
);
131143

132144
// Construct and log the full URL that will be opened
133-
const connection = targetOrg.getConnection();
145+
const connection = targetOrg.getConnection(apiVersion);
134146

135-
const decodedFullUrl = PreviewUtils.generateComponentPreviewUrl(
147+
const previewUrl = PreviewUtils.generateComponentPreviewUrl(
136148
connection.instanceUrl,
137149
ldpServerUrl,
138150
ldpServerId,
139151
componentName,
140152
false
141153
);
142154

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+
143164
// Open the browser and navigate to the right page (unless OPEN_BROWSER is set to true)
144165
if (process.env.OPEN_BROWSER !== 'false') {
145166
await this.config.runCommand('org:open', launchArguments);
146-
} else {
147-
// Otherwise, log the URL to the console
148-
this.log(`PreviewURL: ${decodedFullUrl}`);
149167
}
168+
169+
return result;
150170
}
151171
}

0 commit comments

Comments
 (0)