@rigstate/cli 0.7.7 → 0.7.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +25 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +25 -3
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/daemon/factory.ts +4 -2
- package/src/utils/config.ts +46 -1
package/package.json
CHANGED
package/src/daemon/factory.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { GuardianDaemon } from './core.js';
|
|
2
|
-
import { getApiKey, getApiUrl, getProjectId } from '../utils/config.js';
|
|
2
|
+
import { getApiKey, getApiUrl, discoverApiUrl, getProjectId } from '../utils/config.js';
|
|
3
3
|
import { loadManifest } from '../utils/manifest.js';
|
|
4
4
|
import { DaemonConfig } from './types.js';
|
|
5
5
|
|
|
@@ -12,7 +12,9 @@ export async function createDaemon(options: {
|
|
|
12
12
|
noBridge?: boolean;
|
|
13
13
|
verbose?: boolean;
|
|
14
14
|
}): Promise<GuardianDaemon> {
|
|
15
|
-
|
|
15
|
+
// Force discovery on startup to find the correct local port if applicable
|
|
16
|
+
const apiUrl = await discoverApiUrl();
|
|
17
|
+
|
|
16
18
|
let projectId = options.project;
|
|
17
19
|
|
|
18
20
|
if (!projectId) {
|
package/src/utils/config.ts
CHANGED
|
@@ -48,8 +48,14 @@ export function setProjectId(projectId: string): void {
|
|
|
48
48
|
config.set('projectId', projectId);
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
+
// Basic check if a port is open - simplistic implementation for now
|
|
52
|
+
// In a real scenario, we might want to actually ping /api/health
|
|
53
|
+
// But for getApiUrl which is synchronous, we cannot easily do async checks.
|
|
54
|
+
// So we will stick to the config/env value, BUT we will expose a separate
|
|
55
|
+
// async function `discoverApiUrl` that the Daemon calls on startup.
|
|
56
|
+
|
|
51
57
|
/**
|
|
52
|
-
* Get the API URL
|
|
58
|
+
* Get the API URL (Synchronous)
|
|
53
59
|
* Priority: Environment variable > Stored config > Production default
|
|
54
60
|
*/
|
|
55
61
|
export function getApiUrl(): string {
|
|
@@ -66,6 +72,45 @@ export function getApiUrl(): string {
|
|
|
66
72
|
return 'https://app.rigstate.com';
|
|
67
73
|
}
|
|
68
74
|
|
|
75
|
+
/**
|
|
76
|
+
* Discovers the active local API URL (Async)
|
|
77
|
+
* Scans ports 3000-3010 to find a responding Rigstate server.
|
|
78
|
+
*/
|
|
79
|
+
export async function discoverApiUrl(): Promise<string> {
|
|
80
|
+
const configuredUrl = getApiUrl();
|
|
81
|
+
|
|
82
|
+
// If not localhost, trust the config
|
|
83
|
+
if (!configuredUrl.includes('localhost') && !configuredUrl.includes('127.0.0.1')) {
|
|
84
|
+
return configuredUrl;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// If localhost, try to find the real port
|
|
88
|
+
const ports = [3000, 3001, 3002, 3003, 3004, 3005, 3006, 3007, 3008, 3009, 3010];
|
|
89
|
+
|
|
90
|
+
// Dynamic import to avoid top-level await issues in some contexts
|
|
91
|
+
const { default: axios } = await import('axios');
|
|
92
|
+
|
|
93
|
+
for (const port of ports) {
|
|
94
|
+
try {
|
|
95
|
+
const url = `http://localhost:${port}`;
|
|
96
|
+
// Try a lightweight endpoint
|
|
97
|
+
await axios.get(`${url}/api/v1/system/health`, { timeout: 500 });
|
|
98
|
+
// If success, we found it!
|
|
99
|
+
if (url !== configuredUrl) {
|
|
100
|
+
// Update config for next time
|
|
101
|
+
setApiUrl(url);
|
|
102
|
+
}
|
|
103
|
+
return url;
|
|
104
|
+
} catch (e) {
|
|
105
|
+
// Check next port
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Fallback to configured if scan fails (maybe it's starting up)
|
|
111
|
+
return configuredUrl;
|
|
112
|
+
}
|
|
113
|
+
|
|
69
114
|
/**
|
|
70
115
|
* Set the API URL
|
|
71
116
|
*/
|