This document explains how Git commands are implemented in the Git Mastery learning platform and how to add new commands.
Commands in Git Mastery are implemented using a class-based architecture with the following components:
src/commands/base/Command.ts)src/commands/base/CommandRegistry.ts)src/commands/base/CommandParser.ts)All commands implement this interface:
export interface Command {
// Metadata
name: string;
description: string;
usage: string;
examples: string[];
// Tab completion properties
includeInTabCompletion: boolean;
supportsFileCompletion: boolean;
// Method to execute the command
execute(args: CommandArgs, context: CommandContext): string[];
// Optional validation method
validate?(args: CommandArgs): { isValid: boolean; errorMessage?: string };
}
Create a new TypeScript file in the appropriate category folder:
src/commands/git/src/commands/filesystem/src/commands/helpers/Implement the Command interface:
import type { Command, CommandArgs, CommandContext } from "../base/Command";
export class MyNewCommand implements Command {
name = "command-name";
description = "Description of what the command does";
usage = "command-name [options]";
examples = ["command-name example"];
includeInTabCompletion = true;
supportsFileCompletion = false;
execute(args: CommandArgs, context: CommandContext): string[] {
// Command implementation logic
// Return string array with command output lines
return ["Command output"];
}
// Optional validation
validate(args: CommandArgs): { isValid: boolean; errorMessage?: string } {
return { isValid: true };
}
}
src/commands/index.ts:import { MyNewCommand } from "./category/MyNewCommand";
// ...
registry.register(new MyNewCommand());
Commands have access to these components through the CommandContext:
Use these to implement command behavior, for example:
// Inside your execute method:
const { fileSystem, gitRepository, currentDirectory } = context;
// Check Git state
if (!gitRepository.isInitialized()) {
return ["Not a git repository. Run 'git init' first."];
}
// File operations
fileSystem.writeFile("/path/to/file", "content");
// Directory operations
const contents = fileSystem.getDirectoryContents(currentDirectory);
The CommandArgs object provides structured access to arguments:
--option or -o)Example parsing:
// For command: git commit -m "My message" --verbose
{
args: ["-m", "My message", "--verbose"],
flags: {
"m": "My message",
"verbose": true
},
positionalArgs: []
}
When adding a new command, test it thoroughly in the playground mode to ensure it:
Note that the Git implementation in Git Mastery is a simulation, not a full Git implementation. Some advanced features and edge cases may behave differently than real Git.