Merge pull request #198 from Mackiovello/issue/189

Improve UX for pack and publish path expectations
master
ashley williams 7 years ago committed by GitHub
commit e601e701a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 32
      docs/pack-and-publish.md
  2. 10
      docs/pack.md
  3. 4
      docs/publish.md
  4. 12
      src/command/pack.rs
  5. 13
      src/command/publish.rs
  6. 21
      src/command/utils.rs
  7. 9
      src/error.rs
  8. 9
      src/npm.rs

@ -0,0 +1,32 @@
# pack and publish
The `publish` and `pack` commands interact with the pkg directory that's
created when you run `wasm-pack init`. The `pack` command creates a tarball
from the pkg directory and the `publish` command creates a tarball from the
pkg directory __and__ publishes it to the NPM registry.
Underneath, these commands use `npm pack` and `npm publish`. You can read
more about these in the NPM documentation:
- [`npm pack`](https://docs.npmjs.com/cli/pack)
- [`npm publish`](https://docs.npmjs.com/cli/publish)
Both these commands take the path to the pkg directory as the first argument.
You can either set the argument directly to the pkg directory or to the parent
of the pkg directory:
```
$ wasm-pack pack myproject/pkg
| 🎒 packed up your package!
$ wasm-pack pack myproject
| 🎒 packed up your package!
```
If you try to call `pack` or `publish` on another directory, you get an error:
```
$ wasm-pack pack myproject/src/
Unable to find the pkg directory at path 'myproject/src/', or in a child directory of 'myproject/src/'
```
If you don't set a path, they use the current directory as the path.

@ -1,10 +0,0 @@
# wasm-pack pack
The `wasm-pack pack` command is used to create a tarball of your wasm project
using the `npm pack` command. This is useful if you would like to create a
local tarball containing your package, without publishing it to the npm
registry.
You can read more about the `npm pack` command [here][npm-pack-documentation].
[npm-pack-documentation]: https://docs.npmjs.com/cli/pack

@ -1,4 +0,0 @@
# wasm-pack publish
The `wasm-pack publish` command will create a tarball of your wasm project,
and publish it to the npm registry.

@ -1,15 +1,23 @@
use command::utils::set_crate_path; use command::utils::{find_pkg_directory, set_crate_path};
use error::Error; use error::Error;
use npm; use npm;
use slog::Logger; use slog::Logger;
use std::result; use std::result;
use PBAR; use PBAR;
/// Executes the 'npm pack' command on the 'pkg' directory
/// which creates a tarball that can be published to the NPM registry
pub fn pack(path: Option<String>, log: &Logger) -> result::Result<(), Error> { pub fn pack(path: Option<String>, log: &Logger) -> result::Result<(), Error> {
let crate_path = set_crate_path(path); let crate_path = set_crate_path(path);
info!(&log, "Packing up the npm package..."); info!(&log, "Packing up the npm package...");
npm::npm_pack(&crate_path)?; 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
),
})?;
npm::npm_pack(&pkg_directory.to_string_lossy())?;
#[cfg(not(target_os = "windows"))] #[cfg(not(target_os = "windows"))]
info!(&log, "Your package is located at {}/pkg", &crate_path); info!(&log, "Your package is located at {}/pkg", &crate_path);
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]

@ -1,16 +1,25 @@
use command::utils::set_crate_path; use command::utils::{find_pkg_directory, set_crate_path};
use error::Error; use error::Error;
use npm; use npm;
use slog::Logger; use slog::Logger;
use std::result; use std::result;
use PBAR; use PBAR;
/// Creates a tarball from a 'pkg' directory
/// and publishes it to the NPM registry
pub fn publish(path: Option<String>, log: &Logger) -> result::Result<(), Error> { pub fn publish(path: Option<String>, log: &Logger) -> result::Result<(), Error> {
let crate_path = set_crate_path(path); let crate_path = set_crate_path(path);
info!(&log, "Publishing the npm package..."); info!(&log, "Publishing the npm package...");
info!(&log, "npm info located in the npm debug log"); info!(&log, "npm info located in the npm debug log");
npm::npm_publish(&crate_path)?; 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
),
})?;
npm::npm_publish(&pkg_directory.to_string_lossy())?;
info!(&log, "Published your package!"); info!(&log, "Published your package!");
PBAR.message("💥 published your package!"); PBAR.message("💥 published your package!");

@ -1,5 +1,7 @@
//! Utility functions for commands. //! Utility functions for commands.
use std::path::{Path, PathBuf};
/// If an explicit path is given, then use it, otherwise assume the current /// If an explicit path is given, then use it, otherwise assume the current
/// directory is the crate path. /// directory is the crate path.
pub fn set_crate_path(path: Option<String>) -> String { pub fn set_crate_path(path: Option<String>) -> String {
@ -10,3 +12,22 @@ pub fn set_crate_path(path: Option<String>) -> String {
crate_path crate_path
} }
/// Locates the pkg directory from a specific path
/// Returns None if unable to find the 'pkg' directory
pub fn find_pkg_directory(guess_path: &str) -> Option<PathBuf> {
let path = PathBuf::from(guess_path);
if is_pkg_directory(&path) {
return Some(path);
}
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")
}

@ -34,6 +34,12 @@ pub enum Error {
/// A message describing the configuration error. /// A message describing the configuration error.
message: String, message: String,
}, },
#[fail(display = "{}", message)]
/// Error when the 'pkg' directory is not found.
PkgNotFound {
/// Message describing the error.
message: String,
},
} }
impl Error { impl Error {
@ -65,6 +71,9 @@ impl Error {
Error::CrateConfig { message: _ } => { Error::CrateConfig { message: _ } => {
"There was a crate configuration error. Details:\n\n" "There was a crate configuration error. Details:\n\n"
} }
Error::PkgNotFound {
message: _,
} => "Unable to find the 'pgk' directory at the path, set the path as the parent of the 'pkg' directory \n\n",
}.to_string() }.to_string()
} }
} }

@ -8,11 +8,7 @@ pub const DEFAULT_NPM_REGISTRY: &'static str = "https://registry.npmjs.org/";
/// Run the `npm pack` command. /// Run the `npm pack` command.
pub fn npm_pack(path: &str) -> Result<(), Error> { pub fn npm_pack(path: &str) -> Result<(), Error> {
let pkg_file_path = format!("{}/pkg", path); let output = Command::new("npm").current_dir(path).arg("pack").output()?;
let output = Command::new("npm")
.current_dir(pkg_file_path)
.arg("pack")
.output()?;
if !output.status.success() { if !output.status.success() {
let s = String::from_utf8_lossy(&output.stderr); let s = String::from_utf8_lossy(&output.stderr);
Error::cli("Packaging up your code failed", s) Error::cli("Packaging up your code failed", s)
@ -23,9 +19,8 @@ pub fn npm_pack(path: &str) -> Result<(), Error> {
/// Run the `npm publish` command. /// Run the `npm publish` command.
pub fn npm_publish(path: &str) -> Result<(), Error> { pub fn npm_publish(path: &str) -> Result<(), Error> {
let pkg_file_path = format!("{}/pkg", path);
let output = Command::new("npm") let output = Command::new("npm")
.current_dir(pkg_file_path) .current_dir(path)
.arg("publish") .arg("publish")
.output()?; .output()?;
if !output.status.success() { if !output.status.success() {

Loading…
Cancel
Save