Files
cc-cloud/backend/src/utils/storage.ts
T
hughhuangandClaude Sonnet 4.5 ed626e74dc feat: 初始化 Claude Code 管理平台项目
实现了基于 Web 的 Claude Code 实例管理功能:

## 功能特性
- 🔐 用户认证:JWT 登录验证
- 🚀 实例管理:创建、启动、停止、删除 Claude Code 实例
- 📊 实时监控:CPU、内存、运行时间统计
- 📝 日志管理:Winston 日志系统
- 🔌 PM2 集成:稳定的进程管理
- 🌐 Web 界面:Vue 3 + Element Plus

## 技术栈
- 后端:Node.js + Express + TypeScript + PM2
- 前端:Vue 3 + TypeScript + Vite + Element Plus
- 认证:JWT
- 存储:文件系统(JSON)

## 项目结构
- Monorepo 结构,前后端分离
- 完整的类型定义
- 模块化设计
- 详细的开发文档

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-02 22:57:33 +08:00

109 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;
}
}