feat: machine and instance storage
This commit is contained in:
@@ -2,3 +2,5 @@ export * from "./config.ts";
|
|||||||
export * from "./ipc/client.ts";
|
export * from "./ipc/client.ts";
|
||||||
export * from "./ipc/protocol.ts";
|
export * from "./ipc/protocol.ts";
|
||||||
export * from "./ipc/server.ts";
|
export * from "./ipc/server.ts";
|
||||||
|
export * from "./storage.ts";
|
||||||
|
export * from "./types.ts";
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import type { InstanceStatus } from "../types.ts";
|
||||||
|
|
||||||
export interface SpawnRequest {
|
export interface SpawnRequest {
|
||||||
type: "spawn";
|
type: "spawn";
|
||||||
cwd: string;
|
cwd: string;
|
||||||
@@ -31,7 +33,7 @@ export type OrchestratorRequest = RequestMap[keyof RequestMap];
|
|||||||
|
|
||||||
export interface InstanceSummary {
|
export interface InstanceSummary {
|
||||||
id: string;
|
id: string;
|
||||||
status: "starting" | "online" | "stopping" | "stopped" | "error";
|
status: InstanceStatus;
|
||||||
cwd: string;
|
cwd: string;
|
||||||
label?: string;
|
label?: string;
|
||||||
sessionId?: string;
|
sessionId?: string;
|
||||||
|
|||||||
@@ -0,0 +1,62 @@
|
|||||||
|
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
||||||
|
import { getInstancesPath, getMachinePath, getOrchestratorDir } from "./config.ts";
|
||||||
|
import type { InstanceRecord, MachineRecord } from "./types.ts";
|
||||||
|
|
||||||
|
function ensureOrchestratorDir(): void {
|
||||||
|
const orchestratorDir = getOrchestratorDir();
|
||||||
|
if (!existsSync(orchestratorDir)) {
|
||||||
|
mkdirSync(orchestratorDir, { recursive: true });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function loadMachine(): MachineRecord | undefined {
|
||||||
|
const machinePath = getMachinePath();
|
||||||
|
if (!existsSync(machinePath)) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = readFileSync(machinePath, "utf-8");
|
||||||
|
return JSON.parse(data) as MachineRecord;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function saveMachine(machine: MachineRecord): void {
|
||||||
|
ensureOrchestratorDir();
|
||||||
|
writeFileSync(getMachinePath(), JSON.stringify(machine, null, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
export function loadInstances(): InstanceRecord[] {
|
||||||
|
const instancesPath = getInstancesPath();
|
||||||
|
if (!existsSync(instancesPath)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = readFileSync(instancesPath, "utf-8");
|
||||||
|
return JSON.parse(data) as InstanceRecord[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function saveInstances(instances: InstanceRecord[]): void {
|
||||||
|
ensureOrchestratorDir();
|
||||||
|
writeFileSync(getInstancesPath(), JSON.stringify(instances, null, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getInstance(instanceId: string): InstanceRecord | undefined {
|
||||||
|
return loadInstances().find((instance) => instance.id === instanceId);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function upsertInstance(instance: InstanceRecord): void {
|
||||||
|
const instances = loadInstances();
|
||||||
|
const index = instances.findIndex((existing) => existing.id === instance.id);
|
||||||
|
if (index === -1) {
|
||||||
|
instances.push(instance);
|
||||||
|
saveInstances(instances);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
instances[index] = instance;
|
||||||
|
saveInstances(instances);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function removeInstance(instanceId: string): void {
|
||||||
|
const instances = loadInstances().filter((instance) => instance.id !== instanceId);
|
||||||
|
saveInstances(instances);
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
export type InstanceStatus = "starting" | "online" | "stopping" | "stopped" | "error";
|
||||||
|
|
||||||
|
export interface MachineRecord {
|
||||||
|
id: string;
|
||||||
|
createdAt: string;
|
||||||
|
lastSeenAt?: string;
|
||||||
|
label?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface InstanceRecord {
|
||||||
|
id: string;
|
||||||
|
status: InstanceStatus;
|
||||||
|
cwd: string;
|
||||||
|
createdAt: string;
|
||||||
|
lastSeenAt?: string;
|
||||||
|
label?: string;
|
||||||
|
sessionId?: string;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user