From 63c91caeae41e6e642fac503bf5798d8a6d7f514 Mon Sep 17 00:00:00 2001 From: Mackiovello Date: Sat, 7 Jul 2018 18:19:43 +0200 Subject: [PATCH] Check child directories instead of parent directory --- src/command/pack.rs | 3 +-- src/command/publish.rs | 3 +-- src/command/utils.rs | 24 ++++++++++++++---------- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/command/pack.rs b/src/command/pack.rs index 6bdf6e7..158f9f0 100644 --- a/src/command/pack.rs +++ b/src/command/pack.rs @@ -2,7 +2,6 @@ use command::utils::{find_pkg_directory, set_crate_path}; use error::Error; use npm; use slog::Logger; -use std::path::Path; use std::result; use PBAR; @@ -10,7 +9,7 @@ pub fn pack(path: Option, log: &Logger) -> result::Result<(), Error> { let crate_path = set_crate_path(path); info!(&log, "Packing up the npm package..."); - let pkg_directory: Box<&Path> = find_pkg_directory(&crate_path).ok_or(Error::PkgNotFound { + let pkg_directory = find_pkg_directory(&crate_path).ok_or(Error::PkgNotFound { message: format!( "Unable to find the pkg directory at path '{}', or in a child directory of '{}'", &crate_path, &crate_path diff --git a/src/command/publish.rs b/src/command/publish.rs index 55d8667..dfb9426 100644 --- a/src/command/publish.rs +++ b/src/command/publish.rs @@ -2,7 +2,6 @@ use command::utils::{find_pkg_directory, set_crate_path}; use error::Error; use npm; use slog::Logger; -use std::path::Path; use std::result; use PBAR; @@ -11,7 +10,7 @@ pub fn publish(path: Option, log: &Logger) -> result::Result<(), Error> info!(&log, "Publishing the npm package..."); info!(&log, "npm info located in the npm debug log"); - let pkg_directory: Box<&Path> = find_pkg_directory(&crate_path).ok_or(Error::PkgNotFound { + let pkg_directory = find_pkg_directory(&crate_path).ok_or(Error::PkgNotFound { message: format!( "Unable to find the pkg directory at path '{}', or in a child directory of '{}'", &crate_path, &crate_path diff --git a/src/command/utils.rs b/src/command/utils.rs index 2bbd365..5776b49 100644 --- a/src/command/utils.rs +++ b/src/command/utils.rs @@ -1,4 +1,4 @@ -use std::path::Path; +use std::path::{Path, PathBuf}; pub fn set_crate_path(path: Option) -> String { let crate_path = match path { @@ -9,18 +9,22 @@ pub fn set_crate_path(path: Option) -> String { crate_path } -pub fn find_pkg_directory(guess_path: &str) -> Option> { - let path = Path::new(guess_path); - if is_pkg_directory(path) { - return Some(Box::new(path)); +pub fn find_pkg_directory(guess_path: &str) -> Option { + let path = PathBuf::from(guess_path); + if is_pkg_directory(&path) { + return Some(path); } - path.parent().and_then(|v| { - if is_pkg_directory(v) { - Some(Box::new(v)) - } else { - None + path.read_dir().ok().and_then(|entries| { + for entry in entries { + if entry.is_ok() { + let p = entry.unwrap().path(); + if is_pkg_directory(&p) { + return Some(p); + } + } } + None }) }