gitset
Version:
Enhanced git init with user configuration management
138 lines • 4.5 kB
JavaScript
import { Command } from 'commander';
import { readFileSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
import { WorkflowOrchestrator } from '../core/workflow-orchestrator.js';
import { ErrorHandler } from '../utils/error-handler.js';
import { GitStartError } from '../types/cli.js';
import { APP_CONFIG, CLI_CONFIG, MESSAGES } from '../config/constants.js';
// Get package.json version
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const packageJsonPath = join(__dirname, '../../package.json');
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8'));
const version = `v${packageJson.version}`;
/**
* Command Line Interface for gitset
* Focused on CLI concerns only - delegates business logic to WorkflowOrchestrator
*/
export class GitStartCLI {
program;
orchestrator;
constructor(orchestrator) {
this.program = new Command();
this.orchestrator = orchestrator || WorkflowOrchestrator.createDefault();
this.setupCommands();
}
/**
* Configure CLI commands and options
*/
setupCommands() {
this.program
.name(APP_CONFIG.name)
.description(APP_CONFIG.description)
.version(version, '-v, --version', 'output the current version')
.argument('[directory]', 'Directory to initialize git repository', CLI_CONFIG.defaultDirectory)
.option('-g, --global', 'Use global git configuration without prompting')
.action(async (directory, options) => {
await this.executeWorkflow(directory, options);
});
this.program
.command('help')
.description('Display help information')
.action(() => {
this.program.help();
});
}
/**
* Parse command line arguments
*/
async parseArguments(args) {
this.program.parse(args);
const options = this.program.opts();
const directory = this.program.args[0] || CLI_CONFIG.defaultDirectory;
return { ...options, directory };
}
/**
* Execute the main workflow with proper error handling
*/
async executeWorkflow(directory, options) {
try {
await this.orchestrator.execute(directory, options);
}
catch (error) {
this.handleError(error);
}
}
/**
* Handle errors with appropriate user feedback and exit codes
*/
handleError(error) {
if (error instanceof GitStartError) {
ErrorHandler.handle(error);
}
else {
console.error(MESSAGES.error.unexpectedError, error);
}
// Only exit in production, not in tests
if (this.shouldExitProcess()) {
process.exit(CLI_CONFIG.exitCodes.error);
}
else {
throw error;
}
}
/**
* Determine if we should exit the process
*/
shouldExitProcess() {
return process.env.NODE_ENV !== 'test';
}
/**
* Main entry point for the CLI application
*/
static async main() {
try {
const cli = new GitStartCLI();
await cli.program.parseAsync(process.argv);
}
catch (error) {
console.error(MESSAGES.error.fatalError, error);
if (process.env.NODE_ENV !== 'test') {
process.exit(CLI_CONFIG.exitCodes.error);
}
}
}
/**
* Get the commander program instance (for testing)
*/
getProgram() {
return this.program;
}
/**
* Get the workflow orchestrator (for testing)
*/
getOrchestrator() {
return this.orchestrator;
}
/**
* Legacy methods for backward compatibility with tests
* These delegate to the appropriate components
*/
async checkGitInstallation() {
return await this.orchestrator.checkGitInstallation();
}
async getGlobalGitConfig() {
return await this.orchestrator.getGlobalGitConfig();
}
async validateDirectory(directory) {
return await this.orchestrator.validateDirectory(directory);
}
async initializeGitRepository(directory, userInfo) {
return await this.orchestrator.initializeGitRepository(directory, userInfo);
}
}
// Auto-execute when run directly (simplified for better compatibility)
GitStartCLI.main();
//# sourceMappingURL=index.js.map