Skip to main content

rusty_commit/commands/
commitlint.rs

1use anyhow::{Context, Result};
2use colored::Colorize;
3use dialoguer::{theme::ColorfulTheme, Confirm};
4use std::fs;
5use std::path::Path;
6
7use crate::cli::CommitLintCommand;
8use crate::git;
9
10const COMMITLINT_CONFIG: &str = r#"module.exports = {
11  extends: ['@commitlint/config-conventional'],
12  rules: {
13    'type-enum': [
14      2,
15      'always',
16      [
17        'feat',
18        'fix',
19        'docs',
20        'style',
21        'refactor',
22        'perf',
23        'test',
24        'build',
25        'ci',
26        'chore',
27        'revert'
28      ]
29    ]
30  }
31};
32"#;
33
34pub async fn execute(cmd: CommitLintCommand) -> Result<()> {
35    git::assert_git_repo()?;
36
37    let repo_root = git::get_repo_root()?;
38    let config_path = Path::new(&repo_root).join(".commitlintrc.js");
39
40    if config_path.exists() && !cmd.set {
41        let overwrite = Confirm::with_theme(&ColorfulTheme::default())
42            .with_prompt("Commitlint config already exists. Overwrite?")
43            .default(false)
44            .interact()?;
45
46        if !overwrite {
47            println!("{}", "Commitlint configuration unchanged".yellow());
48            return Ok(());
49        }
50    }
51
52    // Write the commitlint configuration
53    fs::write(&config_path, COMMITLINT_CONFIG)
54        .context("Failed to write commitlint configuration")?;
55
56    println!("{}", "✅ Commitlint configuration created!".green());
57    println!("Configuration written to: {}", config_path.display());
58
59    // Check if package.json exists
60    let package_json_path = Path::new(&repo_root).join("package.json");
61    if package_json_path.exists() {
62        println!("\n{}", "📦 Next steps:".bold());
63        println!("1. Install commitlint dependencies:");
64        println!("   npm install --save-dev @commitlint/cli @commitlint/config-conventional");
65        println!("2. Add husky hook for commit-msg:");
66        println!("   npx husky add .husky/commit-msg 'npx --no -- commitlint --edit \"$1\"'");
67    }
68
69    Ok(())
70}