Use 'find_pkg_directory' for pack

master
Mackiovello 7 years ago
parent b57d68f97a
commit a67762a9e5
  1. 16
      src/command/pack.rs
  2. 21
      src/command/publish.rs
  3. 21
      src/command/utils.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<String>, 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")]

@ -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<String>, log: &Logger) -> result::Result<(), Error>
PBAR.message("💥 published your package!");
Ok(())
}
fn find_pkg_directory(guess_path: &str) -> Option<Box<&Path>> {
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")
}

@ -1,3 +1,5 @@
use std::path::Path;
pub fn set_crate_path(path: Option<String>) -> String {
let crate_path = match path {
Some(p) => p,
@ -6,3 +8,22 @@ pub fn set_crate_path(path: Option<String>) -> String {
crate_path
}
pub fn find_pkg_directory(guess_path: &str) -> Option<Box<&Path>> {
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")
}

Loading…
Cancel
Save