Skip to content

Commit c5852d2

Browse files
committed
temp changes
1 parent 61f06b8 commit c5852d2

File tree

4 files changed

+118
-21
lines changed

4 files changed

+118
-21
lines changed

src/commands/lightning/dev/site.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
*/
77
import fs from 'node:fs';
88
import { SfCommand, Flags } from '@salesforce/sf-plugins-core';
9-
import { Messages } from '@salesforce/core';
9+
import { Logger, Messages } from '@salesforce/core';
1010
import { expDev, SitesLocalDevOptions, setupDev } from '@lwrjs/api';
1111
import { OrgUtils } from '../../../shared/orgUtils.js';
1212
import { PromptUtils } from '../../../shared/promptUtils.js';
1313
import { ExperienceSite } from '../../../shared/experience/expSite.js';
14-
import 'dotenv/config';
14+
import { getExperienceSiteConfig } from '../../../shared/experience/expSiteConfig.js';
1515

1616
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
1717
const messages = Messages.loadMessages('@salesforce/plugin-lightning-dev', 'lightning.dev.site');
@@ -37,6 +37,7 @@ export default class LightningDevSite extends SfCommand<void> {
3737

3838
public async run(): Promise<void> {
3939
const { flags } = await this.parse(LightningDevSite);
40+
const logger = await Logger.child(this.ctor.name);
4041

4142
try {
4243
const org = flags['target-org'];
@@ -52,6 +53,9 @@ export default class LightningDevSite extends SfCommand<void> {
5253

5354
OrgUtils.ensureMatchingAPIVersion(connection);
5455

56+
// Load up any environment configuration
57+
const config = getExperienceSiteConfig();
58+
5559
// If user doesn't specify a site, prompt the user for one
5660
if (!siteName) {
5761
const allSites = await ExperienceSite.getAllExpSites(org);
@@ -85,24 +89,20 @@ export default class LightningDevSite extends SfCommand<void> {
8589
// Establish a valid access token for this site
8690
const authToken = await selectedSite.setupAuth();
8791

88-
// Start the dev server
89-
const port = parseInt(process.env.PORT ?? '3000', 10);
90-
91-
const logLevel = process.env.LOG_LEVEL ?? 'error';
92-
92+
// Start the dev server w/ configured parameters
9393
const startupParams: SitesLocalDevOptions = {
94-
sfCLI: process.env.INTERNAL_DEVELOPER_MODE !== 'true',
94+
sfCLI: !config.lwcConfigEnabled,
9595
authToken,
96-
open: process.env.OPEN_BROWSER === 'false' ? false : true,
97-
port,
98-
logLevel,
96+
open: config.openBrowser,
97+
port: config.port,
98+
logLevel: config.logLevel,
9999
mode: 'dev',
100100
siteZip,
101101
siteDir: selectedSite.getSiteDirectory(),
102102
};
103103

104104
// Environment variable used to setup the site rather than setup & start server
105-
if (process.env.SETUP_ONLY === 'true') {
105+
if (config.setupOnly) {
106106
await setupDev(startupParams);
107107
this.log('[local-dev] setup complete!');
108108
} else {
@@ -111,7 +111,7 @@ export default class LightningDevSite extends SfCommand<void> {
111111
}
112112
} catch (e) {
113113
this.spinner.stop('failed.');
114-
this.log('Local Development setup failed', e);
114+
logger.error('Local Development setup failed', e);
115115
}
116116
}
117117
}

src/shared/experience/expSite.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,8 @@ export class ExperienceSite {
314314
this.logger.debug('Checking if site is set up');
315315
const ssrJsPath = path.join(this.getExtractDirectory(), 'ssr.js');
316316

317+
// Verifies that we have an extracted site at the expected location
318+
// Verifies that our site metadata is the latest
317319
if (fs.existsSync(ssrJsPath)) {
318320
this.logger.debug('ssr.js file exists, checking metadata');
319321
const metadata = await this.getLocalMetadata();
@@ -686,7 +688,7 @@ export class ExperienceSite {
686688
this.logger.debug(`Resource will be saved to: ${resourcePath}`);
687689

688690
try {
689-
const apiUrl = `${instanceUrl}/services/data/${this.config.apiVersion}/sites/${siteIdMinus3}/preview${this.apiQueryParams}`;
691+
const apiUrl = `${instanceUrl}/services/data/v${conn.version}/sites/${siteIdMinus3}/preview${this.apiQueryParams}`;
690692
this.logger.debug(`API URL: ${apiUrl}`);
691693

692694
this.logger.debug('Sending API request');

src/shared/experience/expSiteConfig.ts

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,48 @@
44
* Licensed under the BSD 3-Clause license.
55
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
7+
/* eslint-disable no-console */
8+
// import { Logger } from '@salesforce/core';
79
import 'dotenv/config';
810

911
export type ExperienceSiteConfig = {
1012
previewUser: string;
1113
previewToken: string;
1214
apiStaticMode: boolean;
1315
apiBundlingGroups: boolean;
14-
apiVersion: string;
1516
apiSiteVersion: string;
17+
logLevel: string;
18+
port: number;
19+
openBrowser: boolean;
20+
lwcConfigEnabled: boolean;
21+
setupOnly: boolean;
1622
};
1723

24+
/**
25+
* Environment variables that are considered experimental
26+
*/
27+
const EXPERIMENTAL_ENV_VARS = [
28+
'PREVIEW_USER',
29+
'SID_TOKEN',
30+
'API_STATIC_MODE',
31+
'API_BUNDLING_GROUPS',
32+
'API_SITE_VERSION',
33+
'SETUP_ONLY',
34+
'LWC_CONFIG_ENABLED',
35+
];
36+
37+
/**
38+
* Experimental configuration options for Experience Sites local development
39+
*
40+
*/
1841
export class ExperienceSiteConfigManager {
1942
private static instance: ExperienceSiteConfigManager;
2043
private config: ExperienceSiteConfig;
2144

2245
private constructor() {
46+
// Show a warning
47+
checkExperimentalConfig();
48+
2349
// Backwards Compat
2450
if (process.env.SITE_GUEST_ACCESS === 'true') {
2551
process.env.PREVIEW_USER = 'Guest';
@@ -28,13 +54,29 @@ export class ExperienceSiteConfigManager {
2854
process.env.PREVIEW_USER = 'Custom';
2955
}
3056

57+
// Experimental configuration values
3158
this.config = {
32-
previewUser: process.env.PREVIEW_USER ?? 'Admin',
33-
previewToken: process.env.SID_TOKEN ?? '',
34-
apiStaticMode: process.env.API_STATIC_MODE === 'true' ? true : false,
35-
apiBundlingGroups: process.env.API_BUNDLING_GROUPS === 'true' ? true : false,
36-
apiVersion: process.env.API_VERSION ?? 'v64.0',
37-
apiSiteVersion: process.env.API_SITE_VERSION ?? 'published',
59+
// what user to preview site with
60+
previewUser: process.env.PREVIEW_USER ?? 'Admin', // Default: Admin
61+
// Override the authentication token for this user
62+
previewToken: process.env.SID_TOKEN ?? '', // Default: No Override (empty string)
63+
// download from static resources instead of the API
64+
apiStaticMode: process.env.API_STATIC_MODE === 'true' ? true : false, // Default: false
65+
// use bundling groups or not - testing purposes only
66+
apiBundlingGroups: process.env.API_BUNDLING_GROUPS === 'true' ? true : false, // Default: false
67+
// What version of the API to use
68+
apiSiteVersion: process.env.API_SITE_VERSION ?? 'published', // Default: published
69+
// Log level supplied to the LWR server
70+
logLevel: process.env.LOG_LEVEL ?? 'error', // Default: error
71+
// Port to run the LWR server
72+
port: parseInt(process.env.PORT ?? '3000', 10), // Default: 3000
73+
// Should we automatically open the browser?
74+
openBrowser: process.env.OPEN_BROWSER === 'false' ? false : true, // Default: true
75+
// Enable lwc module resolution outside the context of SFDX
76+
lwcConfigEnabled: process.env.LWC_CONFIG_ENABLED === 'true' ? true : false, // Default: false
77+
// Skip running the server, just setup the site
78+
setupOnly: process.env.SETUP_ONLY === 'true' ? true : false, // Default: false
79+
// TODO Add option for running the site in preview only mode (no local changes included)
3880
};
3981
}
4082

@@ -80,3 +122,21 @@ export class ExperienceSiteConfigManager {
80122
export function getExperienceSiteConfig(): ExperienceSiteConfig {
81123
return ExperienceSiteConfigManager.getInstance().getConfig();
82124
}
125+
126+
/**
127+
* Checks if any experimental environment variables are set and logs a warning if so.
128+
* The warning is only shown once per command execution.
129+
*/
130+
export function checkExperimentalConfig(): void {
131+
// Check if any experimental env vars are set
132+
const usedEnvVars = EXPERIMENTAL_ENV_VARS.filter((envVar) => process.env[envVar] !== undefined);
133+
134+
if (usedEnvVars.length > 0) {
135+
// Log a warning
136+
console.warn('\x1b[33m%s\x1b[0m', '⚠️ EXPERIMENTAL CONFIGURATION OPTIONS DETECTED ⚠️');
137+
console.warn(
138+
'\x1b[33m%s\x1b[0m',
139+
'These configuration options are experimental and may change without notice. Please refer to the official documentation for supported options.'
140+
);
141+
}
142+
}

src/shared/logUtils.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (c) 2024, salesforce.com, inc.
3+
* All rights reserved.
4+
* Licensed under the BSD 3-Clause license.
5+
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
6+
*/
7+
/* eslint-disable no-console */
8+
9+
class Logger {
10+
public static info(message: unknown): void {
11+
console.log(Logger.formatMessage('36', 'ℹ️ INFO:', message)); // Cyan
12+
}
13+
14+
public static warn(message: unknown): void {
15+
console.warn(Logger.formatMessage('33', '⚠️ WARN:', message)); // Yellow
16+
}
17+
18+
public static error(message: unknown): void {
19+
console.error(Logger.formatMessage('31', '❌ ERROR:', message)); // Red
20+
}
21+
22+
public static debug(message: unknown): void {
23+
console.debug(Logger.formatMessage('2', '🐛 DEBUG:', message)); // Dim
24+
}
25+
26+
public static success(message: unknown): void {
27+
console.log(Logger.formatMessage('32', '✅ SUCCESS:', message)); // Green
28+
}
29+
30+
private static formatMessage(color: string, label: string, message: unknown): string {
31+
return `\x1b[${color}m${label} ${message}\x1b[0m`;
32+
}
33+
}
34+
35+
export default Logger;

0 commit comments

Comments
 (0)