108 lines
2.9 KiB
TypeScript
108 lines
2.9 KiB
TypeScript
import fs from 'fs/promises';
|
|||
|
|
import path from 'path';
|
||
|
|
import { config } from '../config';
|
||
|
|
import type { Instance } from '../types';
|
||
|
|
import logger from './logger';
|
||
|
|
|
||
|
|
const instancesDir = config.instanceConfigDir;
|
||
|
|
|
||
|
|
// 确保目录存在
|
||
|
|
export async function ensureDir(dir: string) {
|
||
|
|
try {
|
||
|
|
await fs.mkdir(dir, { recursive: true });
|
||
|
|
} catch (error) {
|
||
|
|
logger.error(`创建目录失败: ${dir}`, error);
|
||
|
|
throw error;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 初始化存储
|
||
|
|
export async function initStorage() {
|
||
|
|
await ensureDir(config.dataDir);
|
||
|
|
await ensureDir(instancesDir);
|
||
|
|
await ensureDir(config.logDir);
|
||
|
|
}
|
||
|
|
|
||
|
|
// 读取实例配置
|
||
|
|
export async function readInstance(instanceId: string): Promise<Instance | null> {
|
||
|
|
try {
|
||
|
|
const filePath = path.join(instancesDir, `${instanceId}.json`);
|
||
|
|
const data = await fs.readFile(filePath, 'utf-8');
|
||
|
|
const instance = JSON.parse(data);
|
||
|
|
// 转换日期
|
||
|
|
instance.createdAt = new Date(instance.createdAt);
|
||
|
|
if (instance.lastStarted) {
|
||
|
|
instance.lastStarted = new Date(instance.lastStarted);
|
||
|
|
}
|
||
|
|
return instance;
|
||
|
|
} catch (error: any) {
|
||
|
|
if (error.code === 'ENOENT') {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
logger.error(`读取实例配置失败: ${instanceId}`, error);
|
||
|
|
throw error;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 写入实例配置
|
||
|
|
export async function writeInstance(instance: Instance): Promise<void> {
|
||
|
|
try {
|
||
|
|
await ensureDir(instancesDir);
|
||
|
|
const filePath = path.join(instancesDir, `${instance.id}.json`);
|
||
|
|
await fs.writeFile(filePath, JSON.stringify(instance, null, 2));
|
||
|
|
} catch (error) {
|
||
|
|
logger.error(`写入实例配置失败: ${instance.id}`, error);
|
||
|
|
throw error;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 删除实例配置
|
||
|
|
export async function deleteInstance(instanceId: string): Promise<void> {
|
||
|
|
try {
|
||
|
|
const filePath = path.join(instancesDir, `${instanceId}.json`);
|
||
|
|
await fs.unlink(filePath);
|
||
|
|
} catch (error: any) {
|
||
|
|
if (error.code !== 'ENOENT') {
|
||
|
|
logger.error(`删除实例配置失败: ${instanceId}`, error);
|
||
|
|
throw error;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 列出所有实例
|
||
|
|
export async function listInstances(): Promise<Instance[]> {
|
||
|
|
try {
|
||
|
|
await ensureDir(instancesDir);
|
||
|
|
const files = await fs.readdir(instancesDir);
|
||
|
|
const instanceFiles = files.filter(f => f.endsWith('.json'));
|
||
|
|
|
||
|
|
const instances: Instance[] = [];
|
||
|
|
for (const file of instanceFiles) {
|
||
|
|
try {
|
||
|
|
const instanceId = path.basename(file, '.json');
|
||
|
|
const instance = await readInstance(instanceId);
|
||
|
|
if (instance) {
|
||
|
|
instances.push(instance);
|
||
|
|
}
|
||
|
|
} catch (error) {
|
||
|
|
logger.error(`读取实例文件失败: ${file}`, error);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return instances;
|
||
|
|
} catch (error) {
|
||
|
|
logger.error('列出实例失败', error);
|
||
|
|
throw error;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 检查实例是否存在
|
||
|
|
export async function instanceExists(instanceId: string): Promise<boolean> {
|
||
|
|
const filePath = path.join(instancesDir, `${instanceId}.json`);
|
||
|
|
try {
|
||
|
|
await fs.access(filePath);
|
||
|
|
return true;
|
||
|
|
} catch {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|