-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsite.ts
More file actions
112 lines (96 loc) · 4.64 KB
/
site.ts
File metadata and controls
112 lines (96 loc) · 4.64 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
/*
* 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 { SfCommand, Flags } from '@salesforce/sf-plugins-core';
import { Connection, Logger, Messages, SfProject } from '@salesforce/core';
import { Platform } from '@salesforce/lwc-dev-mobile-core';
import open from 'open';
import { OrgUtils } from '../../../shared/orgUtils.js';
import { PromptUtils } from '../../../shared/promptUtils.js';
import { ExperienceSite } from '../../../shared/experience/expSite.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.site');
const sharedMessages = Messages.loadMessages('@salesforce/plugin-lightning-dev', 'shared.utils');
export default class LightningDevSite 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 enableJsonFlag = false; // Disable json flag since we don't return anything
public static readonly flags = {
name: Flags.string({
summary: messages.getMessage('flags.name.summary'),
char: 'n',
}),
'target-org': Flags.requiredOrg(),
};
public async run(): Promise<void> {
const { flags } = await this.parse(LightningDevSite);
try {
const org = flags['target-org'];
let siteName = flags.name;
const connection = org.getConnection(undefined);
const localDevEnabled = await OrgUtils.isLocalDevEnabled(connection);
if (!localDevEnabled) {
throw new Error(sharedMessages.getMessage('error.localdev.not.enabled'));
}
OrgUtils.ensureMatchingAPIVersion(connection);
// If user doesn't specify a site, prompt the user for one
if (!siteName) {
const allSites = await ExperienceSite.getAllExpSites(org);
siteName = await PromptUtils.promptUserToSelectSite(allSites);
}
const selectedSite = new ExperienceSite(org, siteName);
return await this.openPreviewUrl(selectedSite, connection);
} catch (e) {
this.spinner.stop('failed.');
this.log('Local Development setup failed', e);
}
}
private async openPreviewUrl(selectedSite: ExperienceSite, connection: Connection): Promise<void> {
let sfdxProjectRootPath = '';
try {
sfdxProjectRootPath = await SfProject.resolveProjectPath();
} catch (error) {
throw new Error(sharedMessages.getMessage('error.no-project', [(error as Error)?.message ?? '']));
}
const previewUrl = await selectedSite.getPreviewUrl();
const username = connection.getUsername();
if (!username) {
throw new Error(sharedMessages.getMessage('error.username'));
}
this.log('Configuring local web server identity');
const appServerIdentity = await PreviewUtils.getOrCreateAppServerIdentity(connection);
const ldpServerToken = appServerIdentity.identityToken;
const ldpServerId = appServerIdentity.usernameToServerEntityIdMap[username];
if (!ldpServerId) {
throw new Error(sharedMessages.getMessage('error.identitydata.entityid'));
}
this.log('Determining the next available port for Local Dev Server');
const serverPorts = await PreviewUtils.getNextAvailablePorts();
this.log(`Next available ports are http=${serverPorts.httpPort} , https=${serverPorts.httpsPort}`);
this.log('Determining Local Dev Server url');
const ldpServerUrl = PreviewUtils.generateWebSocketUrlForLocalDevServer(Platform.desktop, serverPorts);
this.log(`Local Dev Server url is ${ldpServerUrl}`);
const logger = await Logger.child(this.ctor.name);
await startLWCServer(logger, sfdxProjectRootPath, ldpServerToken, Platform.desktop, serverPorts);
const url = new URL(previewUrl);
url.searchParams.set('aura.ldpServerUrl', ldpServerUrl);
url.searchParams.set('aura.ldpServerId', ldpServerId);
url.searchParams.set('lwc.mode', 'dev');
await open(url.toString());
}
}