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

fix: properly parse template literals with nested quotes in pm2Home extraction

Adam Eivy2d74f5ed9b2a
diff --git a/.changelog/v0.13.x.md b/.changelog/v0.13.x.md
index 3a66c93b83..c2005bf340 100644
--- a/.changelog/v0.13.x.md
+++ b/.changelog/v0.13.x.md
@@ -244,6 +244,7 @@ This release expands the genome dashboard with comprehensive cancer predispositi
### Custom PM2_HOME Auto-Detection
- **Automatic pm2Home extraction during app detection/ingestion** — the streaming detector now parses ecosystem.config.js for `const PM2_HOME = ...` declarations and extracts the custom path
-- **Template literal support** — handles common patterns like `` `${require("os").homedir()}/.pm2-grace` `` by resolving the homedir at detection time
+- **Template literal support** — handles common patterns like `` `${require("os").homedir()}/.pm2-grace` `` by resolving the homedir at detection time using `[^`]+` regex pattern that properly captures nested quotes
- **Refresh config updates pm2Home** — the `/api/apps/:id/refresh-config` endpoint now auto-populates pm2Home when re-parsing the ecosystem config, so existing apps can pick up custom PM2_HOME settings without re-ingestion
- **Detection uses correct PM2_HOME for status checks** — when detecting a new app with a custom PM2_HOME, the PM2 status check runs against the correct daemon instance instead of the default
+- **Fixed template literal parsing** — the regex now correctly handles nested quotes in template literals (e.g., `require("os")`) instead of stopping at the first quote character
diff --git a/server/services/streamingDetect.js b/server/services/streamingDetect.js
index 7894ebc6e0..eaeed687b6 100644
--- a/server/services/streamingDetect.js
+++ b/server/services/streamingDetect.js
@@ -18,22 +18,32 @@ export function parseEcosystemConfig(content) {
let pm2Home = null;
// Extract PM2_HOME constant if defined (e.g., const PM2_HOME = `${require("os").homedir()}/.pm2-grace`)
- const pm2HomeMatch = content.match(/(?:const|let|var)\s+PM2_HOME\s*=\s*[`'"](.*?)[`'"]/);
- if (pm2HomeMatch) {
- // Handle template literal with homedir()
- let homePath = pm2HomeMatch[1];
- homePath = homePath.replace(/\$\{require\(['"]os['"]\)\.homedir\(\)\}/, homedir());
+ // First try template literals (backticks) which can contain nested quotes
+ const templateMatch = content.match(/(?:const|let|var)\s+PM2_HOME\s*=\s*`([^`]+)`/);
+ if (templateMatch) {
+ let homePath = templateMatch[1];
+ // Replace common template expressions
+ homePath = homePath.replace(/\$\{require\(['"]os['"]\)\.homedir\(\)\}/g, homedir());
+ homePath = homePath.replace(/\$\{require\(['"]os['"]\)\.userInfo\(\)\.username\}/g, process.env.USER || 'user');
+ homePath = homePath.replace(/\$\{process\.env\.HOME\}/g, homedir());
pm2Home = homePath;
} else {
- // Check for PM2_HOME in env blocks
- const envPm2HomeMatch = content.match(/PM2_HOME\s*:\s*PM2_HOME/);
- if (envPm2HomeMatch) {
- // PM2_HOME is used but defined as a variable - try to extract the original definition
- const varMatch = content.match(/PM2_HOME\s*=\s*`([^`]+)`/);
- if (varMatch) {
- let homePath = varMatch[1];
- homePath = homePath.replace(/\$\{require\(['"]os['"]\)\.homedir\(\)\}/, homedir());
- pm2Home = homePath;
+ // Try regular string literals
+ const stringMatch = content.match(/(?:const|let|var)\s+PM2_HOME\s*=\s*['"]([^'"]+)['"]/);
+ if (stringMatch) {
+ pm2Home = stringMatch[1];
+ } else {
+ // Check for PM2_HOME in env blocks
+ const envPm2HomeMatch = content.match(/PM2_HOME\s*:\s*PM2_HOME/);
+ if (envPm2HomeMatch) {
+ // PM2_HOME is used but defined as a variable - try template literal first
+ const varTemplateMatch = content.match(/PM2_HOME\s*=\s*`([^`]+)`/);
+ if (varTemplateMatch) {
+ let homePath = varTemplateMatch[1];
+ homePath = homePath.replace(/\$\{require\(['"]os['"]\)\.homedir\(\)\}/g, homedir());
+ homePath = homePath.replace(/\$\{require\(['"]os['"]\)\.userInfo\(\)\.username\}/g, process.env.USER || 'user');
+ pm2Home = homePath;
+ }
}
}
}