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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#![allow(clippy::all)]

//! #Paths module
//!
//! The `paths` module is used to get the project root path.
use super::utils::strip_trailing_newline;
use execute::Execute;
use std::{
    env,
    path::{Path, PathBuf},
    process::{Command, Stdio},
};

/// Get the project root path.
pub fn get_project_root_path(root: Option<PathBuf>) -> Option<String> {
    let env_dir = match root {
        Some(dir) => Ok(dir),
        None => env::current_dir(),
    };

    let current_dir = match env_dir {
        Ok(dir) => dir,
        _ => PathBuf::from("./"),
    };
    let current_path = current_dir.as_path();

    let git_root_dir = walk_reverse_dir(&current_path);

    let project_root = match git_root_dir {
        Some(current) => current,
        None => {
            let search_root = get_git_root_dir(&current_path);
            search_root.unwrap_or(current_path.to_str().unwrap().to_string())
        }
    };

    let canonic_path = &std::fs::canonicalize(Path::new(&project_root)).unwrap();
    let root = canonic_path.as_path().display().to_string();

    Some(root)
}

/// Get the git root directory.
fn get_git_root_dir(dir: &Path) -> Option<String> {
    let mut command = Command::new("git");
    command.arg("rev-parse").arg("--show-toplevel");

    command.current_dir(dir);

    command.stdout(Stdio::piped());
    command.stderr(Stdio::piped());

    let output = command.execute_output().unwrap();

    if output.status.success() {
        let output = String::from_utf8(output.stdout).unwrap();
        return Some(strip_trailing_newline(&output));
    }

    None
}

/// Walk reverse directory to find the root project.
fn walk_reverse_dir(path: &Path) -> Option<String> {
    let current_path = path.to_path_buf();
    let map_files = vec![
        ("package-lock.json", "npm"),
        ("npm-shrinkwrap.json", "npm"),
        ("yarn.lock", "yarn"),
        ("pnpm-lock.yaml", "pnpm"),
        ("bun.lockb", "bun"),
    ];

    for (file, _) in map_files.iter() {
        let lock_file = current_path.join(file);

        if lock_file.exists() {
            return Some(current_path.to_str().unwrap().to_string());
        }
    }

    if let Some(parent) = path.parent() {
        return walk_reverse_dir(parent);
    }

    None
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::manager::PackageManager;
    use crate::utils::create_test_monorepo;
    use std::fs::{remove_dir_all, rename};
    use std::path::Path;

    fn git_dir_rename(from: &Path, to: &Path) {
        rename(from, to).expect("Rename dir");
    }

    #[test]
    fn npm_root_project() -> Result<(), Box<dyn std::error::Error>> {
        let ref monorepo_dir = create_test_monorepo(&PackageManager::Npm)?;
        let git_home = monorepo_dir.join(".git");
        let no_git = monorepo_dir.join(".no_git");

        git_dir_rename(&git_home, &no_git);

        let project_root = get_project_root_path(Some(monorepo_dir.to_path_buf()));

        assert_eq!(
            project_root,
            Some(monorepo_dir.to_str().unwrap().to_string())
        );

        remove_dir_all(&monorepo_dir)?;
        Ok(())
    }

    #[test]
    fn yarn_root_project() -> Result<(), Box<dyn std::error::Error>> {
        let ref monorepo_dir = create_test_monorepo(&PackageManager::Yarn)?;
        let git_home = monorepo_dir.join(".git");
        let no_git = monorepo_dir.join(".no_git");

        git_dir_rename(&git_home, &no_git);

        let project_root = get_project_root_path(Some(monorepo_dir.to_path_buf()));

        assert_eq!(
            project_root,
            Some(monorepo_dir.to_str().unwrap().to_string())
        );

        remove_dir_all(&monorepo_dir)?;
        Ok(())
    }

    #[test]
    fn pnpm_root_project() -> Result<(), Box<dyn std::error::Error>> {
        let ref monorepo_dir = create_test_monorepo(&PackageManager::Pnpm)?;
        let git_home = monorepo_dir.join(".git");
        let no_git = monorepo_dir.join(".no_git");

        git_dir_rename(&git_home, &no_git);

        let project_root = get_project_root_path(Some(monorepo_dir.to_path_buf()));

        assert_eq!(
            project_root,
            Some(monorepo_dir.to_str().unwrap().to_string())
        );

        remove_dir_all(&monorepo_dir)?;
        Ok(())
    }

    #[test]
    fn bun_root_project() -> Result<(), Box<dyn std::error::Error>> {
        let ref monorepo_dir = create_test_monorepo(&PackageManager::Bun)?;
        let git_home = monorepo_dir.join(".git");
        let no_git = monorepo_dir.join(".no_git");

        git_dir_rename(&git_home, &no_git);

        let project_root = get_project_root_path(Some(monorepo_dir.to_path_buf()));

        assert_eq!(
            project_root,
            Some(monorepo_dir.to_str().unwrap().to_string())
        );

        remove_dir_all(&monorepo_dir)?;
        Ok(())
    }

    #[test]
    fn git_root_project() -> Result<(), Box<dyn std::error::Error>> {
        let ref monorepo_dir = create_test_monorepo(&PackageManager::Npm)?;
        let project_root = get_project_root_path(Some(monorepo_dir.to_path_buf()));

        assert_eq!(project_root.is_some(), true);
        remove_dir_all(&monorepo_dir)?;
        Ok(())
    }
}