Skip to main content

source_map_tauri/frontend/
mod.rs

1pub mod hooks;
2pub mod swc;
3pub mod tauri_calls;
4pub mod tests;
5
6use std::path::Path;
7
8use anyhow::Result;
9
10use crate::{
11    config::ResolvedConfig,
12    discovery::RepoDiscovery,
13    model::{ArtifactDoc, WarningDoc},
14};
15
16pub fn extract(
17    config: &ResolvedConfig,
18    discovery: &RepoDiscovery,
19) -> Result<(Vec<ArtifactDoc>, Vec<WarningDoc>)> {
20    let mut artifacts = Vec::new();
21    let mut warnings = Vec::new();
22    let known_hooks =
23        swc::discover_hook_names(discovery).or_else(|_| hooks::discover_hook_names(discovery))?;
24
25    for path in &discovery.frontend_files {
26        let text = std::fs::read_to_string(path)?;
27        match swc::extract_file(config, path, &text, &known_hooks, false) {
28            Ok((file_artifacts, file_warnings)) => {
29                artifacts.extend(file_artifacts);
30                warnings.extend(file_warnings);
31            }
32            Err(_) => {
33                artifacts.extend(hooks::extract_components_and_hooks(
34                    config,
35                    path,
36                    &text,
37                    &known_hooks,
38                ));
39                let (call_artifacts, call_warnings) =
40                    tauri_calls::extract_calls(config, path, &text, false);
41                artifacts.extend(call_artifacts);
42                warnings.extend(call_warnings);
43            }
44        }
45    }
46
47    for path in &discovery.guest_js_files {
48        let text = std::fs::read_to_string(path)?;
49        match swc::extract_file(config, path, &text, &known_hooks, true) {
50            Ok((file_artifacts, file_warnings)) => {
51                artifacts.extend(file_artifacts);
52                warnings.extend(file_warnings);
53            }
54            Err(_) => {
55                let (call_artifacts, call_warnings) =
56                    tauri_calls::extract_calls(config, path, &text, true);
57                artifacts.extend(call_artifacts);
58                warnings.extend(call_warnings);
59            }
60        }
61    }
62
63    for path in &discovery.frontend_test_files {
64        let text = std::fs::read_to_string(path)?;
65        artifacts.extend(tests::extract_frontend_tests(config, path, &text));
66    }
67
68    Ok((artifacts, warnings))
69}
70
71pub fn language_for_path(path: &Path) -> Option<String> {
72    path.extension()
73        .and_then(|item| item.to_str())
74        .map(|item| item.to_owned())
75}