From b1741bb98240a84d91bd426ac69ecf3e92b404e7 Mon Sep 17 00:00:00 2001 From: Andy Bell Date: Fri, 6 Apr 2018 10:20:34 +0100 Subject: [PATCH 1/4] feat(refactor): Move all functionality from main.rs to lib.rs to hopefully allow for better use of lazy_static as discussed in issue #25 --- src/lib.rs | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++- src/main.rs | 72 +++----------------------------------------- 2 files changed, 89 insertions(+), 69 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2330b02..0bbb1ea 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ extern crate failure; extern crate indicatif; #[macro_use] extern crate lazy_static; +extern crate quicli; #[macro_use] extern crate serde_derive; extern crate serde_json; @@ -16,17 +17,23 @@ pub mod npm; pub mod progressbar; pub mod readme; +use quicli::prelude::*; use std::fs; +use std::time::Instant; use console::style; use failure::Error; +use indicatif::HumanDuration; use progressbar::ProgressOutput; lazy_static! { pub static ref PBAR: ProgressOutput = { ProgressOutput::new() }; } -pub fn create_pkg_dir(path: &str) -> Result<(), Error> { +// quicli::prelude::* imports a different result struct which gets +// precedence over the std::result::Result, so have had to specify +// the correct type here. +pub fn create_pkg_dir(path: &str) -> std::result::Result<(), Error> { let step = format!( "{} {}Creating a pkg directory...", style("[3/7]").bold().dim(), @@ -38,3 +45,80 @@ pub fn create_pkg_dir(path: &str) -> Result<(), Error> { pb.finish(); Ok(()) } + +pub fn init_command(path: Option, scope: std::option::Option) -> std::result::Result<(), Error> { + let started = Instant::now(); + + let crate_path = match path { + Some(p) => p, + None => ".".to_string(), + }; + + build::rustup_add_wasm_target(); + build::cargo_build_wasm(&crate_path); + create_pkg_dir(&crate_path)?; + manifest::write_package_json(&crate_path, scope)?; + readme::copy_from_crate(&crate_path)?; + bindgen::cargo_install_wasm_bindgen(); + let name = manifest::get_crate_name(&crate_path)?; + bindgen::wasm_bindgen_build(&crate_path, &name); + PBAR.one_off_message(&format!( + "{} Done in {}", + emoji::SPARKLE, + HumanDuration(started.elapsed()) + )); + PBAR.one_off_message(&format!( + "{} Your WASM pkg is ready to publish at {}/pkg", + emoji::PACKAGE, + &crate_path + )); + PBAR.done()?; + Ok(()) +} + +pub fn pack_command(path: Option) -> std::result::Result<(),Error> { + let crate_path = match path { + Some(p) => p, + None => ".".to_string(), + }; + npm::npm_pack(&crate_path); + println!("🎒 packed up your package!"); + Ok(()) +} + +pub fn publish_command(path: Option) -> std::result::Result<(), Error> { + let crate_path = match path { + Some(p) => p, + None => ".".to_string(), + }; + npm::npm_publish(&crate_path); + println!("💥 published your package!"); + Ok(()) +} + +/// 📦 ✨ pack and publish your wasm! +#[derive(Debug, StructOpt)] +pub struct Cli { + #[structopt(subcommand)] // Note that we mark a field as a subcommand + pub cmd: Command, + /// log all the things + #[structopt(long = "verbose", short = "v", parse(from_occurrences))] + pub verbosity: u8, +} + +#[derive(Debug, StructOpt)] +pub enum Command { + #[structopt(name = "init")] + /// 🐣 initialize a package.json based on your cmpiled wasm + Init { + path: Option, + #[structopt(long = "scope", short = "s")] + scope: Option, + }, + #[structopt(name = "pack")] + /// 🍱 create a tar of your npm package but don't ublish! [NOT IMPLEMENTED] + Pack { path: Option }, + #[structopt(name = "publish")] + /// 🎆 pack up your npm package and publish! [NOT MPLEMENTED] + Publish { path: Option }, +} diff --git a/src/main.rs b/src/main.rs index bf56f92..07ba643 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,82 +4,18 @@ extern crate indicatif; #[macro_use] extern crate quicli; -use std::time::Instant; - -use indicatif::HumanDuration; use quicli::prelude::*; -use wasm_pack::{bindgen, build, emoji, manifest, npm, readme, PBAR}; - -/// 📦 ✨ pack and publish your wasm! -#[derive(Debug, StructOpt)] -struct Cli { - #[structopt(subcommand)] // Note that we mark a field as a subcommand - cmd: Command, - /// 📝 log all the things! - #[structopt(long = "verbose", short = "v", parse(from_occurrences))] - verbosity: u8, -} +use wasm_pack::{Cli, Command, init_command, pack_command, publish_command}; -#[derive(Debug, StructOpt)] -enum Command { - #[structopt(name = "init")] - /// 🐣 initialize a package.json based on your compiled wasm - Init { - path: Option, - #[structopt(long = "scope", short = "s")] - scope: Option, - }, - #[structopt(name = "pack")] - /// 🍱 create a tar of your npm package but don't publish! [NOT IMPLEMENTED] - Pack { path: Option }, - #[structopt(name = "publish")] - /// 🎆 pack up your npm package and publish! [NOT IMPLEMENTED] - Publish { path: Option }, -} main!(|args: Cli, log_level: verbosity| match args.cmd { Command::Init { path, scope } => { - let started = Instant::now(); - - let crate_path = match path { - Some(p) => p, - None => ".".to_string(), - }; - - build::rustup_add_wasm_target(); - build::cargo_build_wasm(&crate_path); - wasm_pack::create_pkg_dir(&crate_path)?; - manifest::write_package_json(&crate_path, scope)?; - readme::copy_from_crate(&crate_path)?; - bindgen::cargo_install_wasm_bindgen(); - let name = manifest::get_crate_name(&crate_path)?; - bindgen::wasm_bindgen_build(&crate_path, &name); - PBAR.one_off_message(&format!( - "{} Done in {}", - emoji::SPARKLE, - HumanDuration(started.elapsed()) - )); - PBAR.one_off_message(&format!( - "{} Your WASM pkg is ready to publish at {}/pkg", - emoji::PACKAGE, - &crate_path - )); - PBAR.done()?; + init_command(path, scope)?; } Command::Pack { path } => { - let crate_path = match path { - Some(p) => p, - None => ".".to_string(), - }; - npm::npm_pack(&crate_path); - println!("🎒 packed up your package!"); + pack_command(path)?; } Command::Publish { path } => { - let crate_path = match path { - Some(p) => p, - None => ".".to_string(), - }; - npm::npm_publish(&crate_path); - println!("💥 published your package!"); + publish_command(path)?; } }); From 07d9001b59a16a199d69a5e39ffdabab35dca6c1 Mon Sep 17 00:00:00 2001 From: Andy Bell Date: Fri, 6 Apr 2018 14:43:56 +0100 Subject: [PATCH 2/4] feat(refactor): Furthers previous refactor, by moving Functions to new module. Tidies up reused code into private function. Throws warning due to line 11 in command.rs - unused import Import is used for the StructOpt derive macro, so is needed but still throws this warning --- src/command.rs | 100 +++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 93 ++------------------------------------ src/main.rs | 3 +- tests/manifest/main.rs | 6 +-- 4 files changed, 108 insertions(+), 94 deletions(-) create mode 100644 src/command.rs diff --git a/src/command.rs b/src/command.rs new file mode 100644 index 0000000..f09bc33 --- /dev/null +++ b/src/command.rs @@ -0,0 +1,100 @@ +use bindgen; +use build; +use console::style; +use emoji; +use failure::Error; +use indicatif::HumanDuration; +use manifest; +use npm; +use readme; +use PBAR; +use quicli::prelude::*; +use std::fs; +use std::result; +use std::time::Instant; + +#[derive(Debug, StructOpt)] +pub enum Command { + #[structopt(name = "init")] + /// 🐣 initialize a package.json based on your cmpiled wasm + Init { + path: Option, + #[structopt(long = "scope", short = "s")] + scope: Option, + }, + #[structopt(name = "pack")] + /// 🍱 create a tar of your npm package but don't ublish! [NOT IMPLEMENTED] + Pack { path: Option }, + #[structopt(name = "publish")] + /// 🎆 pack up your npm package and publish! [NOT MPLEMENTED] + Publish { path: Option }, +} + +// quicli::prelude::* imports a different result struct which gets +// precedence over the std::result::Result, so have had to specify +// the correct type here. +pub fn create_pkg_dir(path: &str) -> result::Result<(), Error> { + let step = format!( + "{} {}Creating a pkg directory...", + style("[3/7]").bold().dim(), + emoji::FOLDER + ); + let pb = PBAR.message(&step); + let pkg_dir_path = format!("{}/pkg", path); + fs::create_dir_all(pkg_dir_path)?; + pb.finish(); + Ok(()) +} + +pub fn init_command(path: Option, scope: Option) -> result::Result<(), Error> { + let started = Instant::now(); + + let crate_path = set_crate_path(path); + + build::rustup_add_wasm_target(); + build::cargo_build_wasm(&crate_path); + create_pkg_dir(&crate_path)?; + manifest::write_package_json(&crate_path, scope)?; + readme::copy_from_crate(&crate_path)?; + bindgen::cargo_install_wasm_bindgen(); + let name = manifest::get_crate_name(&crate_path)?; + bindgen::wasm_bindgen_build(&crate_path, &name); + PBAR.one_off_message(&format!( + "{} Done in {}", + emoji::SPARKLE, + HumanDuration(started.elapsed()) + )); + PBAR.one_off_message(&format!( + "{} Your WASM pkg is ready to publish at {}/pkg", + emoji::PACKAGE, + &crate_path + )); + PBAR.done()?; + Ok(()) +} + +pub fn pack_command(path: Option) -> result::Result<(),Error> { + let crate_path = set_crate_path(path); + + npm::npm_pack(&crate_path); + println!("🎒 packed up your package!"); + Ok(()) +} + +pub fn publish_command(path: Option) -> result::Result<(), Error> { + let crate_path = set_crate_path(path); + + npm::npm_publish(&crate_path); + println!("💥 published your package!"); + Ok(()) +} + +fn set_crate_path(path: Option) -> String { + let crate_path = match path { + Some(p) => p, + None => ".".to_string(), + }; + + crate_path + +} diff --git a/src/lib.rs b/src/lib.rs index 0bbb1ea..4827f3f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,114 +11,27 @@ extern crate toml; pub mod bindgen; pub mod build; +pub mod command; pub mod emoji; pub mod manifest; pub mod npm; pub mod progressbar; pub mod readme; -use quicli::prelude::*; -use std::fs; -use std::time::Instant; - -use console::style; -use failure::Error; -use indicatif::HumanDuration; use progressbar::ProgressOutput; +use quicli::prelude::*; lazy_static! { pub static ref PBAR: ProgressOutput = { ProgressOutput::new() }; } -// quicli::prelude::* imports a different result struct which gets -// precedence over the std::result::Result, so have had to specify -// the correct type here. -pub fn create_pkg_dir(path: &str) -> std::result::Result<(), Error> { - let step = format!( - "{} {}Creating a pkg directory...", - style("[3/7]").bold().dim(), - emoji::FOLDER - ); - let pb = PBAR.message(&step); - let pkg_dir_path = format!("{}/pkg", path); - fs::create_dir_all(pkg_dir_path)?; - pb.finish(); - Ok(()) -} - -pub fn init_command(path: Option, scope: std::option::Option) -> std::result::Result<(), Error> { - let started = Instant::now(); - - let crate_path = match path { - Some(p) => p, - None => ".".to_string(), - }; - - build::rustup_add_wasm_target(); - build::cargo_build_wasm(&crate_path); - create_pkg_dir(&crate_path)?; - manifest::write_package_json(&crate_path, scope)?; - readme::copy_from_crate(&crate_path)?; - bindgen::cargo_install_wasm_bindgen(); - let name = manifest::get_crate_name(&crate_path)?; - bindgen::wasm_bindgen_build(&crate_path, &name); - PBAR.one_off_message(&format!( - "{} Done in {}", - emoji::SPARKLE, - HumanDuration(started.elapsed()) - )); - PBAR.one_off_message(&format!( - "{} Your WASM pkg is ready to publish at {}/pkg", - emoji::PACKAGE, - &crate_path - )); - PBAR.done()?; - Ok(()) -} - -pub fn pack_command(path: Option) -> std::result::Result<(),Error> { - let crate_path = match path { - Some(p) => p, - None => ".".to_string(), - }; - npm::npm_pack(&crate_path); - println!("🎒 packed up your package!"); - Ok(()) -} - -pub fn publish_command(path: Option) -> std::result::Result<(), Error> { - let crate_path = match path { - Some(p) => p, - None => ".".to_string(), - }; - npm::npm_publish(&crate_path); - println!("💥 published your package!"); - Ok(()) -} - /// 📦 ✨ pack and publish your wasm! #[derive(Debug, StructOpt)] pub struct Cli { #[structopt(subcommand)] // Note that we mark a field as a subcommand - pub cmd: Command, + pub cmd: command::Command, /// log all the things #[structopt(long = "verbose", short = "v", parse(from_occurrences))] pub verbosity: u8, } -#[derive(Debug, StructOpt)] -pub enum Command { - #[structopt(name = "init")] - /// 🐣 initialize a package.json based on your cmpiled wasm - Init { - path: Option, - #[structopt(long = "scope", short = "s")] - scope: Option, - }, - #[structopt(name = "pack")] - /// 🍱 create a tar of your npm package but don't ublish! [NOT IMPLEMENTED] - Pack { path: Option }, - #[structopt(name = "publish")] - /// 🎆 pack up your npm package and publish! [NOT MPLEMENTED] - Publish { path: Option }, -} diff --git a/src/main.rs b/src/main.rs index 07ba643..17610fc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,8 @@ extern crate indicatif; extern crate quicli; use quicli::prelude::*; -use wasm_pack::{Cli, Command, init_command, pack_command, publish_command}; +use wasm_pack::Cli; +use wasm_pack::command::{Command, init_command, pack_command, publish_command}; main!(|args: Cli, log_level: verbosity| match args.cmd { diff --git a/tests/manifest/main.rs b/tests/manifest/main.rs index 22a2f16..2ef3a6a 100644 --- a/tests/manifest/main.rs +++ b/tests/manifest/main.rs @@ -28,7 +28,7 @@ fn it_gets_the_crate_name_provided_path() { #[test] fn it_creates_a_package_json_default_path() { let path = ".".to_string(); - wasm_pack::create_pkg_dir(&path).unwrap(); + wasm_pack::command::create_pkg_dir(&path).unwrap(); assert!(manifest::write_package_json(&path, None).is_ok()); let package_json_path = format!("{}/pkg/package.json", &path); assert!(fs::metadata(package_json_path).is_ok()); @@ -46,7 +46,7 @@ fn it_creates_a_package_json_default_path() { #[test] fn it_creates_a_package_json_provided_path() { let path = "tests/fixtures/js-hello-world".to_string(); - wasm_pack::create_pkg_dir(&path).unwrap(); + wasm_pack::command::create_pkg_dir(&path).unwrap(); assert!(manifest::write_package_json(&path, None).is_ok()); let package_json_path = format!("{}/pkg/package.json", &path); assert!(fs::metadata(package_json_path).is_ok()); @@ -59,7 +59,7 @@ fn it_creates_a_package_json_provided_path() { #[test] fn it_creates_a_package_json_provided_path_with_scope() { let path = "tests/fixtures/scopes".to_string(); - wasm_pack::create_pkg_dir(&path).unwrap(); + wasm_pack::command::create_pkg_dir(&path).unwrap(); assert!(manifest::write_package_json(&path, Some("test".to_string())).is_ok()); let package_json_path = format!("{}/pkg/package.json", &path); assert!(fs::metadata(package_json_path).is_ok()); From 542dd3312f55275d2772d18b1e6d078355660e6d Mon Sep 17 00:00:00 2001 From: andy-bell Date: Sat, 7 Apr 2018 13:15:52 +0100 Subject: [PATCH 3/4] edits to appease cargo fmt --- src/command.rs | 19 +++++++++---------- src/lib.rs | 1 - src/main.rs | 2 +- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/command.rs b/src/command.rs index f09bc33..0462520 100644 --- a/src/command.rs +++ b/src/command.rs @@ -1,3 +1,4 @@ +use PBAR; use bindgen; use build; use console::style; @@ -6,9 +7,8 @@ use failure::Error; use indicatif::HumanDuration; use manifest; use npm; -use readme; -use PBAR; use quicli::prelude::*; +use readme; use std::fs; use std::result; use std::time::Instant; @@ -60,20 +60,20 @@ pub fn init_command(path: Option, scope: Option) -> result::Resu let name = manifest::get_crate_name(&crate_path)?; bindgen::wasm_bindgen_build(&crate_path, &name); PBAR.one_off_message(&format!( - "{} Done in {}", - emoji::SPARKLE, - HumanDuration(started.elapsed()) + "{} Done in {}", + emoji::SPARKLE, + HumanDuration(started.elapsed()) )); PBAR.one_off_message(&format!( - "{} Your WASM pkg is ready to publish at {}/pkg", - emoji::PACKAGE, - &crate_path + "{} Your WASM pkg is ready to publish at {}/pkg", + emoji::PACKAGE, + &crate_path )); PBAR.done()?; Ok(()) } -pub fn pack_command(path: Option) -> result::Result<(),Error> { +pub fn pack_command(path: Option) -> result::Result<(), Error> { let crate_path = set_crate_path(path); npm::npm_pack(&crate_path); @@ -96,5 +96,4 @@ fn set_crate_path(path: Option) -> String { }; crate_path - } diff --git a/src/lib.rs b/src/lib.rs index 4827f3f..46009a9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -34,4 +34,3 @@ pub struct Cli { #[structopt(long = "verbose", short = "v", parse(from_occurrences))] pub verbosity: u8, } - diff --git a/src/main.rs b/src/main.rs index 17610fc..0c26474 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ extern crate quicli; use quicli::prelude::*; use wasm_pack::Cli; -use wasm_pack::command::{Command, init_command, pack_command, publish_command}; +use wasm_pack::command::{init_command, pack_command, publish_command, Command}; main!(|args: Cli, log_level: verbosity| match args.cmd { From 2a996cb5545675da507546d97c8ab38cb0c0dd12 Mon Sep 17 00:00:00 2001 From: andy-bell Date: Sat, 7 Apr 2018 13:22:40 +0100 Subject: [PATCH 4/4] feat(refactor): missed one warning --- src/main.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 0c26474..afc7355 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,6 @@ use quicli::prelude::*; use wasm_pack::Cli; use wasm_pack::command::{init_command, pack_command, publish_command, Command}; - main!(|args: Cli, log_level: verbosity| match args.cmd { Command::Init { path, scope } => { init_command(path, scope)?;