-
Notifications
You must be signed in to change notification settings - Fork 12
feat(dev-server): integrate with preview app command #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
85887e3
bcc6e59
3ce5311
b47e069
249ad28
bea501e
8fd2324
63417d1
c8707b5
5a8332b
8e26a21
008ef58
908f99f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Copyright (c) 2023, salesforce.com, inc. | ||
* All rights reserved. | ||
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
|
||
import { existsSync, lstatSync, readFileSync } from 'node:fs'; | ||
import path from 'node:path'; | ||
import process from 'node:process'; | ||
import { LWCServer, LogLevel, ServerConfig, Workspace, startLwcDevServer } from '@lwc/lwc-dev-server'; | ||
import { Logger } from '@salesforce/core'; | ||
|
||
const DEV_SERVER_PORT = 8081; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The port needs to be configurable. This a pretty popular port number and we should anticipate that it might not be available. Can we add a new flag to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ravijayaramappa we may need to run it by Marcelino to discuss this. For example:
Maybe we should utilize our slack channel to discuss further with him and the devs? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like that idea of just putting it in the |
||
|
||
/** | ||
* Map sf cli log level to lwc dev server log level | ||
* https://github.com/salesforcecli/cli/wiki/Code-Your-Plugin#logging-levels | ||
* | ||
* @param cliLogLevel | ||
* @returns number | ||
*/ | ||
function mapLogLevel(cliLogLevel: number): number { | ||
switch (cliLogLevel) { | ||
case 10: | ||
return LogLevel.verbose; | ||
case 20: | ||
return LogLevel.debug; | ||
case 30: | ||
return LogLevel.info; | ||
case 40: | ||
return LogLevel.warn; | ||
case 50: | ||
return LogLevel.error; | ||
case 60: | ||
return LogLevel.silent; | ||
default: | ||
return LogLevel.error; | ||
} | ||
} | ||
|
||
function createLWCServerConfig(rootDir: string, logger: Logger): ServerConfig { | ||
const sfdxConfig = path.resolve(rootDir, 'sfdx-project.json'); | ||
|
||
if (!existsSync(sfdxConfig) || !lstatSync(sfdxConfig).isFile()) { | ||
throw new Error(`sfdx-project.json not found in ${rootDir}`); | ||
} | ||
|
||
const sfdxConfigJson = readFileSync(sfdxConfig, 'utf-8'); | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
const { packageDirectories } = JSON.parse(sfdxConfigJson); | ||
const namespacePaths: string[] = []; | ||
|
||
for (const dir of packageDirectories) { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access | ||
if (dir.path) { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-argument | ||
const resolvedDir = path.resolve(rootDir, dir.path, 'main', 'default'); | ||
if (existsSync(resolvedDir) && lstatSync(resolvedDir).isDirectory()) { | ||
logger.debug(`Adding ${resolvedDir} to namespace paths`); | ||
namespacePaths.push(resolvedDir); | ||
} else { | ||
logger.warn(`Skipping ${resolvedDir} because it does not exist or is not a directory`); | ||
} | ||
} | ||
} | ||
|
||
return { | ||
rootDir, | ||
port: DEV_SERVER_PORT, | ||
protocol: 'wss', | ||
host: 'localhost', | ||
rax-it marked this conversation as resolved.
Show resolved
Hide resolved
|
||
paths: namespacePaths, | ||
workspace: Workspace.SfCli, | ||
targets: ['LEX'], // should this be something else? | ||
rax-it marked this conversation as resolved.
Show resolved
Hide resolved
|
||
logLevel: mapLogLevel(logger.getLevel()), | ||
}; | ||
} | ||
|
||
export async function startLWCServer(rootDir: string, logger: Logger): Promise<LWCServer> { | ||
const config = createLWCServerConfig(rootDir, logger); | ||
logger.trace(`Starting LWC Dev Server with config: ${JSON.stringify(config)}`); | ||
let lwcDevServer: LWCServer | null = await startLwcDevServer(config); | ||
|
||
const cleanup = (): void => { | ||
if (lwcDevServer) { | ||
logger.trace('Stopping LWC Dev Server'); | ||
lwcDevServer.stopServer(); | ||
lwcDevServer = null; | ||
} | ||
}; | ||
|
||
[ | ||
'exit', // normal exit flow | ||
'SIGINT', // when a user presses ctrl+c | ||
'SIGTERM', // when a user kills the process | ||
].forEach((signal) => process.on(signal, cleanup)); | ||
|
||
return lwcDevServer; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// Intentionally blank file. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// Intentionally blank file. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"packageDirectories": [ | ||
{ | ||
"path": "force-app" | ||
} | ||
] | ||
} |
Uh oh!
There was an error while loading. Please reload this page.