Skip to main content

lux_cli/
check.rs

1use clap::{Args, ValueEnum};
2use emmylua_check::OutputDestination;
3use eyre::{eyre, Result};
4use itertools::Itertools;
5use lux_lib::{config::Config, progress::MultiProgress, project::Project};
6
7use crate::utils::project::{sync_dependencies_if_locked, sync_test_dependencies_if_locked};
8
9#[derive(Args)]
10pub struct Check {
11    /// Comma-separated list of ignore patterns.
12    /// Patterns must follow glob syntax.
13    /// Lux will automatically add top-level ignored project files.
14    #[arg(short, long, value_delimiter = ',')]
15    ignore: Option<Vec<String>>,
16
17    /// The output format.
18    #[arg(long, default_value = "text", value_enum, ignore_case = true)]
19    output_format: OutputFormat,
20
21    /// Output destination.{n}
22    /// (stdout or a file path, only used when the output format is json).
23    #[arg(long, default_value = "stdout")]
24    output: OutputDestination,
25
26    /// Treat warnings as errors.
27    #[arg(long)]
28    warnings_as_errors: bool,
29}
30
31#[derive(Debug, Clone, PartialEq, ValueEnum)]
32enum OutputFormat {
33    Json,
34    Text,
35}
36
37impl From<OutputFormat> for emmylua_check::OutputFormat {
38    fn from(value: OutputFormat) -> Self {
39        match value {
40            OutputFormat::Json => emmylua_check::OutputFormat::Json,
41            OutputFormat::Text => emmylua_check::OutputFormat::Text,
42        }
43    }
44}
45
46pub async fn check(args: Check, config: Config) -> Result<()> {
47    let project = Project::current_or_err()?;
48
49    let progress = MultiProgress::new_arc(&config);
50    sync_dependencies_if_locked(&project, progress.clone(), &config).await?;
51    sync_test_dependencies_if_locked(&project, progress, &config).await?;
52
53    let project_root = project.root();
54    let workspace = vec![
55        project_root.join("src"),
56        project_root.join("lua"),
57        // For now, we don't include tests
58        // because they require LLS_Addons definitions for busted
59
60        // project_root.join("test"),
61        // project_root.join("tests"),
62        // project_root.join("spec"),
63    ]
64    .into_iter()
65    .filter(|dir| dir.is_dir())
66    .collect_vec();
67
68    if workspace.is_empty() {
69        println!("Nothing to check!");
70        return Ok(());
71    }
72
73    let luarc_path = project.luarc_path();
74    let rc_files = if luarc_path.is_file() {
75        Some(vec![luarc_path])
76    } else {
77        None
78    };
79    let emmylua_check_args = emmylua_check::CmdArgs {
80        config: rc_files,
81        workspace,
82        ignore: args.ignore,
83        output_format: args.output_format.into(),
84        output: args.output,
85        warnings_as_errors: args.warnings_as_errors,
86        verbose: config.verbose(),
87    };
88
89    emmylua_check::run_check(emmylua_check_args)
90        .await
91        .map_err(|err| eyre!(err.to_string()))?;
92    Ok(())
93}