import { Octokit } from '@octokit/rest'; import * as fs from 'fs/promises'; import * as path from 'path'; /** * GitHub publisher for Slidev decks * Pushes generated deck files to buzztrail-presentations repository */ export interface PublishDeckOptions { deckId: string; deckPath: string; // Local path to built deck githubToken: string; owner?: string; repo?: string; branch?: string; } export interface PublishDeckResult { success: boolean; deckUrl: string; commitSha?: string; error?: string; } const DEFAULT_OWNER = 'BuzzTrail'; const DEFAULT_REPO = 'buzztrail-presentations'; const DEFAULT_BRANCH = 'main'; /** * Publish a Slidev deck to GitHub Pages * * @param options - Publishing options * @returns Result with deck URL and commit SHA */ export async function publishDeckToGitHub( options: PublishDeckOptions ): Promise { const { deckId, deckPath, githubToken, owner = DEFAULT_OWNER, repo = DEFAULT_REPO, branch = DEFAULT_BRANCH } = options; try { const octokit = new Octokit({ auth: githubToken }); // Get the current commit SHA of the branch const { data: refData } = await octokit.git.getRef({ owner, repo, ref: `heads/${branch}` }); const currentCommitSha = refData.object.sha; // Get the tree SHA from the current commit const { data: commitData } = await octokit.git.getCommit({ owner, repo, commit_sha: currentCommitSha }); const baseTreeSha = commitData.tree.sha; // Read deck files and create tree entries const deckFiles = await readDeckFiles(deckPath); const treeEntries = deckFiles.map(file => ({ path: `decks/${deckId}/${file.relativePath}`, mode: '100644' as const, type: 'blob' as const, content: file.content })); // Create a new tree with the deck files const { data: newTree } = await octokit.git.createTree({ owner, repo, base_tree: baseTreeSha, tree: treeEntries }); // Create a new commit const { data: newCommit } = await octokit.git.createCommit({ owner, repo, message: `Add deck: ${deckId}\n\nGenerated by Slidev API`, tree: newTree.sha, parents: [currentCommitSha] }); // Update the branch reference await octokit.git.updateRef({ owner, repo, ref: `heads/${branch}`, sha: newCommit.sha }); // Construct the GitHub Pages URL const deckUrl = `https://${owner.toLowerCase()}.github.io/${repo}/${deckId}/`; return { success: true, deckUrl, commitSha: newCommit.sha }; } catch (error) { console.error('Failed to publish deck to GitHub:', error); return { success: false, deckUrl: '', error: error instanceof Error ? error.message : 'Unknown error' }; } } /** * Read all files from a deck directory */ async function readDeckFiles(deckPath: string): Promise> { const files: Array<{ relativePath: string; content: string }> = []; async function readDir(dir: string, relativePath = '') { const entries = await fs.readdir(dir, { withFileTypes: true }); for (const entry of entries) { const fullPath = path.join(dir, entry.name); const relPath = path.join(relativePath, entry.name); if (entry.isDirectory()) { await readDir(fullPath, relPath); } else { const content = await fs.readFile(fullPath, 'utf-8'); files.push({ relativePath: relPath, content }); } } } await readDir(deckPath); return files; } /** * Get the GitHub Pages URL for a deck * (deck may not be published yet, but this returns the expected URL) */ export function getGitHubPagesUrl( deckId: string, owner = DEFAULT_OWNER, repo = DEFAULT_REPO ): string { return `https://${owner.toLowerCase()}.github.io/${repo}/${deckId}/`; } /** * Check if a deck exists in the GitHub repository */ export async function deckExistsInGitHub( deckId: string, githubToken: string, owner = DEFAULT_OWNER, repo = DEFAULT_REPO, branch = DEFAULT_BRANCH ): Promise { try { const octokit = new Octokit({ auth: githubToken }); await octokit.repos.getContent({ owner, repo, path: `decks/${deckId}`, ref: branch }); return true; } catch (error: any) { if (error.status === 404) { return false; } throw error; } }