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
use regex::Regex;
use std::fs;

pub fn is_valid_package_name(project_name: &str) -> bool {
    Regex::new(r"^(?:@[a-z0-9-*~][a-z0-9-*._~]*/)?[a-z0-9-~][a-z0-9-._~]*$")
        .unwrap()
        .is_match(project_name)
}

pub fn to_valid_package_name(project_name: &str) -> String {
    let name = project_name
        .trim()
        .to_lowercase()
        .replace(char::is_whitespace, "-")
        .trim_start_matches(|c| c == '.' || c == '_')
        .replace(
            |c: char| !(c.is_ascii_alphanumeric() || c == '-' || c == '~'),
            "-",
        );
    name
}

pub fn can_skip_emptying(dir: &str) -> bool {
    if let Ok(metadata) = fs::metadata(dir) {
        if !metadata.is_dir() {
            return false;
        }
    } else {
        return true;
    }

    if let Ok(files) = fs::read_dir(dir) {
        let mut entries = 0;
        for file in files {
            if let Ok(entry) = file {
                if entry.file_name() == ".git" {
                    return entries == 0;
                }
                entries += 1;
            }
        }
        return entries == 0;
    }

    false
}

pub fn empty_dir(dir: &str) {
    if !fs::metadata(dir).is_ok() {
        return;
    }
    println!("Target directory already exists or is not empty, will be reset.");
    fs::remove_dir_all(dir).unwrap();
    fs::create_dir(dir).unwrap();
    // post_order_directory_traverse(dir, |dir| fs::remove_dir(dir), |file| fs::remove_file(file))?;
}