Skip to content

Commit e601e70

Browse files
Merge pull request rustwasm#198 from Mackiovello/issue/189
Improve UX for pack and publish path expectations
2 parents 77fb9cd + 88fdbc5 commit e601e70

File tree

8 files changed

+85
-25
lines changed

8 files changed

+85
-25
lines changed

docs/pack-and-publish.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# pack and publish
2+
3+
The `publish` and `pack` commands interact with the pkg directory that's
4+
created when you run `wasm-pack init`. The `pack` command creates a tarball
5+
from the pkg directory and the `publish` command creates a tarball from the
6+
pkg directory __and__ publishes it to the NPM registry.
7+
8+
Underneath, these commands use `npm pack` and `npm publish`. You can read
9+
more about these in the NPM documentation:
10+
11+
- [`npm pack`](https://docs.npmjs.com/cli/pack)
12+
- [`npm publish`](https://docs.npmjs.com/cli/publish)
13+
14+
Both these commands take the path to the pkg directory as the first argument.
15+
You can either set the argument directly to the pkg directory or to the parent
16+
of the pkg directory:
17+
18+
```
19+
$ wasm-pack pack myproject/pkg
20+
| 🎒 packed up your package!
21+
$ wasm-pack pack myproject
22+
| 🎒 packed up your package!
23+
```
24+
25+
If you try to call `pack` or `publish` on another directory, you get an error:
26+
27+
```
28+
$ wasm-pack pack myproject/src/
29+
Unable to find the pkg directory at path 'myproject/src/', or in a child directory of 'myproject/src/'
30+
```
31+
32+
If you don't set a path, they use the current directory as the path.

docs/pack.md

-10
This file was deleted.

docs/publish.md

-4
This file was deleted.

src/command/pack.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1-
use command::utils::set_crate_path;
1+
use command::utils::{find_pkg_directory, set_crate_path};
22
use error::Error;
33
use npm;
44
use slog::Logger;
55
use std::result;
66
use PBAR;
77

8+
/// Executes the 'npm pack' command on the 'pkg' directory
9+
/// which creates a tarball that can be published to the NPM registry
810
pub fn pack(path: Option<String>, log: &Logger) -> result::Result<(), Error> {
911
let crate_path = set_crate_path(path);
1012

1113
info!(&log, "Packing up the npm package...");
12-
npm::npm_pack(&crate_path)?;
14+
let pkg_directory = find_pkg_directory(&crate_path).ok_or(Error::PkgNotFound {
15+
message: format!(
16+
"Unable to find the pkg directory at path '{}', or in a child directory of '{}'",
17+
&crate_path, &crate_path
18+
),
19+
})?;
20+
npm::npm_pack(&pkg_directory.to_string_lossy())?;
1321
#[cfg(not(target_os = "windows"))]
1422
info!(&log, "Your package is located at {}/pkg", &crate_path);
1523
#[cfg(target_os = "windows")]

src/command/publish.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
1-
use command::utils::set_crate_path;
1+
use command::utils::{find_pkg_directory, set_crate_path};
22
use error::Error;
33
use npm;
44
use slog::Logger;
55
use std::result;
66
use PBAR;
77

8+
/// Creates a tarball from a 'pkg' directory
9+
/// and publishes it to the NPM registry
810
pub fn publish(path: Option<String>, log: &Logger) -> result::Result<(), Error> {
911
let crate_path = set_crate_path(path);
1012

1113
info!(&log, "Publishing the npm package...");
1214
info!(&log, "npm info located in the npm debug log");
13-
npm::npm_publish(&crate_path)?;
15+
let pkg_directory = find_pkg_directory(&crate_path).ok_or(Error::PkgNotFound {
16+
message: format!(
17+
"Unable to find the pkg directory at path '{}', or in a child directory of '{}'",
18+
&crate_path, &crate_path
19+
),
20+
})?;
21+
22+
npm::npm_publish(&pkg_directory.to_string_lossy())?;
1423
info!(&log, "Published your package!");
1524

1625
PBAR.message("💥 published your package!");

src/command/utils.rs

+21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Utility functions for commands.
22
3+
use std::path::{Path, PathBuf};
4+
35
/// If an explicit path is given, then use it, otherwise assume the current
46
/// directory is the crate path.
57
pub fn set_crate_path(path: Option<String>) -> String {
@@ -10,3 +12,22 @@ pub fn set_crate_path(path: Option<String>) -> String {
1012

1113
crate_path
1214
}
15+
16+
/// Locates the pkg directory from a specific path
17+
/// Returns None if unable to find the 'pkg' directory
18+
pub fn find_pkg_directory(guess_path: &str) -> Option<PathBuf> {
19+
let path = PathBuf::from(guess_path);
20+
if is_pkg_directory(&path) {
21+
return Some(path);
22+
}
23+
24+
path.read_dir().ok().and_then(|entries| {
25+
entries
26+
.filter_map(|x| x.ok().map(|v| v.path()))
27+
.find(|x| is_pkg_directory(&x))
28+
})
29+
}
30+
31+
fn is_pkg_directory(path: &Path) -> bool {
32+
path.exists() && path.is_dir() && path.ends_with("pkg")
33+
}

src/error.rs

+9
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ pub enum Error {
3434
/// A message describing the configuration error.
3535
message: String,
3636
},
37+
#[fail(display = "{}", message)]
38+
/// Error when the 'pkg' directory is not found.
39+
PkgNotFound {
40+
/// Message describing the error.
41+
message: String,
42+
},
3743
}
3844

3945
impl Error {
@@ -65,6 +71,9 @@ impl Error {
6571
Error::CrateConfig { message: _ } => {
6672
"There was a crate configuration error. Details:\n\n"
6773
}
74+
Error::PkgNotFound {
75+
message: _,
76+
} => "Unable to find the 'pgk' directory at the path, set the path as the parent of the 'pkg' directory \n\n",
6877
}.to_string()
6978
}
7079
}

src/npm.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@ pub const DEFAULT_NPM_REGISTRY: &'static str = "https://registry.npmjs.org/";
88

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

2420
/// Run the `npm publish` command.
2521
pub fn npm_publish(path: &str) -> Result<(), Error> {
26-
let pkg_file_path = format!("{}/pkg", path);
2722
let output = Command::new("npm")
28-
.current_dir(pkg_file_path)
23+
.current_dir(path)
2924
.arg("publish")
3025
.output()?;
3126
if !output.status.success() {

0 commit comments

Comments
 (0)