Skip to main content
Glama
ProjectRepository.ts2.09 kB
import { promises as fs } from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; import { dirname as pathDirname } from 'path'; export function getDirname(): string { if (typeof __dirname !== 'undefined') { // CommonJS 环境 return __dirname; } // ES 模块环境 return pathDirname(fileURLToPath(import.meta.url)); } export class ProjectRepository { private dataPath: string; private projects: any[] = []; constructor() { // 获取当前文件的目录路径 // const __filename = fileURLToPath(import.meta.url); // const dirname = path.dirname(__filename); // 优先尝试从 dist/resources 读取(生产环境) const distPath = path.join(getDirname(), 'resources', 'projects.json'); const srcPath = path.join(getDirname(), 'src', 'resources', 'projects.json'); // 检查文件是否存在,优先使用 dist 路径 try { const fsSync = require('fs'); if (fsSync.existsSync(distPath)) { this.dataPath = distPath; } else { this.dataPath = srcPath; } console.log('⚠️:[ this.dataPath ]🎈:', this.dataPath) } catch (error) { // 如果无法检查文件存在性,默认使用 dist 路径 this.dataPath = distPath; } } async initialize(): Promise<void> { try { const data = await fs.readFile(this.dataPath, 'utf-8'); this.projects = JSON.parse(data); } catch (error) { console.error('读取项目数据失败:', error); this.projects = []; } } async saveProjects(): Promise<void> { await fs.writeFile(this.dataPath, JSON.stringify(this.projects, null, 2)); } getAllProjects(): any[] { return this.projects; } getProjectCount(): number { return this.projects.length; } findProjectByName(projectName: string): any { return this.projects.find((p: any) => p.name === projectName || p.name.includes(projectName)); } addProject(project: any): void { this.projects.push(project); } } // module.exports = { ProjectRepository };

Implementation Reference

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/sweetwisdom/mcp-demo'

If you have feedback or need assistance with the MCP directory API, please join our Discord server