diff --git a/src/command/pack.rs b/src/command/pack.rs index cbdffbf..6bdf6e7 100644 --- a/src/command/pack.rs +++ b/src/command/pack.rs @@ -1,7 +1,8 @@ -use command::utils::set_crate_path; +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; @@ -9,14 +10,13 @@ pub fn pack(path: Option, log: &Logger) -> result::Result<(), Error> { let crate_path = set_crate_path(path); info!(&log, "Packing up the npm package..."); - npm::npm_pack(&crate_path).map_err(|e| { - match e { - Error::Io { .. } => Error::PkgNotFound { - message: format!("Unable to find the pkg directory at path '{}', set the path as the parent directory of the pkg directory", &crate_path), - }, - e => e, - } + let pkg_directory: Box<&Path> = 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 + ), })?; + npm::npm_pack(&pkg_directory.to_string_lossy())?; #[cfg(not(target_os = "windows"))] info!(&log, "Your package is located at {}/pkg", &crate_path); #[cfg(target_os = "windows")] diff --git a/src/command/publish.rs b/src/command/publish.rs index 48c8fe0..55d8667 100644 --- a/src/command/publish.rs +++ b/src/command/publish.rs @@ -1,4 +1,4 @@ -use command::utils::set_crate_path; +use command::utils::{find_pkg_directory, set_crate_path}; use error::Error; use npm; use slog::Logger; @@ -24,22 +24,3 @@ pub fn publish(path: Option, log: &Logger) -> result::Result<(), Error> PBAR.message("💥 published your package!"); Ok(()) } - -fn find_pkg_directory(guess_path: &str) -> Option> { - let path = Path::new(guess_path); - if is_pkg_directory(path) { - return Some(Box::new(path)); - } - - path.parent().and_then(|v| { - if is_pkg_directory(v) { - Some(Box::new(v)) - } else { - None - } - }) -} - -fn is_pkg_directory(path: &Path) -> bool { - path.exists() && path.is_dir() && path.ends_with("pkg") -} diff --git a/src/command/utils.rs b/src/command/utils.rs index ff85ef0..2bbd365 100644 --- a/src/command/utils.rs +++ b/src/command/utils.rs @@ -1,3 +1,5 @@ +use std::path::Path; + pub fn set_crate_path(path: Option) -> String { let crate_path = match path { Some(p) => p, @@ -6,3 +8,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)); + } + + path.parent().and_then(|v| { + if is_pkg_directory(v) { + Some(Box::new(v)) + } else { + None + } + }) +} + +fn is_pkg_directory(path: &Path) -> bool { + path.exists() && path.is_dir() && path.ends_with("pkg") +}