hawk_cli/models/
environment_files.rs1use serde::Deserialize;
2use std::{
3 fs,
4 path::{Path, PathBuf},
5};
6use walkdir::WalkDir;
7
8use crate::{models::files::File, utils::is_workflow_file};
9
10#[derive(Debug, Deserialize, Clone)]
11pub struct PackageJson {
12 pub name: String,
13 pub workspaces: Option<Vec<String>>,
14}
15
16impl File<PackageJson> for PackageJson {}
17impl PackageJson {
18 pub fn has_workspaces(self) -> bool {
19 self.workspaces.unwrap().is_empty()
20 }
21}
22
23#[derive(Debug, Deserialize, Clone)]
24pub struct PnpmWorkspace {
25 pub packages: Vec<String>,
26}
27impl File<PnpmWorkspace> for PnpmWorkspace {}
28
29pub fn search_file(search_path: &str, filename: &str) -> Option<PathBuf> {
30 match fs::read_dir(search_path) {
31 Err(_) => None,
32 Ok(content) => {
33 let mut fpath: Option<PathBuf> = None;
34
35 for f in content {
36 let path = match f {
37 Err(_) => None,
38 Ok(entry) => Some(entry.path()),
39 };
40
41 if let Some(p) = path {
42 if p.is_file() && p.file_name().unwrap().to_str().unwrap() == filename {
43 fpath = Some(p);
44 break;
45 }
46 }
47 }
48
49 fpath
50 }
51 }
52}
53
54pub fn is_empty_dir(path: &Path) -> bool {
55 let count = WalkDir::new(path)
56 .into_iter()
57 .filter(|r| r.as_ref().map_or(false, |e| is_workflow_file(e.path())))
58 .count();
59
60 count == 0
61}
62
63pub fn list_dirs(path: &Path) -> Vec<PathBuf> {
64 WalkDir::new(path)
65 .into_iter()
66 .filter_map(move |p| match p {
67 Ok(entry) => Some(entry),
68 _ => None,
69 })
70 .filter(|p| p.path().is_dir())
71 .map(|f| PathBuf::from(f.path()))
72 .collect()
73}
74
75pub fn list_files(path: &Path) -> Vec<PathBuf> {
76 WalkDir::new(path)
77 .into_iter()
78 .filter_map(move |p| match p {
79 Ok(entry) => Some(entry),
80 _ => None,
81 })
82 .filter(|p| p.path().is_file())
83 .map(|f| PathBuf::from(f.path()))
84 .collect()
85}