From be97683fd28bbdd29c3670d6c8cfc7ac3e064b1f Mon Sep 17 00:00:00 2001 From: Ashley Williams Date: Fri, 2 Mar 2018 12:58:28 -0500 Subject: [PATCH 1/2] feat(example): add js-hello-world example --- examples/js-hello-world/Cargo.toml | 13 +++++++++++++ examples/js-hello-world/src/lib.rs | 16 ++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 examples/js-hello-world/Cargo.toml create mode 100644 examples/js-hello-world/src/lib.rs diff --git a/examples/js-hello-world/Cargo.toml b/examples/js-hello-world/Cargo.toml new file mode 100644 index 0000000..06b256c --- /dev/null +++ b/examples/js-hello-world/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "js-hello-world" +description = "an example rust->wasm crate" +version = "0.1.0" +authors = ["Ashley Williams "] +license = "WTFPL" +repository = "https://github.com/ashleygwilliams/wasm-pack" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +wasm-bindgen = { git = 'https://github.com/alexcrichton/wasm-bindgen' } diff --git a/examples/js-hello-world/src/lib.rs b/examples/js-hello-world/src/lib.rs new file mode 100644 index 0000000..0533cdc --- /dev/null +++ b/examples/js-hello-world/src/lib.rs @@ -0,0 +1,16 @@ +#![feature(proc_macro)] + +extern crate wasm_bindgen; + +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +extern { + fn alert(s: &str); +} + +#[wasm_bindgen] +#[no_mangle] +pub extern fn greet(name: &str) { + alert(&format!("Hello, {}!", name)); +} From 83e44c19b8cd6474cf569ae6a0e724065792d2ca Mon Sep 17 00:00:00 2001 From: Ashley Williams Date: Fri, 2 Mar 2018 17:37:41 -0500 Subject: [PATCH 2/2] feat(args): take a path to a crate as an arg to init --- src/lib.rs | 8 ++++---- src/main.rs | 9 ++++++--- tests/wasm-pack.rs | 10 ++++++++-- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 884f24f..bcdcf30 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,8 +39,8 @@ struct Repository { url: String, } -fn read_cargo_toml() -> Result { - let mut cargo_file = File::open("Cargo.toml")?; +fn read_cargo_toml(path: &str) -> Result { + let mut cargo_file = File::open(format!("{}/Cargo.toml", path))?; let mut cargo_contents = String::new(); cargo_file.read_to_string(&mut cargo_contents)?; @@ -68,10 +68,10 @@ fn create_pkg_dir() -> Result<(), Error> { } /// Generate a package.json file inside in `./pkg`. -pub fn write_package_json() -> Result<(), Error> { +pub fn write_package_json(path: &str) -> Result<(), Error> { create_pkg_dir()?; let mut pkg_file = File::create("./pkg/package.json")?; - let crate_data = read_cargo_toml()?; + let crate_data = read_cargo_toml(path)?; let npm_data = crate_data.into_npm(); let npm_json = serde_json::to_string(&npm_data)?; pkg_file.write_all(npm_json.as_bytes())?; diff --git a/src/main.rs b/src/main.rs index ae8db5f..08fd3f8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,7 +18,7 @@ struct Cli { enum Command { #[structopt(name = "init")] /// 🐣 initialize a package.json based on your compiled wasm - Init {}, + Init { path: Option }, #[structopt(name = "pack")] /// 🍱 create a tar of your npm package but don't publish! Pack {}, @@ -28,8 +28,11 @@ enum Command { } main!(|args: Cli, log_level: verbosity| match args.cmd { - Command::Init { .. } => { - wasm_pack::write_package_json()?; + Command::Init { path } => { + match path { + Some(p) => wasm_pack::write_package_json(&p)?, + None => wasm_pack::write_package_json(".")?, + } println!("✍️ wrote a package.json!"); } Command::Pack { .. } => { diff --git a/tests/wasm-pack.rs b/tests/wasm-pack.rs index 5f9120d..c2bcaf1 100644 --- a/tests/wasm-pack.rs +++ b/tests/wasm-pack.rs @@ -3,7 +3,13 @@ extern crate wasm_pack; use std::fs; #[test] -fn it_creates_a_package_json() { - assert!(wasm_pack::write_package_json().is_ok()); +fn it_creates_a_package_json_default_path() { + assert!(wasm_pack::write_package_json(".").is_ok()); + assert!(fs::metadata("./pkg/package.json").is_ok()); +} + +#[test] +fn it_creates_a_package_json_provided_path() { + assert!(wasm_pack::write_package_json("./examples/js-hello-world").is_ok()); assert!(fs::metadata("./pkg/package.json").is_ok()); }