Refactor git subcommands to accept a git instance

This commit is contained in:
Alex Gleason committed 2025-09-14 17:15:07 -05:00
1 parent 772a2f424b
commit 1ab895bd59
23 files changed
+581 -413

No files matched your search

+5 -4
View File
@@ -7,6 +7,7 @@
*/
import { ShellTool } from './ShellTool';
import { Git } from '../git';
import type { JSRuntimeFS } from '../JSRuntime';
/**
@@ -14,7 +15,7 @@ import type { JSRuntimeFS } from '../JSRuntime';
*/
export async function exampleShellUsage(fs: JSRuntimeFS, projectRoot: string) {
// Create a shell tool instance
const shell = new ShellTool(fs, projectRoot);
const shell = new ShellTool(fs, projectRoot, new Git(fs));
console.log('Available commands:', shell.getAvailableCommands());
console.log('Current directory:', shell.getCurrentWorkingDirectory());
@@ -111,7 +112,7 @@ export async function exampleShellUsage(fs: JSRuntimeFS, projectRoot: string) {
* Example React hook for using ShellTool
*/
export function useShell(fs: JSRuntimeFS, cwd: string) {
const shell = new ShellTool(fs, cwd);
const shell = new ShellTool(fs, cwd, new Git(fs));
const executeCommand = async (command: string): Promise<string> => {
try {
@@ -137,7 +138,7 @@ export class AIShellIntegration {
private shell: ShellTool;
constructor(fs: JSRuntimeFS, projectRoot: string) {
this.shell = new ShellTool(fs, projectRoot);
this.shell = new ShellTool(fs, projectRoot, new Git(fs));
}
/**
@@ -177,7 +178,7 @@ export class SimpleCLI {
private history: string[] = [];
constructor(fs: JSRuntimeFS, projectRoot: string) {
this.shell = new ShellTool(fs, projectRoot);
this.shell = new ShellTool(fs, projectRoot, new Git(fs));
}
async executeCommand(command: string): Promise<string> {
+4 -1
View File
@@ -1,5 +1,6 @@
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { ShellTool } from './ShellTool';
import { Git } from '../git';
import type { JSRuntimeFS } from '../JSRuntime';
// Mock filesystem
@@ -19,12 +20,14 @@ const createMockFS = (): JSRuntimeFS => ({
describe('ShellTool', () => {
let mockFS: JSRuntimeFS;
let mockGit: Git;
let shellTool: ShellTool;
const testCwd = '/test/dir';
beforeEach(() => {
mockFS = createMockFS();
shellTool = new ShellTool(mockFS, testCwd);
mockGit = new Git(mockFS);
shellTool = new ShellTool(mockFS, testCwd, mockGit);
});
it('should have correct tool properties', () => {
+5 -2
View File
@@ -2,6 +2,7 @@ import { z } from "zod";
import type { Tool } from "./Tool";
import type { JSRuntimeFS } from "../JSRuntime";
import type { ShellCommand } from "../commands/ShellCommand";
import { Git } from "../git";
import {
CatCommand,
CdCommand,
@@ -42,6 +43,7 @@ interface ShellToolParams {
export class ShellTool implements Tool<ShellToolParams> {
private fs: JSRuntimeFS;
private cwd: string;
private git: Git;
private commands: Map<string, ShellCommand>;
readonly description = "Execute shell commands like cat, ls, cd, pwd, rm, cp, mv, echo, head, tail, grep, find, wc, touch, mkdir, sort, uniq, cut, tr, diff, which, whoami, date, env, clear, git, curl. Supports compound commands with &&, ||, ;, and | operators";
@@ -52,9 +54,10 @@ export class ShellTool implements Tool<ShellToolParams> {
),
});
constructor(fs: JSRuntimeFS, cwd: string) {
constructor(fs: JSRuntimeFS, cwd: string, git: Git) {
this.fs = fs;
this.cwd = cwd;
this.git = git;
this.commands = new Map();
// Register available commands
@@ -69,7 +72,7 @@ export class ShellTool implements Tool<ShellToolParams> {
this.registerCommand(new EchoCommand());
this.registerCommand(new EnvCommand());
this.registerCommand(new FindCommand(fs));
this.registerCommand(new GitCommand(fs));
this.registerCommand(new GitCommand({ git: this.git, fs, cwd: this.cwd }));
this.registerCommand(new GrepCommand(fs));
this.registerCommand(new HeadCommand(fs));
this.registerCommand(new LsCommand(fs));