1use std::path::{Path, PathBuf};
2
3use clap::Args;
4use emmylua_check::OutputDestination;
5use itertools::Itertools;
6use lux_lib::{config::Config, lua_version::LuaVersion, workspace::Workspace};
7
8use miette::{miette, Result};
9
10use crate::{
11 args::OutputFormat,
12 utils::{
13 self,
14 path::{classify_path, PathTarget},
15 },
16 workspace::sync_dependencies_if_locked,
17};
18
19#[derive(Args)]
20pub struct Check {
21 #[arg(long)]
23 path: Option<PathBuf>,
24 #[arg(short, long, value_delimiter = ',')]
28 ignore: Option<Vec<String>>,
29
30 #[arg(long, default_value = "text", value_enum, ignore_case = true)]
32 output_format: OutputFormat,
33
34 #[arg(long, default_value = "stdout")]
37 output: OutputDestination,
38
39 #[arg(long)]
41 warnings_as_errors: bool,
42}
43
44impl From<OutputFormat> for emmylua_check::OutputFormat {
45 fn from(value: OutputFormat) -> Self {
46 match value {
47 OutputFormat::Json => emmylua_check::OutputFormat::Json,
48 OutputFormat::Text => emmylua_check::OutputFormat::Text,
49 }
50 }
51}
52
53pub async fn check(args: Check, config: Config) -> Result<()> {
54 let target = match args.path.as_deref() {
55 None => PathTarget::Workspace(Box::new(Workspace::current_or_err()?)),
56 Some(path) => classify_path(path)?,
57 };
58
59 let (workspace_dirs, rc_files) = match target {
60 PathTarget::Workspace(workspace) => {
61 sync_dependencies_if_locked(&workspace, &config).await?;
62
63 let dirs = workspace
64 .members()
65 .iter()
66 .map(|project| project.root())
67 .flat_map(|project_root| {
68 vec![
69 project_root.join("src"),
70 project_root.join("lua"),
71 ]
78 })
79 .filter(|dir| dir.is_dir())
80 .collect_vec();
81
82 let luarc_path = workspace.luarc_path(&config);
83
84 let rc = if luarc_path.is_file() {
85 Some(vec![luarc_path])
86 } else {
87 None
88 };
89
90 (dirs, rc)
91 }
92 PathTarget::Directory(dir) => {
93 let rc = resolve_rc_files(&dir, &config)?;
94 (vec![dir], rc)
95 }
96 PathTarget::File(file) => {
97 let root = file
98 .parent()
99 .unwrap_or_else(|| Path::new("."))
100 .to_path_buf();
101
102 let rc = resolve_rc_files(&root, &config)?;
103 (vec![root], rc)
104 }
105 };
106
107 if workspace_dirs.is_empty() {
108 println!("Nothing to check!");
109 return Ok(());
110 }
111
112 if config.lua_version().is_some_and(|v| v == &LuaVersion::Luau) {
113 if args.ignore.is_some()
114 || args.output_format == OutputFormat::Json
115 || args.warnings_as_errors
116 {
117 tracing::warn!(
118 "--ignore, --output-format and --warnings-as-errors are not supported by luau-analyze"
119 );
120 }
121 utils::luau_analyze::run(
122 &config,
123 workspace_dirs
124 .iter()
125 .map(|dir| dir.to_string_lossy().to_string())
126 .collect_vec(),
127 Vec::new(),
128 )
129 .await
130 } else {
131 let emmylua_check_args = emmylua_check::CmdArgs {
132 config: rc_files,
133 workspace: workspace_dirs,
134 ignore: args.ignore,
135 output_format: args.output_format.into(),
136 output: args.output,
137 warnings_as_errors: args.warnings_as_errors,
138 verbose: config.verbose(),
139 severity: None,
140 };
141
142 emmylua_check::run_check(emmylua_check_args)
143 .await
144 .map_err(|err| miette!("{err}"))?;
145 Ok(())
146 }
147}
148
149fn resolve_rc_files(root: &Path, config: &Config) -> Result<Option<Vec<PathBuf>>> {
150 let rc_path = match Workspace::from(root)? {
151 Some(workspace) => workspace.luarc_path(config),
152 None => root.join(".luarc.json"),
153 };
154
155 if rc_path.is_file() {
156 Ok(Some(vec![rc_path]))
157 } else {
158 Ok(None)
159 }
160}