Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

refactor: extract the per-process vite-config port lookup out of streamDetection's 8-deep branch (#3452)

[._.]/ Adam Eivya5f573e587ae
diff --git a/server/services/streamingDetect.js b/server/services/streamingDetect.js
index 0443786bc6..c5f840ec49 100644
--- a/server/services/streamingDetect.js
+++ b/server/services/streamingDetect.js
@@ -451,6 +451,35 @@ function extractVitePort(content) {
return portMatch ? parseInt(portMatch[1], 10) : null;
}
+/** vite config filenames probed inside a PM2 process's `cwd`, in probe order. */
+const VITE_CONFIG_FILENAMES = ['vite.config.ts', 'vite.config.js'];
+
+/**
+ * Resolve the dev-server port for a single parsed PM2 process by reading the
+ * vite config in its `cwd`, relative to the app root `dirPath`.
+ *
+ * Only applies to a process that runs Vite and carries no port of its own — any
+ * other process resolves to `null` so callers can assign unconditionally. The
+ * first config file that yields a port wins; an unreadable file is treated as
+ * empty (no port) and the next candidate is probed.
+ *
+ * @param {string} dirPath - app root the process's `cwd` is relative to
+ * @param {{ usesVite?: boolean, port?: number|null, cwd?: string }} proc
+ * @returns {Promise<number|null>} the resolved port, or null when none applies
+ */
+export async function resolveViteConfigPortForProcess(dirPath, proc) {
+ if (!proc?.usesVite || proc.port || !proc.cwd) return null;
+ const cwdPath = join(dirPath, proc.cwd);
+ for (const viteConfig of VITE_CONFIG_FILENAMES) {
+ const viteConfigPath = join(cwdPath, viteConfig);
+ if (!existsSync(viteConfigPath)) continue;
+ const viteContent = await readFile(viteConfigPath, 'utf-8').catch(() => '');
+ const port = extractVitePort(viteContent);
+ if (port) return port;
+ }
+ return null;
+}
+
/**
* Parse ecosystem config from a directory path (non-streaming, for refresh)
* Also checks vite.config files in subdirectories for processes that use Vite
@@ -465,20 +494,8 @@ export async function parseEcosystemFromPath(dirPath) {
// For processes that use vite and don't have a port, check their cwd for vite.config
for (const proc of processes) {
- if (proc.usesVite && !proc.port && proc.cwd) {
- const cwdPath = join(dirPath, proc.cwd);
- for (const viteConfig of ['vite.config.ts', 'vite.config.js']) {
- const viteConfigPath = join(cwdPath, viteConfig);
- if (existsSync(viteConfigPath)) {
- const viteContent = await readFile(viteConfigPath, 'utf-8').catch(() => '');
- const port = extractVitePort(viteContent);
- if (port) {
- proc.port = port;
- break;
- }
- }
- }
- }
+ const vitePort = await resolveViteConfigPortForProcess(dirPath, proc);
+ if (vitePort) proc.port = vitePort;
// Clean up internal properties before returning
delete proc.cwd;
delete proc.usesVite;
@@ -1225,7 +1242,7 @@ export async function streamDetection(socket, dirPath) {
}
// Check ecosystem.config.js/cjs for PM2 configuration
- for (const ecosystemFile of ['ecosystem.config.js', 'ecosystem.config.cjs']) {
+ for (const ecosystemFile of ECOSYSTEM_CONFIG_FILENAMES) {
const ecosystemPath = join(dirPath, ecosystemFile);
if (existsSync(ecosystemPath)) {
const content = await readFile(ecosystemPath, 'utf-8').catch(() => '');
@@ -1238,24 +1255,12 @@ export async function streamDetection(socket, dirPath) {
if (parsedProcesses.length > 0) {
// For processes that use vite and don't have a port, check their cwd for vite.config
for (const proc of parsedProcesses) {
- if (proc.usesVite && !proc.port && proc.cwd) {
- const cwdPath = join(dirPath, proc.cwd);
- for (const viteConfig of ['vite.config.ts', 'vite.config.js']) {
- const viteConfigPath = join(cwdPath, viteConfig);
- if (existsSync(viteConfigPath)) {
- const viteContent = await readFile(viteConfigPath, 'utf-8').catch(() => '');
- const port = extractVitePort(viteContent);
- if (port) {
- proc.port = port;
- break;
- }
- }
- }
- }
+ const vitePort = await resolveViteConfigPortForProcess(dirPath, proc);
+ if (vitePort) proc.port = vitePort;
// Clean up internal properties
delete proc.cwd;
delete proc.usesVite;
- }
+ }
result.processes = parsedProcesses;
result.pm2ProcessNames = parsedProcesses.map(p => p.name);
diff --git a/server/services/streamingDetect.test.js b/server/services/streamingDetect.test.js
index e73a7cd7af..29a82bf688 100644
--- a/server/services/streamingDetect.test.js
+++ b/server/services/streamingDetect.test.js
@@ -2,7 +2,7 @@ import { describe, it, expect, afterEach } from 'vitest';
import { mkdirSync, mkdtempSync, writeFileSync, readFileSync, rmSync } from 'fs';
import { tmpdir } from 'os';
import { join } from 'path';
-import { detectGodotNativeLaunch, parseEcosystemConfig, rewriteEcosystemPorts, rewriteEcosystemPortsByProcess, writeEcosystemPorts, writeEcosystemPortsByProcess, writeEcosystemPortEdits, DESKTOP_TYPES, NON_PM2_TYPES } from './streamingDetect.js';
+import { detectGodotNativeLaunch, parseEcosystemConfig, resolveViteConfigPortForProcess, rewriteEcosystemPorts, rewriteEcosystemPortsByProcess, writeEcosystemPorts, writeEcosystemPortsByProcess, writeEcosystemPortEdits, DESKTOP_TYPES, NON_PM2_TYPES } from './streamingDetect.js';
describe('detectGodotNativeLaunch', () => {
let dir;
@@ -957,6 +957,57 @@ module.exports = { apps: [
//
// The assertion lives HERE, not in a client test, and the direction matters: the
// client module is dependency-free so a server test can import it, whereas
+describe('resolveViteConfigPortForProcess', () => {
+ let dir;
+ afterEach(() => { if (dir) rmSync(dir, { recursive: true, force: true }); dir = null; });
+
+ const writeViteConfig = (relDir, name, body) => {
+ mkdirSync(join(dir, relDir), { recursive: true });
+ writeFileSync(join(dir, relDir, name), body);
+ };
+
+ it('reads the port out of the vite config in the process cwd', async () => {
+ dir = mkdtempSync(join(tmpdir(), 'vite-port-'));
+ writeViteConfig('client', 'vite.config.js', 'export default { server: { port: 5174 } };\n');
+
+ expect(await resolveViteConfigPortForProcess(dir, { usesVite: true, cwd: 'client' })).toBe(5174);
+ });
+
+ it('prefers vite.config.ts over vite.config.js', async () => {
+ dir = mkdtempSync(join(tmpdir(), 'vite-port-'));
+ writeViteConfig('client', 'vite.config.ts', 'export default { server: { port: 4001 } };\n');
+ writeViteConfig('client', 'vite.config.js', 'export default { server: { port: 4002 } };\n');
+
+ expect(await resolveViteConfigPortForProcess(dir, { usesVite: true, cwd: 'client' })).toBe(4001);
+ });
+
+ it('falls through to the next candidate when the first declares no port', async () => {
+ dir = mkdtempSync(join(tmpdir(), 'vite-port-'));
+ writeViteConfig('client', 'vite.config.ts', 'export default { plugins: [] };\n');
+ writeViteConfig('client', 'vite.config.js', 'export default { server: { port: 4002 } };\n');
+
+ expect(await resolveViteConfigPortForProcess(dir, { usesVite: true, cwd: 'client' })).toBe(4002);
+ });
+
+ it('returns null when the process is not a port-less vite process', async () => {
+ dir = mkdtempSync(join(tmpdir(), 'vite-port-'));
+ writeViteConfig('client', 'vite.config.ts', 'export default { server: { port: 4001 } };\n');
+
+ // Not a vite process, already has a port, or has no cwd to look in.
+ expect(await resolveViteConfigPortForProcess(dir, { usesVite: false, cwd: 'client' })).toBeNull();
+ expect(await resolveViteConfigPortForProcess(dir, { usesVite: true, port: 3000, cwd: 'client' })).toBeNull();
+ expect(await resolveViteConfigPortForProcess(dir, { usesVite: true })).toBeNull();
+ expect(await resolveViteConfigPortForProcess(dir, null)).toBeNull();
+ });
+
+ it('returns null when no vite config exists in the cwd', async () => {
+ dir = mkdtempSync(join(tmpdir(), 'vite-port-'));
+ mkdirSync(join(dir, 'client'), { recursive: true });
+
+ expect(await resolveViteConfigPortForProcess(dir, { usesVite: true, cwd: 'client' })).toBeNull();
+ });
+});
+
// streamingDetect.js reaches `pm2.js` → the `pm2` package, which does not
// resolve from the client workspace in CI (only via root hoisting locally). A
// client-side version of this test therefore passes on a dev machine and fails