UNPKG

commit-vibes

Version:

A CLI tool to add vibes to your Git commit messages

135 lines (114 loc) • 3.79 kB
/** * Main commit flow handler for commit-vibes CLI. * Handles staging, prompting, vibe selection, Spotify, and running the commit. * @param {string[]} args - CLI arguments (commit message) */ import { intro, outro } from "@clack/prompts"; import chalk from "chalk"; import { promptCommitMessage, promptForFileSelection, promptForMoodSelection, } from "../prompts.js"; import { stageFiles, commitChanges, getUnstagedFiles } from "../git-utils.js"; import { getCurrentTrack } from "../spotify-auth.js"; import { showIntro, printStagedFiles, printSpotifyTrack, printRecentTracks, handleError, checkCancellationState, } from "../utils.js"; export async function handleCommit(args) { try { showIntro(); checkCancellationState(); // Get commit message (from args or prompt) let message = args[0]; if (!message) { message = await promptCommitMessage(); checkCancellationState(); } if (!message) { console.log(chalk.red("āŒ No commit message provided. Exiting.")); process.exit(1); } // Check for unstaged files const unstagedFiles = getUnstagedFiles(); if (unstagedFiles.length > 0) { const selectedFiles = await promptForFileSelection(); checkCancellationState(); if (selectedFiles.length > 0) { stageFiles(selectedFiles); checkCancellationState(); } } // Select vibe const vibe = await promptForMoodSelection(); checkCancellationState(); if (!vibe) { console.log(chalk.red("āŒ No vibe selected. Exiting.")); process.exit(1); } // Add vibe to message message = `${message} ${vibe}`; // Check Spotify for current track const spotifyData = await getCurrentTrack(); checkCancellationState(); if (spotifyData && !spotifyData.error) { if (spotifyData.current) { printSpotifyTrack(spotifyData.current); const { confirm } = await import("@clack/prompts"); const includeSong = await confirm({ message: "Add this song to your commit message?", }); checkCancellationState(); if (includeSong === undefined) { console.log(chalk.red("āŒ Commit canceled.")); process.exit(1); } if (includeSong) { message += `\n\nšŸŽµ Now playing: "${spotifyData.current.name} - ${spotifyData.current.artist}"`; } } else if (spotifyData.recent && spotifyData.recent.length > 0) { printRecentTracks(spotifyData.recent); const { confirm } = await import("@clack/prompts"); const includeSong = await confirm({ message: "Add most recent song to your commit message?", }); checkCancellationState(); if (includeSong === undefined) { console.log(chalk.red("āŒ Commit canceled.")); process.exit(1); } if (includeSong) { const mostRecent = spotifyData.recent[0]; message += `\n\nšŸŽµ Last played: "${mostRecent.name} - ${mostRecent.artist}"`; } } } // Show final commit message console.log(chalk.cyan("\nšŸ“ Final commit message:")); console.log(chalk.white(message)); console.log(); // Confirm commit const { confirm } = await import("@clack/prompts"); const shouldCommit = await confirm({ message: "Create this commit?", }); checkCancellationState(); if (shouldCommit === undefined) { console.log(chalk.red("āŒ Commit canceled.")); process.exit(1); } if (shouldCommit) { await commitChanges(message); outro(chalk.green("šŸŽ‰ Commit created successfully!")); } else { console.log(chalk.yellow("āŒ Commit canceled.")); } } catch (error) { handleError(error); } }