create_vue_business_rs/utils/
util.rs1use regex::Regex;
2use std::fs;
3
4pub fn is_valid_package_name(project_name: &str) -> bool {
5 Regex::new(r"^(?:@[a-z0-9-*~][a-z0-9-*._~]*/)?[a-z0-9-~][a-z0-9-._~]*$")
6 .unwrap()
7 .is_match(project_name)
8}
9
10pub fn to_valid_package_name(project_name: &str) -> String {
11 let name = project_name
12 .trim()
13 .to_lowercase()
14 .replace(char::is_whitespace, "-")
15 .trim_start_matches(|c| c == '.' || c == '_')
16 .replace(
17 |c: char| !(c.is_ascii_alphanumeric() || c == '-' || c == '~'),
18 "-",
19 );
20 name
21}
22
23pub fn can_skip_emptying(dir: &str) -> bool {
24 if let Ok(metadata) = fs::metadata(dir) {
25 if !metadata.is_dir() {
26 return false;
27 }
28 } else {
29 return true;
30 }
31
32 if let Ok(files) = fs::read_dir(dir) {
33 let mut entries = 0;
34 for file in files {
35 if let Ok(entry) = file {
36 if entry.file_name() == ".git" {
37 return entries == 0;
38 }
39 entries += 1;
40 }
41 }
42 return entries == 0;
43 }
44
45 false
46}
47
48pub fn empty_dir(dir: &str) {
49 if !fs::metadata(dir).is_ok() {
50 return;
51 }
52 println!("Target directory already exists or is not empty, will be reset.");
53 fs::remove_dir_all(dir).unwrap();
54 fs::create_dir(dir).unwrap();
55 }