import { writeFile, mkdir } from 'fs/promises'; import { join } from 'path'; import { env } from '../../env.js'; import type { Script, ScriptCard, GitHubFile } from '../../types/script'; export class GitHubJsonService { private baseUrl: string | null = null; private repoUrl: string | null = null; private branch: string | null = null; private jsonFolder: string | null = null; private localJsonDirectory: string | null = null; private scriptCache: Map = new Map(); constructor() { // Initialize lazily to avoid accessing env vars during module load } private initializeConfig() { if (this.repoUrl === null) { this.repoUrl = env.REPO_URL ?? ""; this.branch = env.REPO_BRANCH; this.jsonFolder = env.JSON_FOLDER; this.localJsonDirectory = join(process.cwd(), 'scripts', 'json'); // Only validate GitHub URL if it's provided if (this.repoUrl) { // Extract owner and repo from the URL const urlMatch = /github\.com\/([^\/]+)\/([^\/]+)/.exec(this.repoUrl); if (!urlMatch) { throw new Error(`Invalid GitHub repository URL: ${this.repoUrl}`); } const [, owner, repo] = urlMatch; this.baseUrl = `https://api.github.com/repos/${owner}/${repo}`; } else { // Set a dummy base URL if no REPO_URL is provided this.baseUrl = ""; } } } private async fetchFromGitHub(endpoint: string): Promise { this.initializeConfig(); 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; } private async downloadJsonFile(filePath: string): Promise