|
1 | | -use std::path::Path; |
| 1 | +use std::{collections::HashSet, path::Path}; |
2 | 2 |
|
3 | 3 | use serde_json::Value; |
4 | 4 |
|
5 | | -use crate::{error::Result, LegolasError}; |
| 5 | +use crate::{error::Result, workspace::exists}; |
6 | 6 |
|
7 | | -pub fn detect_frameworks(_project_root: &Path, _manifest: &Value) -> Result<Vec<String>> { |
8 | | - Err(LegolasError::NotImplemented("detect_frameworks")) |
| 7 | +struct FrameworkMarker { |
| 8 | + name: &'static str, |
| 9 | + packages: &'static [&'static str], |
| 10 | + files: &'static [&'static str], |
9 | 11 | } |
10 | 12 |
|
11 | | -pub fn detect_package_manager(_project_root: &Path, _manifest: &Value) -> Result<String> { |
12 | | - Err(LegolasError::NotImplemented("detect_package_manager")) |
| 13 | +const FRAMEWORK_MARKERS: [FrameworkMarker; 9] = [ |
| 14 | + FrameworkMarker { |
| 15 | + name: "Next.js", |
| 16 | + packages: &["next"], |
| 17 | + files: &["next.config.js", "next.config.mjs", "next.config.ts"], |
| 18 | + }, |
| 19 | + FrameworkMarker { |
| 20 | + name: "Vite", |
| 21 | + packages: &["vite"], |
| 22 | + files: &["vite.config.js", "vite.config.ts", "vite.config.mjs"], |
| 23 | + }, |
| 24 | + FrameworkMarker { |
| 25 | + name: "Webpack", |
| 26 | + packages: &["webpack"], |
| 27 | + files: &["webpack.config.js", "webpack.config.ts"], |
| 28 | + }, |
| 29 | + FrameworkMarker { |
| 30 | + name: "Rollup", |
| 31 | + packages: &["rollup"], |
| 32 | + files: &["rollup.config.js", "rollup.config.mjs", "rollup.config.ts"], |
| 33 | + }, |
| 34 | + FrameworkMarker { |
| 35 | + name: "Astro", |
| 36 | + packages: &["astro"], |
| 37 | + files: &["astro.config.mjs", "astro.config.ts"], |
| 38 | + }, |
| 39 | + FrameworkMarker { |
| 40 | + name: "Nuxt", |
| 41 | + packages: &["nuxt"], |
| 42 | + files: &["nuxt.config.ts", "nuxt.config.js"], |
| 43 | + }, |
| 44 | + FrameworkMarker { |
| 45 | + name: "React", |
| 46 | + packages: &["react"], |
| 47 | + files: &[], |
| 48 | + }, |
| 49 | + FrameworkMarker { |
| 50 | + name: "Vue", |
| 51 | + packages: &["vue"], |
| 52 | + files: &[], |
| 53 | + }, |
| 54 | + FrameworkMarker { |
| 55 | + name: "Svelte", |
| 56 | + packages: &["svelte", "@sveltejs/kit"], |
| 57 | + files: &[], |
| 58 | + }, |
| 59 | +]; |
| 60 | + |
| 61 | +const PACKAGE_MANAGER_CHECKS: [(&str, &str); 5] = [ |
| 62 | + ("pnpm-lock.yaml", "pnpm"), |
| 63 | + ("yarn.lock", "yarn"), |
| 64 | + ("package-lock.json", "npm"), |
| 65 | + ("bun.lockb", "bun"), |
| 66 | + ("bun.lock", "bun"), |
| 67 | +]; |
| 68 | + |
| 69 | +pub fn detect_frameworks(project_root: &Path, manifest: &Value) -> Result<Vec<String>> { |
| 70 | + let all_dependencies = dependency_names(manifest); |
| 71 | + let mut detected = Vec::new(); |
| 72 | + |
| 73 | + for marker in FRAMEWORK_MARKERS { |
| 74 | + let package_hit = marker |
| 75 | + .packages |
| 76 | + .iter() |
| 77 | + .any(|package| all_dependencies.contains(*package)); |
| 78 | + let file_hit = any_exists(project_root, marker.files)?; |
| 79 | + |
| 80 | + if package_hit || file_hit { |
| 81 | + detected.push(marker.name.to_string()); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + Ok(detected) |
| 86 | +} |
| 87 | + |
| 88 | +pub fn detect_package_manager(project_root: &Path, manifest: &Value) -> Result<String> { |
| 89 | + if let Some(explicit) = manifest |
| 90 | + .get("packageManager") |
| 91 | + .and_then(Value::as_str) |
| 92 | + .filter(|value| !value.is_empty()) |
| 93 | + { |
| 94 | + return Ok(explicit.to_string()); |
| 95 | + } |
| 96 | + |
| 97 | + for (file, name) in PACKAGE_MANAGER_CHECKS { |
| 98 | + if exists(project_root.join(file))? { |
| 99 | + return Ok(name.to_string()); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + Ok("unknown".to_string()) |
| 104 | +} |
| 105 | + |
| 106 | +fn dependency_names(manifest: &Value) -> HashSet<String> { |
| 107 | + let mut dependencies = HashSet::new(); |
| 108 | + |
| 109 | + for field in ["dependencies", "devDependencies"] { |
| 110 | + if let Some(entries) = manifest.get(field).and_then(Value::as_object) { |
| 111 | + dependencies.extend(entries.keys().cloned()); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + dependencies |
| 116 | +} |
| 117 | + |
| 118 | +fn any_exists(project_root: &Path, files: &[&str]) -> Result<bool> { |
| 119 | + for file in files { |
| 120 | + if exists(project_root.join(file))? { |
| 121 | + return Ok(true); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + Ok(false) |
13 | 126 | } |
0 commit comments