Merge pull request #1370 from Storykit/pack-and-publish-pkg-dir-option

pkg-dir option for pack and publish commands
master
Jesper Håkansson 1 year ago committed by GitHub
commit 6a76f640c2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 18
      src/command/mod.rs
  2. 9
      src/command/pack.rs
  3. 3
      src/command/publish/mod.rs
  4. 10
      src/command/utils.rs

@ -31,6 +31,10 @@ pub enum Command {
#[clap(name = "pack")] #[clap(name = "pack")]
/// 🍱 create a tar of your npm package but don't publish! /// 🍱 create a tar of your npm package but don't publish!
Pack { Pack {
#[clap(long = "pkg-dir", short = 'd', default_value = "pkg")]
/// The name of the output directory where the npm package is stored
pkg_directory: PathBuf,
/// The path to the Rust crate. If not set, searches up the path from the current directory. /// The path to the Rust crate. If not set, searches up the path from the current directory.
#[clap()] #[clap()]
path: Option<PathBuf>, path: Option<PathBuf>,
@ -68,6 +72,10 @@ pub enum Command {
#[clap(long = "tag")] #[clap(long = "tag")]
tag: Option<String>, tag: Option<String>,
#[clap(long = "pkg-dir", short = 'd', default_value = "pkg")]
/// The name of the output directory where the npm package is stored
pkg_directory: PathBuf,
/// The path to the Rust crate. If not set, searches up the path from the current directory. /// The path to the Rust crate. If not set, searches up the path from the current directory.
#[clap()] #[clap()]
path: Option<PathBuf>, path: Option<PathBuf>,
@ -113,10 +121,13 @@ pub fn run_wasm_pack(command: Command) -> Result<()> {
info!("Running build command..."); info!("Running build command...");
Build::try_from_opts(build_opts).and_then(|mut b| b.run()) Build::try_from_opts(build_opts).and_then(|mut b| b.run())
} }
Command::Pack { path } => { Command::Pack {
path,
pkg_directory,
} => {
info!("Running pack command..."); info!("Running pack command...");
info!("Path: {:?}", &path); info!("Path: {:?}", &path);
pack(path) pack(path, pkg_directory)
} }
Command::Generate { Command::Generate {
template, template,
@ -133,10 +144,11 @@ pub fn run_wasm_pack(command: Command) -> Result<()> {
path, path,
access, access,
tag, tag,
pkg_directory,
} => { } => {
info!("Running publish command..."); info!("Running publish command...");
info!("Path: {:?}", &path); info!("Path: {:?}", &path);
publish(&target, path, access, tag) publish(&target, path, access, tag, pkg_directory)
} }
Command::Login { Command::Login {
registry, registry,

@ -7,11 +7,11 @@ use std::path::PathBuf;
/// Executes the 'npm pack' command on the 'pkg' directory /// Executes the 'npm pack' command on the 'pkg' directory
/// which creates a tarball that can be published to the NPM registry /// which creates a tarball that can be published to the NPM registry
pub fn pack(path: Option<PathBuf>) -> Result<()> { pub fn pack(path: Option<PathBuf>, pkg_directory: PathBuf) -> Result<()> {
let crate_path = get_crate_path(path)?; let crate_path = get_crate_path(path)?;
info!("Packing up the npm package..."); info!("Packing up the npm package...");
let pkg_directory = find_pkg_directory(&crate_path).ok_or_else(|| { let pkg_directory = find_pkg_directory(&crate_path, &pkg_directory).ok_or_else(|| {
anyhow!( anyhow!(
"Unable to find the pkg directory at path {:#?}, or in a child directory of {:#?}", "Unable to find the pkg directory at path {:#?}, or in a child directory of {:#?}",
&crate_path, &crate_path,
@ -19,7 +19,10 @@ pub fn pack(path: Option<PathBuf>) -> Result<()> {
) )
})?; })?;
npm::npm_pack(&pkg_directory.to_string_lossy())?; npm::npm_pack(&pkg_directory.to_string_lossy())?;
info!("Your package is located at {:#?}", crate_path.join("pkg")); info!(
"Your package is located at {:#?}",
crate_path.join(pkg_directory)
);
PBAR.info("🎒 packed up your package!"); PBAR.info("🎒 packed up your package!");
Ok(()) Ok(())

@ -19,13 +19,14 @@ pub fn publish(
path: Option<PathBuf>, path: Option<PathBuf>,
access: Option<Access>, access: Option<Access>,
tag: Option<String>, tag: Option<String>,
pkg_directory: PathBuf,
) -> Result<()> { ) -> Result<()> {
let crate_path = get_crate_path(path)?; let crate_path = get_crate_path(path)?;
info!("Publishing the npm package..."); info!("Publishing the npm package...");
info!("npm info located in the npm debug log"); info!("npm info located in the npm debug log");
let pkg_directory = match find_pkg_directory(&crate_path) { let pkg_directory = match find_pkg_directory(&crate_path, &pkg_directory) {
Some(path) => Ok(path), Some(path) => Ok(path),
None => { None => {
// while `wasm-pack publish`, if the pkg directory cannot be found, // while `wasm-pack publish`, if the pkg directory cannot be found,

@ -45,19 +45,19 @@ pub fn create_pkg_dir(out_dir: &Path) -> Result<()> {
/// Locates the pkg directory from a specific path /// Locates the pkg directory from a specific path
/// Returns None if unable to find the 'pkg' directory /// Returns None if unable to find the 'pkg' directory
pub fn find_pkg_directory(path: &Path) -> Option<PathBuf> { pub fn find_pkg_directory(path: &Path, pkg_directory: &Path) -> Option<PathBuf> {
if is_pkg_directory(path) { if is_pkg_directory(path, pkg_directory) {
return Some(path.to_owned()); return Some(path.to_owned());
} }
WalkDir::new(path) WalkDir::new(path)
.into_iter() .into_iter()
.filter_map(|x| x.ok().map(|e| e.into_path())) .filter_map(|x| x.ok().map(|e| e.into_path()))
.find(|x| is_pkg_directory(&x)) .find(|x| is_pkg_directory(&x, pkg_directory))
} }
fn is_pkg_directory(path: &Path) -> bool { fn is_pkg_directory(path: &Path, pkg_directory: &Path) -> bool {
path.exists() && path.is_dir() && path.ends_with("pkg") path.exists() && path.is_dir() && path.ends_with(pkg_directory)
} }
/// Render a `Duration` to a form suitable for display on a console /// Render a `Duration` to a form suitable for display on a console

Loading…
Cancel
Save