From 27d6919eea3be29901b0e49feedcba0a47193022 Mon Sep 17 00:00:00 2001 From: Jamie Kyle Date: Tue, 27 Mar 2018 13:45:40 -0700 Subject: [PATCH] init pack and publish --- README.md | 17 +++++++++++------ src/bindgen.rs | 2 +- src/build.rs | 2 +- src/lib.rs | 9 +++++---- src/main.rs | 26 +++++++++++++++++--------- src/manifest.rs | 4 ++-- src/npm.rs | 32 ++++++++++++++++++++++++++++++++ src/readme.rs | 6 +++--- 8 files changed, 72 insertions(+), 26 deletions(-) create mode 100644 src/npm.rs diff --git a/README.md b/README.md index 2780d29..7b4d845 100644 --- a/README.md +++ b/README.md @@ -20,9 +20,16 @@ this project is written in rust. [get rust] to work on this project. [get rust]: https://www.rustup.rs/ +if you want to publish packages, you'll also need an account on [npm] and have +[node/npm] installed. + +[npm]: https://www.npmjs.com +[node/npm]: https://nodejs.org/ + ## 🏃‍♀️ up and running 1. fork and clone this repository +2. install [node/npm] 2. `cd wasm-pack` 3. `cargo run` @@ -39,10 +46,8 @@ this project is written in rust. [get rust] to work on this project. wasm-pack init examples/scopes-hello-world --scope test ``` generates a `package.json` for an npm package called `@test/scopes-hello-world` - -#### to be implemented -- 🍱 `pack`: create a tarball but don't push to the npm registry [NOT IMPLEMENTED] -- 🎆 `publish`: create a tarball and publish to the npm registry [NOT IMPLEMENTED] +- 🍱 `pack`: create a tarball but don't push to the npm registry (see https://docs.npmjs.com/cli/pack) +- 🎆 `publish`: create a tarball and publish to the npm registry (see https://docs.npmjs.com/cli/publish) ## ⚙️ how to use @@ -82,8 +87,8 @@ this project is written in rust. [get rust] to work on this project. 5. install this tool: `cargo install wasm-pack` 6. run `wasm-pack init`, optionally, pass a path to a dir or a scope (see above for details) -7. this tool generates files in a `pkg` dir. to publish to npm, `cd pkg` and then `npm publish` - (in the future you'll be able to use this tool to publish) +7. this tool generates files in a `pkg` dir +8. to publish to npm, run `wasm-pack publish` (making sure you are logged in with npm) [rust-wasm/36]: https://github.com/rust-lang-nursery/rust-wasm/issues/36 [wasm-bindgen]: https://github.com/alexcrichton/wasm-bindgen diff --git a/src/bindgen.rs b/src/bindgen.rs index c41f4ee..3def230 100644 --- a/src/bindgen.rs +++ b/src/bindgen.rs @@ -1,7 +1,7 @@ -use std::process::Command; use console::style; use emoji; use progressbar; +use std::process::Command; pub fn cargo_install_wasm_bindgen() { let step = format!( diff --git a/src/build.rs b/src/build.rs index 527a8c7..f3ed8a5 100644 --- a/src/build.rs +++ b/src/build.rs @@ -1,7 +1,7 @@ -use std::process::Command; use console::style; use emoji; use progressbar; +use std::process::Command; pub fn rustup_add_wasm_target() { let step = format!( diff --git a/src/lib.rs b/src/lib.rs index 05b78ba..c7d45c9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,17 +6,18 @@ extern crate serde_derive; extern crate serde_json; extern crate toml; -pub mod build; pub mod bindgen; -pub mod readme; +pub mod build; +pub mod emoji; pub mod manifest; +pub mod npm; pub mod progressbar; -pub mod emoji; +pub mod readme; use std::fs; -use failure::Error; use console::style; +use failure::Error; pub fn create_pkg_dir(path: &str) -> Result<(), Error> { let step = format!( diff --git a/src/main.rs b/src/main.rs index 9afeabe..13c2ebe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,7 @@ use std::time::Instant; use indicatif::HumanDuration; use quicli::prelude::*; -use wasm_pack::{bindgen, build, emoji, manifest, readme}; +use wasm_pack::{bindgen, build, emoji, manifest, npm, readme}; /// 📦 ✨ pack and publish your wasm! #[derive(Debug, StructOpt)] @@ -31,10 +31,10 @@ enum Command { }, #[structopt(name = "pack")] /// 🍱 create a tar of your npm package but don't publish! [NOT IMPLEMENTED] - Pack {}, + Pack { path: Option }, #[structopt(name = "publish")] /// 🎆 pack up your npm package and publish! [NOT IMPLEMENTED] - Publish {}, + Publish { path: Option }, } main!(|args: Cli, log_level: verbosity| match args.cmd { @@ -65,12 +65,20 @@ main!(|args: Cli, log_level: verbosity| match args.cmd { &crate_path ) } - Command::Pack { .. } => { - println!("🙅‍♀️ whoops! this is not implemented yet! sorry!"); - //println!("🎒 packed up your package!"); + Command::Pack { path } => { + let crate_path = match path { + Some(p) => p, + None => ".".to_string(), + }; + npm::npm_pack(&crate_path); + println!("🎒 packed up your package!"); } - Command::Publish { .. } => { - println!("🙅‍♀️ whoops! this is not implemented yet! sorry!"); - //println!("💥 published your package!"); + Command::Publish { path } => { + let crate_path = match path { + Some(p) => p, + None => ".".to_string(), + }; + npm::npm_publish(&crate_path); + println!("💥 published your package!"); } }); diff --git a/src/manifest.rs b/src/manifest.rs index da5308d..05936c1 100644 --- a/src/manifest.rs +++ b/src/manifest.rs @@ -2,11 +2,11 @@ use std::fs::File; use std::io::prelude::*; use console::style; +use emoji; use failure::Error; +use progressbar; use serde_json; use toml; -use emoji; -use progressbar; #[derive(Deserialize)] struct CargoManifest { diff --git a/src/npm.rs b/src/npm.rs new file mode 100644 index 0000000..75660c2 --- /dev/null +++ b/src/npm.rs @@ -0,0 +1,32 @@ +use emoji; +use std::process::Command; + +pub fn npm_pack(path: &str) { + let pkg_file_path = format!("{}/pkg", path); + let output = Command::new("npm") + .current_dir(pkg_file_path) + .arg("pack") + .output() + .unwrap_or_else(|e| panic!("{} failed to execute process: {}", emoji::ERROR, e)); + if !output.status.success() { + let s = String::from_utf8_lossy(&output.stderr); + print!("{} npm_pack failed and stderr was:\n{}", emoji::ERROR, s); + } +} + +pub fn npm_publish(path: &str) { + let pkg_file_path = format!("{}/pkg", path); + let output = Command::new("npm") + .current_dir(pkg_file_path) + .arg("publish") + .output() + .unwrap_or_else(|e| panic!("{} failed to execute process: {}", emoji::ERROR, e)); + if !output.status.success() { + let s = String::from_utf8_lossy(&output.stderr); + print!( + "{} npm_publish failed and stderr was:\n{}", + emoji::ERROR, + s + ); + } +} diff --git a/src/readme.rs b/src/readme.rs index 20f5c45..6bde0dd 100644 --- a/src/readme.rs +++ b/src/readme.rs @@ -1,10 +1,10 @@ -use std::fs; -use failure::Error; use console::style; +use failure::Error; use indicatif::MultiProgress; +use std::fs; -use progressbar; use emoji; +use progressbar; pub fn copy_from_crate(path: &str) -> Result<(), Error> { let m = MultiProgress::new();