import * as plugins from './smartgit.plugins.js'; import { GitRepo } from './smartgit.classes.gitrepo.js'; interface IEnvDeps { fs: typeof import('fs') | null; http: any | null; // isomorphic-git http interface } /** * class Smartgit provides a high-level interface for git operations * Must be initialized before use by calling init() */ export class Smartgit { public smartenvInstance = new plugins.smartenv.Smartenv(); public envDeps: IEnvDeps = { fs: null, http: null, }; /** * initializes the Smartgit instance with required environment dependencies * Must be called before using any repository methods */ public async init(): Promise { try { if (this.smartenvInstance.isNode) { this.envDeps.fs = await this.smartenvInstance.getSafeNodeModule('fs'); this.envDeps.http = await this.smartenvInstance.getSafeNodeModule( 'isomorphic-git/http/node' ); } else { throw new Error('currently only node.js is supported.'); } } catch (error) { throw new Error(`Failed to initialize Smartgit: ${error instanceof Error ? error.message : String(error)}`); } } private ensureInitialized(): void { if (!this.envDeps.fs || !this.envDeps.http) { throw new Error('Smartgit must be initialized before use. Call init() first.'); } } /** * creates a new GitRepo instance by cloning from a remote URL * @param fromUrlArg the URL to clone from * @param toDirArg the directory to clone into * @returns Promise the created repository instance */ public async createRepoByClone(fromUrlArg: string, toDirArg: string): Promise { this.ensureInitialized(); try { const repo = await GitRepo.fromCloningIntoDir(this, fromUrlArg, toDirArg); return repo; } catch (error) { throw new Error(`Failed to clone repository: ${error instanceof Error ? error.message : String(error)}`); } } /** * creates a new GitRepo instance by initializing a new repository in a directory * @param dirArg the directory to initialize the repository in * @returns Promise the created repository instance */ public async createRepoByInit(dirArg: string): Promise { this.ensureInitialized(); try { const repo = await GitRepo.fromCreatingRepoInDir(this, dirArg); return repo; } catch (error) { throw new Error(`Failed to initialize repository: ${error instanceof Error ? error.message : String(error)}`); } } /** * creates a new GitRepo instance by opening an existing repository in a directory * @param dirArg the directory containing the existing repository * @returns Promise the opened repository instance */ public async createRepoByOpen(dirArg: string): Promise { this.ensureInitialized(); try { const repo = await GitRepo.fromOpeningRepoDir(this, dirArg); return repo; } catch (error) { throw new Error(`Failed to open repository: ${error instanceof Error ? error.message : String(error)}`); } } }