Open History to see the last commit
1import {
2 type DeviceHostSummary,
3 type DevicePlatformAvailability,
4 type EnvironmentId,
5 SshDeviceHostConfig,
6} from "@t3tools/contracts";
7import * as Schema from "effect/Schema";
8
9export interface DeviceHostCheckTarget {
10 environmentId: EnvironmentId;
11 label: string;
12 connected: boolean;
13}
14export type DeviceHostCheck =
15 | { status: "pending" }
16 | { status: "local" }
17 | { status: "connected"; platforms: ReadonlyArray<DevicePlatformAvailability> }
18 | { status: "failed"; error: string };
19
20const decodeDeviceHostDraft = Schema.decodeUnknownOption(SshDeviceHostConfig);
21
22export function parseDeviceHostDraft(host: SshDeviceHostConfig) {
23 const { identityFile, ...rest } = host;
24 return decodeDeviceHostDraft({
25 ...rest,
26 ...(identityFile?.trim() ? { identityFile: identityFile.trim() } : {}),
27 });
28}
29
30export function deviceHostConnectionKey(host: SshDeviceHostConfig) {
31 return JSON.stringify([host.target.trim(), host.port, host.identityFile?.trim() || undefined]);
32}
33
34/** Each environment settles independently so one failure cannot hide the other results. */
35export async function checkDeviceHostConnections(
36 targets: ReadonlyArray<DeviceHostCheckTarget>,
37 host: SshDeviceHostConfig,
38 probe: (environmentId: EnvironmentId, host: SshDeviceHostConfig) => Promise<DeviceHostSummary>,
39 report: (environmentId: EnvironmentId, result: DeviceHostCheck) => void,
40) {
41 await Promise.all(
42 targets.map(async (target) => {
43 report(target.environmentId, { status: "pending" });
44 try {
45 if (!target.connected) throw new Error("Environment disconnected");
46 const result = await probe(target.environmentId, host);
47 report(
48 target.environmentId,
49 result.kind === "local"
50 ? { status: "local" }
51 : { status: "connected", platforms: result.platforms },
52 );
53 } catch (error) {
54 report(target.environmentId, {
55 status: "failed",
56 error: error instanceof Error ? error.message : String(error),
57 });
58 }
59 }),
60 );
61}