| 1 | import { readFileSync } from "node:fs"; |
| 2 | import { join } from "node:path"; |
| 3 | |
| 4 | export interface BuildIdentity { |
| 5 | version: string; |
| 6 | channel: string; |
| 7 | commit: string; |
| 8 | } |
| 9 | |
| 10 | // Native version resources are numeric and may discard both the tag prefix |
| 11 | // and prerelease suffix. The package manifest owns the RPC build identity. |
| 12 | export function loadBuildIdentity(packaged: boolean, resourcesPath: string, env: NodeJS.ProcessEnv): BuildIdentity { |
| 13 | if (!packaged) { |
| 14 | return { version: "dev", channel: env.REASONIX_CHANNEL || "dev", commit: env.REASONIX_COMMIT || "dev" }; |
| 15 | } |
| 16 | const value: unknown = JSON.parse(readFileSync(join(resourcesPath, "build.json"), "utf8")); |
| 17 | if (!value || typeof value !== "object" || Array.isArray(value)) throw new Error("Invalid packaged build identity"); |
| 18 | const info = value as Record<string, unknown>; |
| 19 | if (info.schemaVersion !== 1) throw new Error("Unsupported packaged build identity schema"); |
| 20 | for (const field of ["version", "channel", "commit"] as const) { |
| 21 | if (typeof info[field] !== "string" || info[field].trim() === "") throw new Error(`Missing packaged build identity: ${field}`); |
| 22 | } |
| 23 | const { version, channel, commit } = info as unknown as BuildIdentity; |
| 24 | if (!/^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-[0-9A-Za-z.-]+)?$/.test(version)) { |
| 25 | throw new Error("Invalid packaged build version"); |
| 26 | } |
| 27 | return { version, channel, commit }; |
| 28 | } |
| 29 |