Merge pull request #78 from Andy-Bell/refactor-main-rs

Refactor main.rs
master
ashley williams 7 years ago committed by GitHub
commit 0f0127255b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 99
      src/command.rs
  2. 26
      src/lib.rs
  3. 74
      src/main.rs
  4. 6
      tests/manifest/main.rs

@ -0,0 +1,99 @@
use PBAR;
use bindgen;
use build;
use console::style;
use emoji;
use failure::Error;
use indicatif::HumanDuration;
use manifest;
use npm;
use quicli::prelude::*;
use readme;
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<String>,
#[structopt(long = "scope", short = "s")]
scope: Option<String>,
},
#[structopt(name = "pack")]
/// 🍱 create a tar of your npm package but don't ublish! [NOT IMPLEMENTED]
Pack { path: Option<String> },
#[structopt(name = "publish")]
/// 🎆 pack up your npm package and publish! [NOT MPLEMENTED]
Publish { path: Option<String> },
}
// 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<String>, scope: Option<String>) -> 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<String>) -> 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<String>) -> 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>) -> String {
let crate_path = match path {
Some(p) => p,
None => ".".to_string(),
};
crate_path
}

@ -3,6 +3,7 @@ extern crate failure;
extern crate indicatif; extern crate indicatif;
#[macro_use] #[macro_use]
extern crate lazy_static; extern crate lazy_static;
extern crate quicli;
#[macro_use] #[macro_use]
extern crate serde_derive; extern crate serde_derive;
extern crate serde_json; extern crate serde_json;
@ -10,31 +11,26 @@ extern crate toml;
pub mod bindgen; pub mod bindgen;
pub mod build; pub mod build;
pub mod command;
pub mod emoji; pub mod emoji;
pub mod manifest; pub mod manifest;
pub mod npm; pub mod npm;
pub mod progressbar; pub mod progressbar;
pub mod readme; pub mod readme;
use std::fs;
use console::style;
use failure::Error;
use progressbar::ProgressOutput; use progressbar::ProgressOutput;
use quicli::prelude::*;
lazy_static! { lazy_static! {
pub static ref PBAR: ProgressOutput = { ProgressOutput::new() }; pub static ref PBAR: ProgressOutput = { ProgressOutput::new() };
} }
pub fn create_pkg_dir(path: &str) -> Result<(), Error> { /// 📦 ✨ pack and publish your wasm!
let step = format!( #[derive(Debug, StructOpt)]
"{} {}Creating a pkg directory...", pub struct Cli {
style("[3/7]").bold().dim(), #[structopt(subcommand)] // Note that we mark a field as a subcommand
emoji::FOLDER pub cmd: command::Command,
); /// log all the things
let pb = PBAR.message(&step); #[structopt(long = "verbose", short = "v", parse(from_occurrences))]
let pkg_dir_path = format!("{}/pkg", path); pub verbosity: u8,
fs::create_dir_all(pkg_dir_path)?;
pb.finish();
Ok(())
} }

@ -4,82 +4,18 @@ extern crate indicatif;
#[macro_use] #[macro_use]
extern crate quicli; extern crate quicli;
use std::time::Instant;
use indicatif::HumanDuration;
use quicli::prelude::*; use quicli::prelude::*;
use wasm_pack::{bindgen, build, emoji, manifest, npm, readme, PBAR}; use wasm_pack::Cli;
use wasm_pack::command::{init_command, pack_command, publish_command, Command};
/// 📦 ✨ 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,
}
#[derive(Debug, StructOpt)]
enum Command {
#[structopt(name = "init")]
/// 🐣 initialize a package.json based on your compiled wasm
Init {
path: Option<String>,
#[structopt(long = "scope", short = "s")]
scope: Option<String>,
},
#[structopt(name = "pack")]
/// 🍱 create a tar of your npm package but don't publish! [NOT IMPLEMENTED]
Pack { path: Option<String> },
#[structopt(name = "publish")]
/// 🎆 pack up your npm package and publish! [NOT IMPLEMENTED]
Publish { path: Option<String> },
}
main!(|args: Cli, log_level: verbosity| match args.cmd { main!(|args: Cli, log_level: verbosity| match args.cmd {
Command::Init { path, scope } => { Command::Init { path, scope } => {
let started = Instant::now(); init_command(path, scope)?;
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()?;
} }
Command::Pack { path } => { Command::Pack { path } => {
let crate_path = match path { pack_command(path)?;
Some(p) => p,
None => ".".to_string(),
};
npm::npm_pack(&crate_path);
println!("🎒 packed up your package!");
} }
Command::Publish { path } => { Command::Publish { path } => {
let crate_path = match path { publish_command(path)?;
Some(p) => p,
None => ".".to_string(),
};
npm::npm_publish(&crate_path);
println!("💥 published your package!");
} }
}); });

@ -28,7 +28,7 @@ fn it_gets_the_crate_name_provided_path() {
#[test] #[test]
fn it_creates_a_package_json_default_path() { fn it_creates_a_package_json_default_path() {
let path = ".".to_string(); 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()); assert!(manifest::write_package_json(&path, None).is_ok());
let package_json_path = format!("{}/pkg/package.json", &path); let package_json_path = format!("{}/pkg/package.json", &path);
assert!(fs::metadata(package_json_path).is_ok()); assert!(fs::metadata(package_json_path).is_ok());
@ -46,7 +46,7 @@ fn it_creates_a_package_json_default_path() {
#[test] #[test]
fn it_creates_a_package_json_provided_path() { fn it_creates_a_package_json_provided_path() {
let path = "tests/fixtures/js-hello-world".to_string(); 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()); assert!(manifest::write_package_json(&path, None).is_ok());
let package_json_path = format!("{}/pkg/package.json", &path); let package_json_path = format!("{}/pkg/package.json", &path);
assert!(fs::metadata(package_json_path).is_ok()); assert!(fs::metadata(package_json_path).is_ok());
@ -59,7 +59,7 @@ fn it_creates_a_package_json_provided_path() {
#[test] #[test]
fn it_creates_a_package_json_provided_path_with_scope() { fn it_creates_a_package_json_provided_path_with_scope() {
let path = "tests/fixtures/scopes".to_string(); 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()); assert!(manifest::write_package_json(&path, Some("test".to_string())).is_ok());
let package_json_path = format!("{}/pkg/package.json", &path); let package_json_path = format!("{}/pkg/package.json", &path);
assert!(fs::metadata(package_json_path).is_ok()); assert!(fs::metadata(package_json_path).is_ok());

Loading…
Cancel
Save