File tree 8 files changed +85
-25
lines changed
8 files changed +85
-25
lines changed Original file line number Diff line number Diff line change
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.
Load Diff This file was deleted.
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1
- use command:: utils:: set_crate_path;
1
+ use command:: utils:: { find_pkg_directory , set_crate_path} ;
2
2
use error:: Error ;
3
3
use npm;
4
4
use slog:: Logger ;
5
5
use std:: result;
6
6
use PBAR ;
7
7
8
+ /// Executes the 'npm pack' command on the 'pkg' directory
9
+ /// which creates a tarball that can be published to the NPM registry
8
10
pub fn pack ( path : Option < String > , log : & Logger ) -> result:: Result < ( ) , Error > {
9
11
let crate_path = set_crate_path ( path) ;
10
12
11
13
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 ( ) ) ?;
13
21
#[ cfg( not( target_os = "windows" ) ) ]
14
22
info ! ( & log, "Your package is located at {}/pkg" , & crate_path) ;
15
23
#[ cfg( target_os = "windows" ) ]
Original file line number Diff line number Diff line change 1
- use command:: utils:: set_crate_path;
1
+ use command:: utils:: { find_pkg_directory , set_crate_path} ;
2
2
use error:: Error ;
3
3
use npm;
4
4
use slog:: Logger ;
5
5
use std:: result;
6
6
use PBAR ;
7
7
8
+ /// Creates a tarball from a 'pkg' directory
9
+ /// and publishes it to the NPM registry
8
10
pub fn publish ( path : Option < String > , log : & Logger ) -> result:: Result < ( ) , Error > {
9
11
let crate_path = set_crate_path ( path) ;
10
12
11
13
info ! ( & log, "Publishing the npm package..." ) ;
12
14
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 ( ) ) ?;
14
23
info ! ( & log, "Published your package!" ) ;
15
24
16
25
PBAR . message ( "💥 published your package!" ) ;
Original file line number Diff line number Diff line change 1
1
//! Utility functions for commands.
2
2
3
+ use std:: path:: { Path , PathBuf } ;
4
+
3
5
/// If an explicit path is given, then use it, otherwise assume the current
4
6
/// directory is the crate path.
5
7
pub fn set_crate_path ( path : Option < String > ) -> String {
@@ -10,3 +12,22 @@ pub fn set_crate_path(path: Option<String>) -> String {
10
12
11
13
crate_path
12
14
}
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
+ }
Original file line number Diff line number Diff line change @@ -34,6 +34,12 @@ pub enum Error {
34
34
/// A message describing the configuration error.
35
35
message : String ,
36
36
} ,
37
+ #[ fail( display = "{}" , message) ]
38
+ /// Error when the 'pkg' directory is not found.
39
+ PkgNotFound {
40
+ /// Message describing the error.
41
+ message : String ,
42
+ } ,
37
43
}
38
44
39
45
impl Error {
@@ -65,6 +71,9 @@ impl Error {
65
71
Error :: CrateConfig { message : _ } => {
66
72
"There was a crate configuration error. Details:\n \n "
67
73
}
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 " ,
68
77
} . to_string ( )
69
78
}
70
79
}
Original file line number Diff line number Diff line change @@ -8,11 +8,7 @@ pub const DEFAULT_NPM_REGISTRY: &'static str = "https://registry.npmjs.org/";
8
8
9
9
/// Run the `npm pack` command.
10
10
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 ( ) ?;
16
12
if !output. status . success ( ) {
17
13
let s = String :: from_utf8_lossy ( & output. stderr ) ;
18
14
Error :: cli ( "Packaging up your code failed" , s)
@@ -23,9 +19,8 @@ pub fn npm_pack(path: &str) -> Result<(), Error> {
23
19
24
20
/// Run the `npm publish` command.
25
21
pub fn npm_publish ( path : & str ) -> Result < ( ) , Error > {
26
- let pkg_file_path = format ! ( "{}/pkg" , path) ;
27
22
let output = Command :: new ( "npm" )
28
- . current_dir ( pkg_file_path )
23
+ . current_dir ( path )
29
24
. arg ( "publish" )
30
25
. output ( ) ?;
31
26
if !output. status . success ( ) {
You can’t perform that action at this time.
0 commit comments