@aight-cool/aight-utils 0.1.0 → 0.1.1
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/package.json +1 -1
- package/src/version.ts +38 -8
package/package.json
CHANGED
package/src/version.ts
CHANGED
|
@@ -10,7 +10,6 @@ import { join, dirname } from "node:path";
|
|
|
10
10
|
import { fileURLToPath } from "node:url";
|
|
11
11
|
|
|
12
12
|
let cachedVersion: string | null = null;
|
|
13
|
-
let cachedLatest: { version: string; checkedAt: number } | null = null;
|
|
14
13
|
const CACHE_TTL_MS = 60 * 60 * 1000; // 1 hour
|
|
15
14
|
|
|
16
15
|
function getCurrentVersion(): string {
|
|
@@ -25,22 +24,47 @@ function getCurrentVersion(): string {
|
|
|
25
24
|
return cachedVersion!;
|
|
26
25
|
}
|
|
27
26
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
let cachedGatewayVersion: string | null = null;
|
|
28
|
+
|
|
29
|
+
function getGatewayVersion(): string {
|
|
30
|
+
if (cachedGatewayVersion) return cachedGatewayVersion;
|
|
31
|
+
try {
|
|
32
|
+
const { execSync } = require("node:child_process");
|
|
33
|
+
const out = execSync("openclaw --version", { timeout: 5000 }).toString().trim();
|
|
34
|
+
cachedGatewayVersion = out || "unknown";
|
|
35
|
+
} catch {
|
|
36
|
+
// Try to find openclaw's package.json via require.resolve
|
|
37
|
+
try {
|
|
38
|
+
const resolved = require.resolve("openclaw");
|
|
39
|
+
const pkgPath = join(dirname(resolved), "package.json");
|
|
40
|
+
const pkg = JSON.parse(readFileSync(pkgPath, "utf8"));
|
|
41
|
+
cachedGatewayVersion = pkg.version ?? "unknown";
|
|
42
|
+
} catch {
|
|
43
|
+
cachedGatewayVersion = "unknown";
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return cachedGatewayVersion!;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const npmCache = new Map<string, { version: string; checkedAt: number }>();
|
|
50
|
+
|
|
51
|
+
async function getLatestNpmVersion(pkg: string): Promise<string> {
|
|
52
|
+
const cached = npmCache.get(pkg);
|
|
53
|
+
if (cached && Date.now() - cached.checkedAt < CACHE_TTL_MS) {
|
|
54
|
+
return cached.version;
|
|
31
55
|
}
|
|
32
56
|
try {
|
|
33
|
-
const res = await fetch(
|
|
57
|
+
const res = await fetch(`https://registry.npmjs.org/${encodeURIComponent(pkg)}/latest`, {
|
|
34
58
|
headers: { accept: "application/json" },
|
|
35
59
|
signal: AbortSignal.timeout(5000),
|
|
36
60
|
});
|
|
37
61
|
if (!res.ok) return "unknown";
|
|
38
62
|
const data = (await res.json()) as { version?: string };
|
|
39
63
|
const version = data.version ?? "unknown";
|
|
40
|
-
|
|
64
|
+
npmCache.set(pkg, { version, checkedAt: Date.now() });
|
|
41
65
|
return version;
|
|
42
66
|
} catch {
|
|
43
|
-
return
|
|
67
|
+
return cached?.version ?? "unknown";
|
|
44
68
|
}
|
|
45
69
|
}
|
|
46
70
|
|
|
@@ -48,11 +72,17 @@ export function registerVersion(api: OpenClawPluginApi) {
|
|
|
48
72
|
api.registerGatewayMethod("aight.version", async ({ respond }: GatewayRequestHandlerOptions) => {
|
|
49
73
|
try {
|
|
50
74
|
const current = getCurrentVersion();
|
|
51
|
-
const latest = await
|
|
75
|
+
const latest = await getLatestNpmVersion("@aight-cool/aight-utils");
|
|
76
|
+
const gateway = getGatewayVersion();
|
|
77
|
+
const gatewayLatest = await getLatestNpmVersion("openclaw");
|
|
52
78
|
respond(true, {
|
|
53
79
|
current,
|
|
54
80
|
latest,
|
|
55
81
|
updateAvailable: latest !== "unknown" && current !== latest,
|
|
82
|
+
gatewayVersion: gateway,
|
|
83
|
+
gatewayLatest,
|
|
84
|
+
gatewayUpdateAvailable:
|
|
85
|
+
gatewayLatest !== "unknown" && gateway !== "unknown" && gateway !== gatewayLatest,
|
|
56
86
|
});
|
|
57
87
|
} catch (err) {
|
|
58
88
|
respond(false, {
|