-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.ts
More file actions
44 lines (39 loc) · 1.45 KB
/
json.ts
File metadata and controls
44 lines (39 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { readFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
function readPackageVersion(): string {
const here = dirname(fileURLToPath(import.meta.url));
const pkgPath = join(here, "..", "..", "package.json");
try {
const j = JSON.parse(readFileSync(pkgPath, "utf8")) as { version?: string };
return j.version ?? "0.0.0";
} catch {
return "0.0.0";
}
}
/** FastMCP types require major.minor.patch; strip prerelease suffixes from package.json. */
export function readMcpServerVersion(): `${number}.${number}.${number}` {
const raw = readPackageVersion().trim();
const m = /^(\d+)\.(\d+)\.(\d+)/.exec(raw);
if (m?.[1] !== undefined && m[2] !== undefined && m[3] !== undefined) {
return `${m[1]}.${m[2]}.${m[3]}` as `${number}.${number}.${number}`;
}
return "0.0.0";
}
export function jsonRespond(body: Record<string, unknown>): string {
return JSON.stringify(body);
}
/** Spread into an object literal only when `cond` is true; otherwise `{}`. */
export function spreadWhen<T extends Record<string, unknown>>(
cond: boolean,
fields: T,
): T | Record<string, never> {
return cond ? fields : {};
}
/** Spread `{ [key]: value }` only when `value` is not `undefined`. */
export function spreadDefined<K extends string, V>(
key: K,
value: V | undefined,
): Record<K, V> | Record<string, never> {
return spreadWhen(value !== undefined, { [key]: value } as Record<K, V>);
}