All files / rw-lazy-installer/src/services repository-manager.ts

100% Statements 85/85
100% Branches 30/30
100% Functions 8/8
100% Lines 85/85

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 1311x             1x 30x   30x 30x 30x         30x 3x 3x 3x 2x 2x 3x         30x 4x 4x     4x     4x 3x     2x   2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x         30x 5x 5x     5x 5x 1x 1x 1x 1x     3x     3x 5x   5x 5x 5x 5x 5x 5x 1x 1x 1x 1x 5x         30x 4x 4x 4x 4x 4x 1x 1x 4x         30x 5x 5x 5x   4x 3x 3x 4x 5x 1x 1x 5x         30x 3x 3x 3x 3x 1x 1x 3x 30x
import simpleGit, { SimpleGit, SimpleGitOptions } from 'simple-git';
import type { GitPullResult, GitStatus } from '../types/git';
 
/**
 * Service for managing Git repositories for mod installations
 * Provides a simple wrapper around Git operations needed for mod management
 */
export class RepositoryManager {
  private git: SimpleGit;
 
  constructor(options?: Partial<SimpleGitOptions>) {
    this.git = options ? simpleGit(options) : simpleGit();
  }
 
  /**
   * Clone a repository to a destination directory
   */
  async cloneRepository(url: string, destination: string): Promise<void> {
    try {
      await this.git.clone(url, destination);
    } catch (error) {
      throw new Error(`Failed to clone repository from ${url}: ${(error as Error).message}`);
    }
  }
 
  /**
   * Update a repository (fetch and pull)
   */
  async updateRepository(path: string): Promise<GitPullResult> {
    try {
      const repo = simpleGit(path);
 
      // Get current commit before update
      const beforeCommit = await repo.revparse(['HEAD']);
 
      // Fetch and pull
      await repo.fetch();
      await repo.pull();
 
      // Get commit after update
      const afterCommit = await repo.revparse(['HEAD']);
 
      return {
        success: true,
        hasChanges: beforeCommit !== afterCommit,
      };
    } catch (error) {
      return {
        success: false,
        hasChanges: false,
        error: (error as Error).message,
      };
    }
  }
 
  /**
   * Get repository status and current commit
   */
  async getRepositoryStatus(path: string): Promise<GitStatus> {
    try {
      const repo = simpleGit(path);
 
      // Check if it's a valid Git repository
      const isRepo = await repo.checkIsRepo();
      if (!isRepo) {
        return {
          isValidRepo: false,
        };
      }
 
      // Get current commit hash
      const currentCommit = await repo.revparse(['HEAD']);
 
      // Get remote URL
      const remotes = await repo.getRemotes(true);
      const remoteUrl = remotes.length > 0 && remotes[0]?.refs.fetch ? remotes[0].refs.fetch : undefined;
 
      return {
        isValidRepo: true,
        currentCommit,
        remoteUrl,
      };
    } catch (error) {
      return {
        isValidRepo: false,
      };
    }
  }
 
  /**
   * Get the remote URL of a repository
   */
  async getRemoteUrl(path: string): Promise<string | undefined> {
    try {
      const repo = simpleGit(path);
      const remotes = await repo.getRemotes(true);
      return remotes.length > 0 && remotes[0]?.refs.fetch ? remotes[0].refs.fetch : undefined;
    } catch (error) {
      return undefined;
    }
  }
 
  /**
   * Get recent commit messages (for changelog)
   */
  async getRecentCommits(path: string, limit: number = 5): Promise<Array<{ hash: string; message: string }>> {
    try {
      const repo = simpleGit(path);
      const log = await repo.log({ '--max-count': limit });
 
      return log.all.map(commit => ({
        hash: commit.hash.substring(0, 6),
        message: commit.message,
      }));
    } catch (error) {
      return [];
    }
  }
 
  /**
   * Get current commit hash
   */
  async getCurrentCommit(path: string): Promise<string | undefined> {
    try {
      const repo = simpleGit(path);
      return await repo.revparse(['HEAD']);
    } catch (error) {
      return undefined;
    }
  }
}