UNPKG

ai-commit-report-generator-cli

Version:

An AI-powered CLI tool that generates weekly reports from your Git activity

185 lines (183 loc) • 8.25 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MenuService = void 0; const inquirer_1 = __importDefault(require("inquirer")); const dotenv_1 = __importDefault(require("dotenv")); const prompts_1 = require("../prompts"); const date_fns_1 = require("date-fns"); const summarize_commits_use_case_1 = require("../use-cases/summarize-commits.use-case"); const generate_report_use_case_1 = require("../use-cases/generate-report.use-case"); const commitFetcher_1 = require("../commitFetcher"); const json_store_factory_1 = require("../json-store.factory"); const utils_1 = require("../utils"); const renderers_1 = require("../renderers"); const APP_DESCRIPTION = ` šŸš€ Welcome to GitHub Weekly Report Generator! This CLI tool helps you generate insightful weekly reports from your GitHub commits. It uses AI to analyze your commits and create meaningful summaries and technical reports. `; class MenuService { start() { return __awaiter(this, void 0, void 0, function* () { while (true) { const option = yield this.showMainMenu(); yield this.handleOption(option); if (option === 'exit') { break; } yield this.waitForConfirmation(); } }); } showMainMenu() { return __awaiter(this, void 0, void 0, function* () { console.clear(); const { option } = yield inquirer_1.default.prompt([ { type: 'list', name: 'option', message: 'What would you like to do?', choices: [ { name: 'Run Repository Scan', value: 'scan' }, { name: 'Display Technical Summary', value: 'technical-summary' }, { name: 'Display Report', value: 'display-report' }, { name: 'Exit', value: 'exit' } ] } ]); return option; }); } waitForConfirmation() { return __awaiter(this, void 0, void 0, function* () { yield inquirer_1.default.prompt([ { type: 'input', name: 'continue', message: 'Press Enter to continue...', } ]); }); } handleOption(option) { return __awaiter(this, void 0, void 0, function* () { switch (option) { case 'scan': yield this.handleScan(); break; case 'technical-summary': yield this.handleTechnicalSummary(); break; case 'display-report': yield this.handleDisplayReport(); break; case 'exit': console.log('šŸ‘‹ Goodbye!'); break; } }); } ensureGoogleApiKey() { return __awaiter(this, void 0, void 0, function* () { dotenv_1.default.config(); if (!process.env.GOOGLE_API_KEY) { const { apiKey } = yield inquirer_1.default.prompt([ { type: 'input', name: 'apiKey', message: 'Please enter your Google API Key:', validate: (input) => { if (!input.trim()) { return 'API Key is required'; } return true; } } ]); // Write to .env file const fs = require('fs'); fs.appendFileSync('.env', `\nGOOGLE_API_KEY=${apiKey}`); process.env.GOOGLE_API_KEY = apiKey; } }); } handleScan() { return __awaiter(this, void 0, void 0, function* () { yield this.ensureGoogleApiKey(); const { repoPath } = yield inquirer_1.default.prompt([ { type: 'list', name: 'repoPath', message: 'Which repository would you like to scan?', choices: [ { name: 'Current Directory (.)', value: '.' }, { name: 'Specify Custom Path', value: 'custom' } ] } ]); let finalPath = "."; if (repoPath === "custom") { const { customPath } = yield inquirer_1.default.prompt([{ type: 'input', name: 'customPath', message: 'Enter the path to the repository:', validate: (input) => { if (!input.trim()) { return 'Path is required'; } return true; } }]); finalPath = customPath; } const options = yield (0, prompts_1.promptForScanFilteringOptions)(finalPath); console.log(`Fetching commits from ${(0, date_fns_1.format)(options.dateRange.startDate, 'yyyy-MM-dd')} to ${(0, date_fns_1.format)(options.dateRange.endDate, 'yyyy-MM-dd')}`); const commitsEntries = yield (0, commitFetcher_1.fetchCommitsWithStatistics)({ path: finalPath, filters: { dateRange: options.dateRange, username: options.username } }); console.log(`Found ${commitsEntries.length} commits in the specified date range`); const commitsWithSummaries = yield (0, summarize_commits_use_case_1.summarizeCommitsUseCase)(commitsEntries); yield (0, generate_report_use_case_1.generateReportUseCase)(commitsWithSummaries); }); } handleTechnicalSummary() { return __awaiter(this, void 0, void 0, function* () { const jsonFactory = json_store_factory_1.JsonStoreFactory.getInstance(); const cacheStore = yield jsonFactory.createOrGetStore((0, utils_1.slugify)('.')); console.dir(cacheStore.getAll('summaries'), { depth: 4 }); const hashes = cacheStore.getKeys("summaries").map(e => e.split(":")[1]); const commitsWithStatistics = yield (0, commitFetcher_1.fetchCommitsWithStatistics)({ filters: { hashes } }); const cachedSummaries = commitsWithStatistics.map(e => ({ commit: e.commit, statistics: e.statistics, summary: cacheStore.get(`summaries:${e.commit.hash}`) })); renderers_1.aiSummaryConsoleRenderer.renderSummary(cachedSummaries); }); } handleDisplayReport() { return __awaiter(this, void 0, void 0, function* () { const jsonFactory = json_store_factory_1.JsonStoreFactory.getInstance(); const cacheStore = yield jsonFactory.createOrGetStore((0, utils_1.slugify)('.')); const reports = cacheStore.getAll('report').map(e => e[1]); renderers_1.aiReportConsoleRenderer.renderReport(reports); }); } } exports.MenuService = MenuService;