Skip to main content

source_map_tauri/frontend/
mod.rs

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