-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathcomponent.ts
More file actions
144 lines (123 loc) · 5.37 KB
/
Copy pathcomponent.ts
File metadata and controls
144 lines (123 loc) · 5.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Copyright 2025, Salesforce, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import path from 'node:path';
import { SfCommand, Flags } from '@salesforce/sf-plugins-core';
import { Messages, SfProject, Logger } from '@salesforce/core';
import { Platform } from '@salesforce/lwc-dev-mobile-core';
import { ComponentUtils } from '../../../shared/componentUtils.js';
import { PromptUtils } from '../../../shared/promptUtils.js';
import { PreviewUtils } from '../../../shared/previewUtils.js';
import { startLWCServer } from '../../../lwc-dev-server/index.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-lightning-dev', 'lightning.dev.component');
const sharedMessages = Messages.loadMessages('@salesforce/plugin-lightning-dev', 'shared.utils');
export default class LightningDevComponent extends SfCommand<void> {
public static readonly summary = messages.getMessage('summary');
public static readonly description = messages.getMessage('description');
public static readonly examples = messages.getMessages('examples');
public static readonly flags = {
name: Flags.string({
summary: messages.getMessage('flags.name.summary'),
char: 'n',
requiredOrDefaulted: false,
}),
'client-select': Flags.boolean({
summary: messages.getMessage('flags.client-select.summary'),
char: 'c',
default: false,
}),
'target-org': Flags.requiredOrg(),
};
public async run(): Promise<void> {
const { flags } = await this.parse(LightningDevComponent);
const logger = await Logger.child(this.ctor.name);
const project = await SfProject.resolve();
let sfdxProjectRootPath = '';
try {
sfdxProjectRootPath = await SfProject.resolveProjectPath();
} catch (error) {
return Promise.reject(
new Error(sharedMessages.getMessage('error.no-project', [(error as Error)?.message ?? '']))
);
}
let componentName = flags['name'];
const clientSelect = flags['client-select'];
const targetOrg = flags['target-org'];
const { ldpServerId, ldpServerToken } = await PreviewUtils.initializePreviewConnection(targetOrg);
logger.debug('Determining the next available port for Local Dev Server');
const serverPorts = await PreviewUtils.getNextAvailablePorts();
logger.debug(`Next available ports are http=${serverPorts.httpPort} , https=${serverPorts.httpsPort}`);
logger.debug('Determining Local Dev Server url');
const ldpServerUrl = PreviewUtils.generateWebSocketUrlForLocalDevServer(Platform.desktop, serverPorts, logger);
logger.debug(`Local Dev Server url is ${ldpServerUrl}`);
const namespacePaths = await ComponentUtils.getNamespacePaths(project);
const componentPaths = await ComponentUtils.getAllComponentPaths(namespacePaths);
if (!componentPaths) {
throw new Error(messages.getMessage('error.directory'));
}
const components = (
await Promise.all(
componentPaths.map(async (componentPath) => {
let xml;
try {
xml = await ComponentUtils.getComponentMetadata(componentPath);
} catch (err) {
this.warn(messages.getMessage('error.component-metadata', [componentPath]));
}
// components must have meta xml to be previewed
if (!xml) {
return undefined;
}
const name = path.basename(componentPath);
const label = ComponentUtils.componentNameToTitleCase(name);
return {
name,
label: xml.LightningComponentBundle.masterLabel ?? label,
description: xml.LightningComponentBundle.description ?? '',
};
})
)
).filter((component) => !!component);
if (!clientSelect) {
if (componentName) {
// validate that the component exists before launching the server
const match = components.find(
(component) => componentName === component.name || componentName === component.label
);
if (!match) {
throw new Error(messages.getMessage('error.component-not-found', [componentName]));
}
componentName = match.name;
} else {
// prompt the user for a name if one was not provided
componentName = await PromptUtils.promptUserToSelectComponent(components);
if (!componentName) {
throw new Error(messages.getMessage('error.component'));
}
}
}
await startLWCServer(logger, sfdxProjectRootPath, ldpServerToken, Platform.desktop, serverPorts);
const targetOrgArg = PreviewUtils.getTargetOrgFromArguments(this.argv);
const launchArguments = PreviewUtils.generateComponentPreviewLaunchArguments(
ldpServerUrl,
ldpServerId,
componentName,
targetOrgArg
);
// Open the browser and navigate to the right page
await this.config.runCommand('org:open', launchArguments);
}
}