import { env } from "~/env.js"; import type { Script, ScriptCard, GitHubFile } from "~/types/script.js"; export class GitHubService { private baseUrl: string; private repoUrl: string; private branch: string; private jsonFolder: string; constructor() { this.repoUrl = env.REPO_URL || ""; this.branch = env.REPO_BRANCH; this.jsonFolder = env.JSON_FOLDER; // Extract owner and repo from the URL const urlMatch = this.repoUrl.match(/github\.com\/([^\/]+)\/([^\/]+)/); if (!urlMatch) { throw new Error("Invalid GitHub repository URL"); } const [, owner, repo] = urlMatch; this.baseUrl = `https://api.github.com/repos/${owner}/${repo}`; } private async fetchFromGitHub(endpoint: string): Promise { const response = await fetch(`${this.baseUrl}${endpoint}`, { headers: { 'Accept': 'application/vnd.github.v3+json', 'User-Agent': 'PVEScripts-Local/1.0', }, }); if (!response.ok) { throw new Error(`GitHub API error: ${response.status} ${response.statusText}`); } return response.json() as Promise; } async getJsonFiles(): Promise { try { const files = await this.fetchFromGitHub( `/contents/${this.jsonFolder}?ref=${this.branch}` ); // Filter for JSON files only return files.filter(file => file.name.endsWith('.json')); } catch (error) { console.error('Error fetching JSON files from GitHub:', error); throw new Error('Failed to fetch script files from repository'); } } async getScriptContent(filePath: string): Promise