ginger_shared_rs/
utils.rs

1use serde_json::Value;
2use serde_json::Value as JsonValue;
3use std::fs::File;
4use std::io::Read;
5use std::path::PathBuf;
6use std::process::exit;
7
8pub fn split_slug(slug: &str) -> Option<(String, String)> {
9    // Attempt to split the slug into two parts based on the '/'
10    match slug.split_once('/') {
11        Some((org_id, name)) => Some((org_id.to_string(), name.to_string())),
12        None => None, // Return None if the slug does not contain a '/'
13    }
14}
15
16pub fn get_token_from_file_storage() -> String {
17    let home_dir = match dirs::home_dir() {
18        Some(path) => path,
19        None => {
20            println!("Failed to locate home directory. Exiting.");
21            exit(1);
22        }
23    };
24
25    // Construct the path to the auth.json file
26    let auth_file_path: PathBuf = [home_dir.to_str().unwrap(), ".ginger-society", "auth.json"]
27        .iter()
28        .collect();
29
30    // Read the token from the file
31    let mut file = match File::open(&auth_file_path) {
32        Ok(f) => f,
33        Err(_) => {
34            println!("Failed to open {}. Exiting.", auth_file_path.display());
35            exit(1);
36        }
37    };
38    let mut contents = String::new();
39    if let Err(_) = file.read_to_string(&mut contents) {
40        println!("Failed to read the auth.json file. Exiting.");
41        exit(1);
42    }
43
44    let json: Value = match serde_json::from_str(&contents) {
45        Ok(v) => v,
46        Err(_) => {
47            println!("Failed to parse auth.json as JSON. Exiting.");
48            exit(1);
49        }
50    };
51
52    let token = match json.get("API_TOKEN").and_then(|v| v.as_str()) {
53        Some(t) => t.to_string(),
54        None => {
55            println!("API_TOKEN not found in auth.json. Exiting.");
56            exit(1);
57        }
58    };
59
60    token
61}
62
63pub fn get_package_json_info() -> Option<(String, String, String, String, Vec<String>)> {
64    let mut file = File::open("package.json").expect("Failed to open package.json");
65    let mut content = String::new();
66    file.read_to_string(&mut content)
67        .expect("Failed to read package.json");
68
69    let package_json: JsonValue =
70        serde_json::from_str(&content).expect("Failed to parse package.json");
71
72    let name = package_json.get("name")?.as_str()?.to_string();
73    let version = package_json.get("version")?.as_str()?.to_string();
74    let description = package_json.get("description")?.as_str()?.to_string();
75
76    // Extract organization and package name
77    let (organization, package_name) = if name.starts_with('@') {
78        let parts: Vec<&str> = name.split('/').collect();
79        if parts.len() == 2 {
80            (
81                parts[0].trim_start_matches('@').to_string(),
82                parts[1].to_string(),
83            )
84        } else {
85            println!("The package name should be of format @scope/pkg-name");
86            exit(1);
87        }
88    } else {
89        println!("The package name should be of format @scope/pkg-name");
90        exit(1);
91    };
92
93    // Internal dependencies logic
94    let prefix = format!("@{}/", organization);
95    let mut internal_dependencies = Vec::new();
96
97    if let Some(dependencies) = package_json.get("dependencies").and_then(|d| d.as_object()) {
98        for (key, _) in dependencies {
99            if key.starts_with(&prefix) {
100                internal_dependencies.push(key.clone());
101            }
102        }
103    }
104
105    if let Some(dev_dependencies) = package_json
106        .get("devDependencies")
107        .and_then(|d| d.as_object())
108    {
109        for (key, _) in dev_dependencies {
110            if key.starts_with(&prefix) {
111                internal_dependencies.push(key.clone());
112            }
113        }
114    }
115
116    Some((
117        package_name,
118        version,
119        description,
120        organization,
121        internal_dependencies,
122    ))
123}