Skip to content

Commit fad188c

Browse files
feat: prompt for app and site, added coverage
1 parent db5e58c commit fad188c

File tree

10 files changed

+705
-68
lines changed

10 files changed

+705
-68
lines changed

messages/shared.utils.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ The SSL certificate data to be used by the local dev server for secure connectio
2626

2727
You must provide valid SSL certificate data
2828

29+
# localdev.enabled
30+
31+
Local dev has been enabled for this org.
32+
2933
# error.localdev.not.enabled
3034

3135
Local Dev is not enabled for your org. See https://developer.salesforce.com/docs/platform/lwc/guide/get-started-test-components.html for more information on enabling and using Local Dev.

src/commands/lightning/dev/app.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { Flags, SfCommand } from '@salesforce/sf-plugins-core';
2929
import { startLWCServer } from '../../../lwc-dev-server/index.js';
3030
import { PreviewUtils } from '../../../shared/previewUtils.js';
3131
import { PromptUtils } from '../../../shared/promptUtils.js';
32+
import { MetaUtils } from '../../../shared/metaUtils.js';
3233

3334
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
3435
const messages = Messages.loadMessages('@salesforce/plugin-lightning-dev', 'lightning.dev.app');
@@ -86,6 +87,11 @@ export default class LightningDevApp extends SfCommand<void> {
8687
}
8788

8889
logger.debug('Initalizing preview connection and configuring local web server identity');
90+
91+
if (await MetaUtils.handleLocalDevEnablement(targetOrg.getConnection(undefined))) {
92+
this.log(sharedMessages.getMessage('localdev.enabled'));
93+
}
94+
8995
const { connection, ldpServerId, ldpServerToken } = await PreviewUtils.initializePreviewConnection(targetOrg);
9096

9197
const platform = flags['device-type'] ?? (await PromptUtils.promptUserToSelectPlatform());

src/commands/lightning/dev/component.ts

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -75,29 +75,8 @@ export default class LightningDevComponent extends SfCommand<ComponentPreviewRes
7575
const targetOrg = flags['target-org'];
7676
const apiVersion = flags['api-version'];
7777

78-
// If local dev is not enabled, prompt the user to enable local dev
79-
const setupConnection = targetOrg.getConnection(undefined);
80-
const isLightningPreviewEnabled = await MetaUtils.isLightningPreviewEnabled(setupConnection);
81-
82-
if (!isLightningPreviewEnabled) {
83-
const autoEnableLocalDev = process.env.AUTO_ENABLE_LOCAL_DEV;
84-
85-
// If executed via VSCode command, autoEnableLocalDev will contain the users choice, provided via UI.
86-
// Else, prompt the user on the command line.
87-
const enableLocalDev =
88-
autoEnableLocalDev !== undefined
89-
? autoEnableLocalDev === 'true'
90-
: await PromptUtils.promptUserToEnableLocalDev();
91-
92-
if (enableLocalDev) {
93-
try {
94-
await MetaUtils.setLightningPreviewEnabled(setupConnection, true);
95-
await MetaUtils.ensureFirstPartyCookiesNotRequired(setupConnection);
96-
this.log('Local dev has been enabled for this org.');
97-
} catch (error) {
98-
this.log('Error autoenabling local dev', error);
99-
}
100-
}
78+
if (await MetaUtils.handleLocalDevEnablement(targetOrg.getConnection(undefined))) {
79+
this.log(sharedMessages.getMessage('localdev.enabled'));
10180
}
10281

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

src/commands/lightning/dev/site.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { PromptUtils } from '../../../shared/promptUtils.js';
2424
import { ExperienceSite } from '../../../shared/experience/expSite.js';
2525
import { PreviewUtils } from '../../../shared/previewUtils.js';
2626
import { startLWCServer } from '../../../lwc-dev-server/index.js';
27+
import { MetaUtils } from '../../../shared/metaUtils.js';
2728

2829
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
2930
const messages = Messages.loadMessages('@salesforce/plugin-lightning-dev', 'lightning.dev.site');
@@ -67,9 +68,8 @@ export default class LightningDevSite extends SfCommand<void> {
6768

6869
const connection = org.getConnection(undefined);
6970

70-
const localDevEnabled = await OrgUtils.isLocalDevEnabled(connection);
71-
if (!localDevEnabled) {
72-
throw new Error(sharedMessages.getMessage('error.localdev.not.enabled'));
71+
if (await MetaUtils.handleLocalDevEnablement(connection)) {
72+
this.log(sharedMessages.getMessage('localdev.enabled'));
7373
}
7474

7575
OrgUtils.ensureMatchingAPIVersion(connection);

src/shared/metaUtils.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { Connection, Logger } from '@salesforce/core';
17+
import { Connection, Logger, Messages } from '@salesforce/core';
18+
import { PromptUtils } from './promptUtils.js';
1819

1920
type LightningExperienceSettingsMetadata = {
2021
[key: string]: unknown;
@@ -34,6 +35,8 @@ type MetadataUpdateResult = {
3435
errors?: Array<{ message: string }>;
3536
};
3637

38+
const sharedMessages = Messages.loadMessages('@salesforce/plugin-lightning-dev', 'shared.utils');
39+
3740
/**
3841
* Utility class for managing Salesforce metadata settings related to Lightning Development.
3942
*/
@@ -217,4 +220,36 @@ export class MetaUtils {
217220
this.logger.debug('First-party cookies are not required');
218221
return true;
219222
}
223+
224+
/**
225+
* Enables local dev if required and permitted. If executed via VSCode command
226+
* the user's response is already assigned to AUTO_ENABLE_LOCAL_DEV and it will be used.
227+
* If executed via command line, this method will prompt the user.
228+
*
229+
* @param connection the connection to the org
230+
* @returns true if enabled
231+
* @throws local dev not enabled error if not enabled
232+
*/
233+
public static async handleLocalDevEnablement(connection: Connection): Promise<boolean | undefined> {
234+
const isLightningPreviewEnabled = await this.isLightningPreviewEnabled(connection);
235+
236+
if (!isLightningPreviewEnabled) {
237+
const autoEnableLocalDev = process.env.AUTO_ENABLE_LOCAL_DEV;
238+
239+
// If executed via VSCode command, autoEnableLocalDev will contain the users choice, provided via UI.
240+
// Else, prompt the user on the command line.
241+
const enableLocalDev =
242+
autoEnableLocalDev !== undefined
243+
? autoEnableLocalDev === 'true'
244+
: await PromptUtils.promptUserToEnableLocalDev();
245+
246+
if (enableLocalDev) {
247+
await this.setLightningPreviewEnabled(connection, true);
248+
await this.ensureFirstPartyCookiesNotRequired(connection);
249+
return true;
250+
} else {
251+
throw new Error(sharedMessages.getMessage('error.localdev.not.enabled'));
252+
}
253+
}
254+
}
220255
}

src/shared/previewUtils.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -433,11 +433,6 @@ export class PreviewUtils {
433433
return Promise.reject(new Error(sharedMessages.getMessage('error.username')));
434434
}
435435

436-
const localDevEnabled = await OrgUtils.isLocalDevEnabled(connection);
437-
if (!localDevEnabled) {
438-
return Promise.reject(new Error(sharedMessages.getMessage('error.localdev.not.enabled')));
439-
}
440-
441436
OrgUtils.ensureMatchingAPIVersion(connection);
442437

443438
const appServerIdentity = await PreviewUtils.getOrCreateAppServerIdentity(connection);

0 commit comments

Comments
 (0)