Skip to content

Commit

Permalink
Merge pull request #198 from Mackiovello/issue/189
Browse files Browse the repository at this point in the history
Improve UX for pack and publish path expectations
  • Loading branch information
ashleygwilliams authored Jul 19, 2018
2 parents 77fb9cd + 88fdbc5 commit e601e70
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 25 deletions.
32 changes: 32 additions & 0 deletions docs/pack-and-publish.md
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 0 additions & 10 deletions docs/pack.md

This file was deleted.

4 changes: 0 additions & 4 deletions docs/publish.md

This file was deleted.

12 changes: 10 additions & 2 deletions src/command/pack.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
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::result;
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> {
let crate_path = set_crate_path(path);

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"))]
info!(&log, "Your package is located at {}/pkg", &crate_path);
#[cfg(target_os = "windows")]
Expand Down
13 changes: 11 additions & 2 deletions src/command/publish.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
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::result;
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> {
let crate_path = set_crate_path(path);

info!(&log, "Publishing the npm package...");
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!");

PBAR.message("💥 published your package!");
Expand Down
21 changes: 21 additions & 0 deletions src/command/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Utility functions for commands.

use std::path::{Path, PathBuf};

/// 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<String>) -> String {
Expand All @@ -10,3 +12,22 @@ pub fn set_crate_path(path: Option<String>) -> String {

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")
}
9 changes: 9 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ pub enum Error {
/// A message describing the configuration error.
message: String,
},
#[fail(display = "{}", message)]
/// Error when the 'pkg' directory is not found.
PkgNotFound {
/// Message describing the error.
message: String,
},
}

impl Error {
Expand Down Expand Up @@ -65,6 +71,9 @@ impl Error {
Error::CrateConfig { message: _ } => {
"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()
}
}
Expand Down
9 changes: 2 additions & 7 deletions src/npm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ pub const DEFAULT_NPM_REGISTRY: &'static str = "https://registry.npmjs.org/";

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

/// Run the `npm publish` command.
pub fn npm_publish(path: &str) -> Result<(), Error> {
let pkg_file_path = format!("{}/pkg", path);
let output = Command::new("npm")
.current_dir(pkg_file_path)
.current_dir(path)
.arg("publish")
.output()?;
if !output.status.success() {
Expand Down

0 comments on commit e601e70

Please sign in to comment.