UNPKG

@elsikora/commitizen-plugin-commitlint-ai

Version:
89 lines (86 loc) 3.33 kB
import { existsSync } from 'node:fs'; import { join } from 'node:path'; import load from '@commitlint/load'; import chalk from 'chalk'; import { config } from 'dotenv'; import inquirer from 'inquirer'; import Process from './Process.js'; import './services/llm/index.js'; import { getLLMConfig, setLLMConfig } from './services/llm/config.js'; // Load environment variables from .env file try { config(); } catch { // Silently continue if .env file is not found or cannot be loaded } // Check what commit mode to use based on config, environment variable, and fallback file const getCommitMode = () => { try { // First check environment variable (highest priority) // Next check for manual flag file if (existsSync(join("./.elsikora", "manual"))) { return "manual"; } // Finally check config file const config = getLLMConfig(); if (config?.mode && // Validation is now done in config.ts to avoid duplicate messages (config.mode === "auto" || config.mode === "manual")) { return config.mode; } // Default to auto if not specified return "auto"; } catch { // In case of any errors, default to auto return "auto"; } }; /** * Entry point for commitizen * @param inquirerIns instance passed by commitizen, unused * @param commit callback to execute with complete commit message * @return {void} */ async function prompter(inquirerIns, commit) { // eslint-disable-next-line @elsikora-typescript/typedef await load().then(async ({ prompt = {}, rules }) => { // Use process (AI mode) unless manual mode is enabled const commitMode = getCommitMode(); if (commitMode === "manual") { const { useExisting } = await inquirer.prompt([ { // eslint-disable-next-line @elsikora-typescript/naming-convention default: true, message: `Use manual configuration?`, name: "useExisting", type: "confirm", }, ]); if (useExisting) { console.log(chalk.blue("Using manual commit mode...")); // Import manualProcess dynamically to avoid loading AI deps when not needed // eslint-disable-next-line @elsikora-typescript/typedef await import('./ManualProcess.js').then(async ({ default: manualProcess }) => { await manualProcess(rules, prompt, inquirerIns).then(commit); }); } else { console.log(chalk.blue("Using AI-powered commit mode...")); // eslint-disable-next-line @elsikora/typescript/no-non-null-assertion const oldConfig = getLLMConfig(); setLLMConfig({ ...oldConfig, mode: "auto", }); await Process(rules, prompt, inquirerIns).then(commit); } } else { console.log(chalk.blue("Using AI-powered commit mode...")); await Process(rules, prompt, inquirerIns).then(commit); } }); } export { getLLMConfig, prompter, setLLMConfig }; //# sourceMappingURL=index.js.map