fork of https://github.com/rustwasm/wasm-pack for the needs of NextGraph.org
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.2 KiB
46 lines
1.2 KiB
//! Utility functions for commands.
|
|
|
|
use emoji;
|
|
use error::Error;
|
|
use progressbar::Step;
|
|
use std::fs;
|
|
use std::io;
|
|
use std::path::{Path, PathBuf};
|
|
use PBAR;
|
|
|
|
/// If an explicit path is given, then use it, otherwise assume the current
|
|
/// directory is the crate path.
|
|
pub fn set_crate_path(path: Option<PathBuf>) -> io::Result<PathBuf> {
|
|
let crate_path = match path {
|
|
Some(p) => p,
|
|
None => PathBuf::from("."),
|
|
};
|
|
|
|
crate_path.canonicalize()
|
|
}
|
|
|
|
/// Construct our `pkg` directory in the crate.
|
|
pub fn create_pkg_dir(out_dir: &Path, step: &Step) -> Result<(), Error> {
|
|
let msg = format!("{}Creating a pkg directory...", emoji::FOLDER);
|
|
PBAR.step(step, &msg);
|
|
fs::create_dir_all(&out_dir)?;
|
|
Ok(())
|
|
}
|
|
|
|
/// Locates the pkg directory from a specific path
|
|
/// Returns None if unable to find the 'pkg' directory
|
|
pub fn find_pkg_directory(path: &Path) -> Option<PathBuf> {
|
|
if is_pkg_directory(path) {
|
|
return Some(path.to_owned());
|
|
}
|
|
|
|
path.read_dir().ok().and_then(|entries| {
|
|
entries
|
|
.filter_map(|x| x.ok().map(|v| v.path()))
|
|
.find(|x| is_pkg_directory(&x))
|
|
})
|
|
}
|
|
|
|
fn is_pkg_directory(path: &Path) -> bool {
|
|
path.exists() && path.is_dir() && path.ends_with("pkg")
|
|
}
|
|
|