|
| 1 | +use std::path::{ MAIN_SEPARATOR, PathBuf }; |
| 2 | + |
| 3 | +use clap::Parser; |
| 4 | +use serde_json::{json, Map, Value}; |
| 5 | + |
| 6 | +use pacquet_package_json::{DependencyGroup, PackageJson}; |
| 7 | + |
| 8 | +use crate::package_manager::{PackageManager, PackageManagerError}; |
| 9 | + |
| 10 | +#[derive(Parser, Debug)] |
| 11 | +pub struct ListArgs { |
| 12 | + /// Perform the command on every package subdirectories |
| 13 | + /// or on every workspace |
| 14 | + #[arg(long = "recursive", short = 'r')] |
| 15 | + pub recursive: bool, |
| 16 | + /// Log the output as JSON |
| 17 | + #[arg(long = "json")] |
| 18 | + pub json: bool, |
| 19 | + /// Show the extended information |
| 20 | + #[arg(long = "long")] |
| 21 | + pub long: bool, |
| 22 | + /// Outputs the package directories into a parseable format |
| 23 | + #[arg(long = "parseable")] |
| 24 | + pub parseable: bool, |
| 25 | + /// List the package in the global install directory instead of the |
| 26 | + /// current project |
| 27 | + #[arg(long = "global", short = 'g')] |
| 28 | + pub global: bool, |
| 29 | + /// Max display depth of the dependency tree |
| 30 | + #[arg(long = "depth")] |
| 31 | + pub depth: u32, |
| 32 | + /// Display only dependencies within dependencies or optionalDependencies |
| 33 | + #[arg(long = "prod", short = 'p')] |
| 34 | + pub prod: bool, |
| 35 | + /// Display only dependencies within devDependencies |
| 36 | + #[arg(long = "dev", short = 'd')] |
| 37 | + pub dev: bool, |
| 38 | + /// Omit packages from optionalDependencies |
| 39 | + #[arg(long = "no-optional")] |
| 40 | + pub no_opts: bool, |
| 41 | + /// Display only depndencies that are also projects within the workspace |
| 42 | + #[arg(long = "only-projects")] |
| 43 | + pub projects_only: bool, |
| 44 | + /// Display the dependencies from a given subset of dependencies |
| 45 | + #[arg(long = "filter", short = 'f')] |
| 46 | + pub filter: String, |
| 47 | +} |
| 48 | + |
| 49 | +impl ListArgs { |
| 50 | + pub fn get_scope(&self) -> DependencyGroup { |
| 51 | + if self.dev { |
| 52 | + DependencyGroup::Dev |
| 53 | + } else { |
| 54 | + DependencyGroup::Default |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + pub fn get_depth(&self) -> u32 { |
| 59 | + let mut depth: u32 = 1; |
| 60 | + self.depth.clone_into(&mut depth); |
| 61 | + |
| 62 | + depth |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +impl PackageManager { |
| 67 | + pub fn list( |
| 68 | + &self, |
| 69 | + package_json: &PackageJson, |
| 70 | + dependency_group: DependencyGroup, |
| 71 | + node_modules_path: &PathBuf, |
| 72 | + depth: u32, |
| 73 | + ) -> Result<String, PackageManagerError> { |
| 74 | + let mut scope: String = String::new(); |
| 75 | + match dependency_group { |
| 76 | + DependencyGroup::Default => { |
| 77 | + let dependencies = package_json.get_dependencies(vec![DependencyGroup::Default]); |
| 78 | + |
| 79 | + for (name, version) in &dependencies { |
| 80 | + scope = format!("{} - {}\n", name, version); |
| 81 | + |
| 82 | + if depth > 1 { |
| 83 | + let path = format!("{}{}{}", &name, MAIN_SEPARATOR, "package.json"); |
| 84 | + let pjson = PackageJson::from_path(&node_modules_path.join(path))?; |
| 85 | + let n_dependencies = self.list(&pjson, DependencyGroup::Default, node_modules_path, depth - 1)?; |
| 86 | + // TODO: fix format |
| 87 | + scope = format!("\n\t{}", n_dependencies) |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + DependencyGroup::Dev => scope = "dependencies".to_string(), |
| 92 | + _ => scope = "all".to_string(), |
| 93 | + } |
| 94 | + |
| 95 | + // let binding = Value::default(); |
| 96 | + // let mut dependencies = self.value.get(scope).unwrap_or(&binding).as_object().into_iter(); |
| 97 | + |
| 98 | + // let mut dep = dependencies.next(); |
| 99 | + // while !dep.is_none() { |
| 100 | + // println!("{:?}", dep); |
| 101 | + // dep = dependencies.next(); |
| 102 | + // } |
| 103 | + |
| 104 | + println!("{}", scope); |
| 105 | + Ok(scope.clone()) |
| 106 | + } |
| 107 | +} |
0 commit comments