-
Notifications
You must be signed in to change notification settings - Fork 167
Expand file tree
/
Copy pathlib.rs
More file actions
43 lines (40 loc) · 1.36 KB
/
lib.rs
File metadata and controls
43 lines (40 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//! Shared utilities for vite-plus crates
mod env_config;
pub mod env_vars;
pub mod header;
mod home;
pub mod output;
mod package_json;
mod path_env;
pub mod string_similarity;
mod tracing;
pub use env_config::{EnvConfig, TestEnvGuard};
pub use home::get_vite_plus_home;
pub use package_json::{DevEngines, Engines, PackageJson, RuntimeEngine, RuntimeEngineConfig};
pub use path_env::{
PrependOptions, PrependResult, format_path_prepended, format_path_with_prepend,
prepend_to_path_env,
};
pub use tracing::init_tracing;
/// Read the project name from the nearest `package.json` in the given directory.
///
/// Walks up the directory tree from `start_dir` looking for a `package.json` file
/// with a `name` field. Returns `None` if no such file is found or if it cannot
/// be parsed.
pub fn read_project_name(start_dir: &std::path::Path) -> Option<String> {
let mut dir = Some(start_dir);
while let Some(current) = dir {
let pkg_path = current.join("package.json");
if let Ok(contents) = std::fs::read_to_string(&pkg_path) {
if let Ok(pkg) = serde_json::from_str::<PackageJson>(&contents) {
if let Some(name) = pkg.name {
if !name.is_empty() {
return Some(name.to_string());
}
}
}
}
dir = current.parent();
}
None
}