Skip to content

Commit 5bf0ead

Browse files
refactor: remove dead code and optimize for readability
- Remove unreachable createGenericError method in DevServerErrorParser (catch-all regex pattern /.*/ makes fallback code unreachable) - Extract resolveWebappName helper to eliminate duplicate name priority logic - Remove redundant dot-prefixed entries from EXCLUDED_DIRECTORIES (already handled by startsWith('.') check) - Optimize handleProcessOutput to split lines once instead of twice
1 parent 685dc1e commit 5bf0ead

3 files changed

Lines changed: 26 additions & 61 deletions

File tree

src/config/webappDiscovery.ts

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,10 @@ export type DiscoveredWebapp = {
5555
};
5656

5757
/**
58-
* Directories to exclude when processing webapplications folder
58+
* Directories to exclude when processing webapplications folder.
59+
* Note: Directories starting with '.' are excluded separately in shouldExcludeDirectory()
5960
*/
60-
const EXCLUDED_DIRECTORIES = new Set([
61-
'node_modules',
62-
'.git',
63-
'dist',
64-
'build',
65-
'out',
66-
'coverage',
67-
'.next',
68-
'.nuxt',
69-
'.output',
70-
'__pycache__',
71-
'.venv',
72-
'venv',
73-
]);
61+
const EXCLUDED_DIRECTORIES = new Set(['node_modules', 'dist', 'build', 'out', 'coverage', '__pycache__', 'venv']);
7462

7563
/**
7664
* Check if a directory should be excluded from processing
@@ -137,6 +125,21 @@ async function tryParseWebappManifest(filePath: string): Promise<WebAppManifest
137125
}
138126
}
139127

128+
/**
129+
* Resolve webapp name using priority: manifest.name > metaXmlName > folderName
130+
*
131+
* @param folderName - The folder name (fallback)
132+
* @param metaXmlName - Name extracted from .webapplication-meta.xml (or null)
133+
* @param manifest - Parsed manifest (or null)
134+
* @returns The resolved webapp name
135+
*/
136+
function resolveWebappName(folderName: string, metaXmlName: string | null, manifest: WebAppManifest | null): string {
137+
if (manifest?.name && typeof manifest.name === 'string' && manifest.name.trim()) {
138+
return manifest.name;
139+
}
140+
return metaXmlName ?? folderName;
141+
}
142+
140143
/**
141144
* Try to resolve SFDX project root from a given directory.
142145
* Uses SfProject from @salesforce/core which walks up directories to find sfdx-project.json.
@@ -252,19 +255,11 @@ async function discoverWebappsInFolder(webappsFolderPath: string, cwd: string):
252255
// Try to load manifest for dev configuration
253256
const manifest = await tryParseWebappManifest(manifestFilePath);
254257

255-
// Name priority: manifest.name > meta.xml name > folder name
256-
let webappName = entry.name;
257-
if (manifest?.name && typeof manifest.name === 'string' && manifest.name.trim()) {
258-
webappName = manifest.name;
259-
} else if (metaXmlName) {
260-
webappName = metaXmlName;
261-
}
262-
263258
return {
264259
path: webappPath,
265260
relativePath: relative(cwd, webappPath) || entry.name,
266261
manifest,
267-
name: webappName,
262+
name: resolveWebappName(entry.name, metaXmlName, manifest),
268263
hasManifest: manifest !== null,
269264
manifestPath: manifest ? manifestFilePath : null,
270265
hasMetaXml: true,
@@ -340,14 +335,7 @@ async function findAllWebapps(cwd: string = process.cwd()): Promise<FindAllWebap
340335
// Current directory is a standalone webapp
341336
const manifestFilePath = join(cwd, 'webapplication.json');
342337
const manifest = await tryParseWebappManifest(manifestFilePath);
343-
344-
// Name priority: manifest.name > meta.xml name > folder name
345-
let webappName = basename(cwd);
346-
if (manifest?.name && typeof manifest.name === 'string' && manifest.name.trim()) {
347-
webappName = manifest.name;
348-
} else if (metaXmlName) {
349-
webappName = metaXmlName;
350-
}
338+
const webappName = resolveWebappName(basename(cwd), metaXmlName, manifest);
351339

352340
const standaloneWebapp: DiscoveredWebapp = {
353341
path: cwd,

src/error/DevServerErrorParser.ts

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,9 @@ export class DevServerErrorParser {
189189
}
190190
}
191191

192-
// Fallback (should never reach here due to catch-all pattern)
193-
return this.createGenericError(stderr, exitCode, signal);
192+
// This point is unreachable because the last pattern (.*) matches everything
193+
// TypeScript requires a return statement, so we throw an error for safety
194+
throw new Error('Unreachable: ERROR_PATTERNS catch-all should always match');
194195
}
195196

196197
/**
@@ -216,29 +217,4 @@ export class DevServerErrorParser {
216217
// Return last N lines (most recent errors)
217218
return lines.slice(-maxLines);
218219
}
219-
220-
/**
221-
* Create a generic error when no specific pattern matches
222-
*
223-
* @param stderr - Full stderr output
224-
* @param exitCode - Process exit code
225-
* @param signal - Process exit signal
226-
* @returns Generic DevServerError
227-
*/
228-
private static createGenericError(stderr: string, exitCode?: number | null, signal?: string | null): DevServerError {
229-
return {
230-
type: 'unknown',
231-
title: 'Dev Server Failed to Start',
232-
message: 'The dev server encountered an error. Check the error output below for details.',
233-
stderrLines: this.extractRelevantLines(stderr, 15),
234-
suggestions: [
235-
'Review the error output above',
236-
'Try running your dev command manually to debug',
237-
'Verify your project setup is correct',
238-
],
239-
fullStderr: stderr,
240-
exitCode,
241-
signal,
242-
};
243-
}
244220
}

src/server/DevServerManager.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,11 @@ export class DevServerManager extends EventEmitter {
346346
// Emit output event for consumers
347347
this.emit(stream, output);
348348

349+
// Split lines once and reuse for all operations
350+
const lines = output.split('\n').filter((line) => line.trim());
351+
349352
// Capture stderr lines for error parsing
350353
if (stream === 'stderr') {
351-
const lines = output.split('\n').filter((line) => line.trim());
352354
this.stderrBuffer.push(...lines);
353355

354356
// Keep only the last N lines to prevent memory issues
@@ -358,7 +360,6 @@ export class DevServerManager extends EventEmitter {
358360
}
359361

360362
// Log dev server output (only visible when SF_LOG_LEVEL=debug)
361-
const lines = output.split('\n').filter((line) => line.trim());
362363
for (const line of lines) {
363364
this.logger.debug(`[Dev Server ${stream}] ${line}`);
364365
}

0 commit comments

Comments
 (0)