109 lines
3.5 KiB
TypeScript
109 lines
3.5 KiB
TypeScript
import * as plugins from './smartgit.plugins.js';
|
|
import { GitRepo } from './smartgit.classes.gitrepo.js';
|
|
|
|
type TNodeFs = typeof import('node:fs');
|
|
|
|
interface IEnvDeps {
|
|
fs?: TNodeFs;
|
|
http?: plugins.HttpClient;
|
|
}
|
|
|
|
/**
|
|
* 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: undefined,
|
|
http: undefined,
|
|
};
|
|
|
|
/**
|
|
* initializes the Smartgit instance with required environment dependencies
|
|
* Must be called before using any repository methods
|
|
*/
|
|
public async init(): Promise<void> {
|
|
try {
|
|
if (this.smartenvInstance.isNode) {
|
|
this.envDeps.fs = await this.smartenvInstance.getSafeNodeModule<TNodeFs>('fs');
|
|
this.envDeps.http = await this.smartenvInstance.getSafeNodeModule<plugins.HttpClient>(
|
|
'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.');
|
|
}
|
|
}
|
|
|
|
public get fs(): TNodeFs {
|
|
const fs = this.envDeps.fs;
|
|
if (!fs) {
|
|
throw new Error('Smartgit must be initialized before use. Call init() first.');
|
|
}
|
|
return fs;
|
|
}
|
|
|
|
public get http(): plugins.HttpClient {
|
|
const http = this.envDeps.http;
|
|
if (!http) {
|
|
throw new Error('Smartgit must be initialized before use. Call init() first.');
|
|
}
|
|
return http;
|
|
}
|
|
|
|
/**
|
|
* 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<GitRepo> the created repository instance
|
|
*/
|
|
public async createRepoByClone(fromUrlArg: string, toDirArg: string): Promise<GitRepo> {
|
|
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<GitRepo> the created repository instance
|
|
*/
|
|
public async createRepoByInit(dirArg: string): Promise<GitRepo> {
|
|
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<GitRepo> the opened repository instance
|
|
*/
|
|
public async createRepoByOpen(dirArg: string): Promise<GitRepo> {
|
|
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)}`);
|
|
}
|
|
}
|
|
}
|