From b840f6aecd4751ca62e89d2220ba5ac28eac2af8 Mon Sep 17 00:00:00 2001 From: "guillem.cordoba" Date: Tue, 14 May 2024 15:21:37 +0200 Subject: [PATCH] choose_npm_package --- crates/npm_scaffolding_utils/src/lib.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/crates/npm_scaffolding_utils/src/lib.rs b/crates/npm_scaffolding_utils/src/lib.rs index 87c916e..fba1f68 100644 --- a/crates/npm_scaffolding_utils/src/lib.rs +++ b/crates/npm_scaffolding_utils/src/lib.rs @@ -36,6 +36,24 @@ fn default_select_npm_package( .interact()?) } +pub fn choose_npm_package(file_tree: &FileTree, prompt: &String) -> Result { + let package_jsons = find_files_by_name(&file_tree, PathBuf::from("package.json").as_path()); + + let package_jsons: Vec<(PathBuf, String)> = package_jsons.into_iter().collect(); + let packages_names = package_jsons + .iter() + .map(|package_json| get_npm_package_name(package_json)) + .collect::, NpmScaffoldingUtilsError>>()?; + + let index = Select::with_theme(&ColorfulTheme::default()) + .with_prompt(prompt) + .default(0) + .items(&packages_names[..]) + .interact()?; + + Ok(packages_names[index].clone()) +} + pub fn add_npm_dependency( mut file_tree: FileTree, dependency: String,