File size: 4,174 Bytes
35743bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import {
  getVersionManagerStatus,
  getVersionManagerTool,
  upsertVersionManagerTool,
  updateVersionManagerTool,
  setToolStatus,
  updateToolVersion,
} from "@/lib/db/versionManager";
import { getLatestRelease, clearCache as clearReleaseCache } from "./releaseChecker.ts";
import { installVersion, getCurrentBinaryPath, rollbackVersion } from "./binaryManager.ts";
import { startProcess, stopProcess, restartProcess, isProcessRunning } from "./processManager.ts";
import { checkHealth, startMonitoring, stopMonitoring, isMonitoring } from "./healthMonitor.ts";

export { getVersionManagerStatus, getVersionManagerTool } from "@/lib/db/versionManager";

export type { HealthResult } from "./healthMonitor.ts";

export async function installTool(
  tool: string,
  version?: string
): Promise<{
  installedVersion: string;
  binaryPath: string;
}> {
  const targetVersion = version || (await getLatestRelease()).version;
  const binaryPath = await installVersion(targetVersion);

  await upsertVersionManagerTool({
    tool,
    installedVersion: targetVersion,
    binaryPath,
    status: "installed",
  });

  return { installedVersion: targetVersion, binaryPath };
}

export async function startTool(tool: string): Promise<{
  pid: number;
  port: number;
  health?: Awaited<ReturnType<typeof checkHealth>>;
}> {
  const info = await getVersionManagerTool(tool);
  const binaryPath = info?.binaryPath || (await getCurrentBinaryPath());

  if (!binaryPath) {
    throw new Error(
      `No binary found for ${tool}. Run installTool('${tool}') or set binaryPath in version manager.`
    );
  }

  const { pid, port } = await startProcess(binaryPath, info?.port || undefined);

  const url = `http://127.0.0.1:${port}`;
  startMonitoring(tool, url);

  const health = await checkHealth(url);

  return { pid, port, health };
}

export async function stopTool(tool: string): Promise<void> {
  const info = await getVersionManagerTool(tool);

  stopMonitoring(tool);

  if (info?.pid) {
    await stopProcess(info.pid);
  }

  await setToolStatus(tool, "stopped");
}

export async function restartTool(tool: string): Promise<{
  pid: number;
  port: number;
}> {
  const info = await getVersionManagerTool(tool);
  const binaryPath = info?.binaryPath || (await getCurrentBinaryPath());

  if (!binaryPath) {
    throw new Error(`No binary found for ${tool}`);
  }

  const { pid, port } = await restartProcess(
    binaryPath,
    info?.port || undefined,
    undefined,
    info?.pid || undefined
  );

  const url = `http://127.0.0.1:${port}`;
  if (isMonitoring(tool)) {
    stopMonitoring(tool);
  }
  startMonitoring(tool, url);

  return { pid, port };
}

export async function checkForUpdates(tool: string): Promise<{
  current: string | null;
  latest: string;
  updateAvailable: boolean;
}> {
  clearReleaseCache();
  const latest = (await getLatestRelease()).version;
  const info = await getVersionManagerTool(tool);
  const current = info?.installedVersion || null;

  if (!current) {
    return { current: null, latest, updateAvailable: true };
  }

  return {
    current,
    latest,
    updateAvailable: current !== latest,
  };
}

export async function pinVersion(tool: string, version: string): Promise<void> {
  await updateVersionManagerTool(tool, { pinnedVersion: version });
}

export async function unpinVersion(tool: string): Promise<void> {
  await updateVersionManagerTool(tool, { pinnedVersion: null });
}

export async function getToolHealth(
  tool: string
): Promise<Awaited<ReturnType<typeof checkHealth>> | null> {
  const info = await getVersionManagerTool(tool);
  if (!info?.port || info.status !== "running") return null;

  const url = `http://127.0.0.1:${info.port}`;
  return checkHealth(url);
}

export async function rollbackTool(tool: string): Promise<string | null> {
  const previousVersion = await rollbackVersion();
  if (!previousVersion) return null;

  await updateVersionManagerTool(tool, {
    installedVersion: previousVersion,
    status: "installed",
  });

  const info = await getVersionManagerTool(tool);
  if (info?.pid && isProcessRunning(info.pid)) {
    await restartTool(tool);
  }

  return previousVersion;
}