@rigstate/cli 0.7.8 ā 0.7.10
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 +251 -206
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +251 -206
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/daemon/core.ts +1 -0
- package/src/daemon/factory.ts +4 -2
- package/src/daemon/guardian-monitor.ts +9 -1
- package/src/utils/config.ts +63 -1
package/package.json
CHANGED
package/src/daemon/core.ts
CHANGED
|
@@ -79,6 +79,7 @@ export class GuardianDaemon extends EventEmitter {
|
|
|
79
79
|
private printWelcome() {
|
|
80
80
|
console.log(chalk.bold.blue('\nš”ļø Guardian Daemon Starting...'));
|
|
81
81
|
console.log(chalk.dim(`Project: ${this.config.projectId}`));
|
|
82
|
+
console.log(chalk.dim(`API URL: ${this.config.apiUrl}`));
|
|
82
83
|
console.log(chalk.dim(`Watch Path: ${this.config.watchPath}`));
|
|
83
84
|
console.log(chalk.dim('ā'.repeat(50)));
|
|
84
85
|
}
|
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) {
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
import axios from 'axios';
|
|
6
|
+
import chalk from 'chalk';
|
|
6
7
|
import fs from 'fs/promises';
|
|
7
8
|
import path from 'path';
|
|
8
9
|
import { checkFile, type EffectiveRule, type Violation, type CheckResult } from '../utils/rule-engine.js';
|
|
@@ -54,10 +55,17 @@ export function createGuardianMonitor(
|
|
|
54
55
|
await saveCachedRules(projectId, rules);
|
|
55
56
|
return;
|
|
56
57
|
}
|
|
57
|
-
} catch (error) {
|
|
58
|
+
} catch (error: any) {
|
|
59
|
+
// Log error for debugging
|
|
60
|
+
console.error(chalk.red(` ā ļø Failed to fetch rules from API: ${error.message}`));
|
|
61
|
+
if (error.response) {
|
|
62
|
+
console.error(chalk.red(` Status: ${error.response.status} - ${JSON.stringify(error.response.data)}`));
|
|
63
|
+
}
|
|
64
|
+
|
|
58
65
|
// Try cache fallback
|
|
59
66
|
const cached = await loadCachedRules(projectId);
|
|
60
67
|
if (cached) {
|
|
68
|
+
console.log(chalk.yellow(' ā¹ļø Using cached rules as fallback'));
|
|
61
69
|
rules = cached.rules;
|
|
62
70
|
lastFetch = Date.now();
|
|
63
71
|
return;
|
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,62 @@ 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
|
+
// Dynamic import to avoid top-level await issues
|
|
83
|
+
const { default: axios } = await import('axios');
|
|
84
|
+
const { default: chalk } = await import('chalk');
|
|
85
|
+
|
|
86
|
+
console.log(chalk.gray(`š Discovering local Rigstate API URL... Configured: ${configuredUrl}`));
|
|
87
|
+
|
|
88
|
+
// If not localhost, trust the config
|
|
89
|
+
if (!configuredUrl.includes('localhost') && !configuredUrl.includes('127.0.0.1')) {
|
|
90
|
+
console.log(chalk.gray(` Configured URL is not localhost, trusting: ${configuredUrl}`));
|
|
91
|
+
return configuredUrl;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Try to find the real port
|
|
95
|
+
const ports = [3000, 3001, 3002, 3003, 3004, 3005, 3006, 3007, 3008, 3009, 3010];
|
|
96
|
+
|
|
97
|
+
for (const port of ports) {
|
|
98
|
+
const url = `http://localhost:${port}`;
|
|
99
|
+
try {
|
|
100
|
+
console.log(chalk.gray(` Attempting to connect to ${url}/api/v1/system/health...`));
|
|
101
|
+
// Try the health endpoint
|
|
102
|
+
await axios.get(`${url}/api/v1/system/health`, { timeout: 800 });
|
|
103
|
+
|
|
104
|
+
console.log(chalk.green(` ā
Found Rigstate API at ${url}`));
|
|
105
|
+
if (url !== configuredUrl) {
|
|
106
|
+
console.log(chalk.gray(` Updating stored API URL from ${configuredUrl} to ${url}`));
|
|
107
|
+
// Update config for next time
|
|
108
|
+
setApiUrl(url);
|
|
109
|
+
}
|
|
110
|
+
return url;
|
|
111
|
+
} catch (e: any) {
|
|
112
|
+
// Check if it's a 404 on the health endpoint - if so, it might still be our server
|
|
113
|
+
// But we prefer a positive match on health.
|
|
114
|
+
if (axios.isAxiosError(e) && e.response && e.response.status === 404) {
|
|
115
|
+
console.log(chalk.yellow(` ā ļø ${url} responded with 404, but might still be a Rigstate server. Skipping for now.`));
|
|
116
|
+
} else if (axios.isAxiosError(e) && e.code === 'ECONNABORTED') {
|
|
117
|
+
console.log(chalk.gray(` Connection to ${url} timed out.`));
|
|
118
|
+
} else if (axios.isAxiosError(e) && e.code === 'ECONNREFUSED') {
|
|
119
|
+
console.log(chalk.gray(` Connection to ${url} refused.`));
|
|
120
|
+
} else {
|
|
121
|
+
console.log(chalk.gray(` Failed to connect to ${url}: ${e.message}`));
|
|
122
|
+
}
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
console.log(chalk.yellow(` ā No local Rigstate API found on scanned ports. Falling back to configured URL: ${configuredUrl}`));
|
|
128
|
+
return configuredUrl;
|
|
129
|
+
}
|
|
130
|
+
|
|
69
131
|
/**
|
|
70
132
|
* Set the API URL
|
|
71
133
|
*/
|