1use std::path::{Path, PathBuf};
2
3use clap::Args;
4use emmylua_check::OutputDestination;
5use itertools::Itertools;
6use lux_lib::{config::Config, lua, 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
113 .lua_version()
114 .is_some_and(|v| v == &lua::Version::Luau)
115 {
116 if args.ignore.is_some()
117 || args.output_format == OutputFormat::Json
118 || args.warnings_as_errors
119 {
120 tracing::warn!(
121 "--ignore, --output-format and --warnings-as-errors are not supported by luau-analyze"
122 );
123 }
124 utils::luau_analyze::run(
125 &config,
126 workspace_dirs
127 .iter()
128 .map(|dir| dir.to_string_lossy().to_string())
129 .collect_vec(),
130 Vec::new(),
131 )
132 .await
133 } else {
134 let emmylua_check_args = emmylua_check::CmdArgs {
135 config: rc_files,
136 workspace: workspace_dirs,
137 ignore: args.ignore,
138 output_format: args.output_format.into(),
139 output: args.output,
140 warnings_as_errors: args.warnings_as_errors,
141 verbose: config.verbose(),
142 severity: None,
143 };
144
145 emmylua_check::run_check(emmylua_check_args)
146 .await
147 .map_err(|err| miette!("{err}"))?;
148 Ok(())
149 }
150}
151
152fn resolve_rc_files(root: &Path, config: &Config) -> Result<Option<Vec<PathBuf>>> {
153 let rc_path = match Workspace::from(root)? {
154 Some(workspace) => workspace.luarc_path(config),
155 None => root.join(".luarc.json"),
156 };
157
158 if rc_path.is_file() {
159 Ok(Some(vec![rc_path]))
160 } else {
161 Ok(None)
162 }
163}