diff --git a/README.md b/README.md index b0de682..ad492d7 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ A lightweight, Promise-based Git client for Node.js that executes the git binary - Spawn mode for streaming operations - Built-in support for common Git operations - Minimal dependencies +- Full TypeScript support with type definitions ## Requirements @@ -104,6 +105,16 @@ const customGit = new git.Git({ gitDir: '/path/to/repo/.git' }); const status = await customGit.status(); ``` +## TypeScript Support + +The library includes TypeScript definitions for all methods and options. When using TypeScript, you'll get full type checking and autocompletion for: + +- Git instance configuration options +- Command execution options +- All git commands and their parameters +- Spawn mode process types +- Event handlers and callbacks + ## API Reference ### Main Function diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..863f1c5 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,115 @@ +declare module 'git-client' { + export interface GitOptions { + command?: string; + gitDir?: string; + workTree?: string; + indexFile?: string; + } + + export interface ExecOptions { + /** Set custom git directory */ + $gitDir?: string; + /** Set custom working tree */ + $workTree?: string; + /** Set custom index file */ + $indexFile?: string; + /** Enable spawn mode */ + $spawn?: boolean; + /** Enable shell mode */ + $shell?: boolean; + /** Return null instead of throwing on error */ + $nullOnError?: boolean; + /** Callback for stdout in spawn mode */ + $onStdout?: (line: string) => void; + /** Callback for stderr in spawn mode */ + $onStderr?: (line: string) => void; + /** Enable passthrough mode */ + $passthrough?: boolean; + /** Wait for process to complete */ + $wait?: boolean; + /** Preserve environment variables */ + $preserveEnv?: boolean; + /** Custom working directory */ + $cwd?: string; + /** Custom environment variables */ + $env?: Record; + /** Additional options */ + $options?: Record; + /** Any git command options */ + [key: string]: any; + } + + export interface SpawnedProcess extends NodeJS.EventEmitter { + stdin: NodeJS.WritableStream; + stdout: NodeJS.ReadableStream; + stderr: NodeJS.ReadableStream; + captureOutput(input?: string | null): Promise; + captureOutputTrimmed(input?: string | null): Promise; + } + + export interface TreeChild { + mode: string; + type: string; + hash: string; + name: string; + } + + export class Git { + constructor(options?: GitOptions); + + command: string; + gitDir: string | null; + workTree: string | null; + indexFile: string | null; + version: string | null; + + static getGitDirFromEnvironment(): Promise; + static getWorkTreeFromEnvironment(): Promise; + + getGitDir(): Promise; + getWorkTree(): Promise; + getIndexPath(): Promise; + getVersion(): Promise; + satisfiesVersion(range: string): Promise; + requireVersion(range: string): Promise; + + readConfigSet(configPath: string): Promise>; + writeConfigSet(configPath: string, set: Set): Promise; + addToConfigSet(configPath: string, ...items: string[]): Promise>; + removeFromConfigSet(configPath: string, ...items: string[]): Promise>; + + isHash(hash: string): boolean; + getTreeHash(ref: string, options?: { verify?: boolean }): Promise; + hashObjectInternally(content: string | Buffer, options?: { type?: string; write?: boolean }): Promise; + mktreeBatch(children: TreeChild[]): Promise; + cleanup(): void; + + exec(command: string, ...args: Array): Promise; + + // Git commands as methods + add(options?: ExecOptions, ...files: string[]): Promise; + commit(options?: ExecOptions, message?: string): Promise; + push(options?: ExecOptions): Promise; + pull(options?: ExecOptions): Promise; + checkout(options?: ExecOptions, ref?: string): Promise; + branch(options?: ExecOptions): Promise; + merge(options?: ExecOptions, ref?: string): Promise; + log(options?: ExecOptions): Promise; + status(options?: ExecOptions): Promise; + fetch(options?: ExecOptions): Promise; + remote(options?: ExecOptions): Promise; + reset(options?: ExecOptions): Promise; + revert(options?: ExecOptions): Promise; + tag(options?: ExecOptions): Promise; + init(options?: ExecOptions): Promise; + clone(options?: ExecOptions): Promise; + diff(options?: ExecOptions): Promise; + show(options?: ExecOptions): Promise; + stash(options?: ExecOptions): Promise; + revParse(options?: ExecOptions, ref?: string): Promise; + [key: string]: any; + } + + const git: Git & ((command: string, ...args: Array) => Promise); + export default git; +} diff --git a/package.json b/package.json index 014057e..e45d0b8 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "1.8.3", "description": "Promise-based git client that mostly just executes the git binary", "main": "index.js", + "types": "index.d.ts", "scripts": { "test": "ava --no-worker-threads" }, @@ -16,7 +17,8 @@ }, "keywords": [ "git", - "promise" + "promise", + "typescript" ], "repository": "JarvusInnovations/git-client", "homepage": "https://github.com/JarvusInnovations/git-client",