Repository: ashleygwilliams/wasm-pack (rust)

Issue:
detect if cargo toml has wasm-bindgen dep
this issue is reserved for someone who has not yet contributed to wasm-pack

<hr/>

in order to compile to wasm32-unknown-unknown, the user must put the following in their `Cargo.toml`:

```
[dependencies]
wasm-bindgen = "0.2"
```

we have the ability to check this before we try to compile- to save the user some time and help remind them to do it!

we'll probably want to work on this in the `manifest.rs` file, and it might be similar to the `get_crate_name` function... [see here](https://github.com/ashleygwilliams/wasm-pack/blob/master/src/manifest.rs#L136)

Pull request description:
feat(wasm_bindgen): checks wasm-bindgen declaration in Cargo.toml
Fixes #141

I've used the same error that's about to be merged in #150 [[error.rs]](https://github.com/ashleygwilliams/wasm-pack/pull/150/files#diff-9a269209bed069ffb36d5565a91c9840R19) to avoid duplication or further refactoring.

I guess texts may have to be reviewed as I'm not a native English speaker 😉 

Any other suggestion to improve the code will be really appreciated.


Patch:
diff --git a/src/command.rs b/src/command.rs
index 21c552a..bcdcc99 100644
--- a/src/command.rs
+++ b/src/command.rs
@@ -176,6 +176,10 @@ fn init(
 
     let crate_path = set_crate_path(path);
 
+    info!(&log, "Checking wasm-bindgen dependency...");
+    manifest::check_wasm_bindgen(&crate_path)?;
+    info!(&log, "wasm-bindgen dependency is correctly declared.");
+
     info!(&log, "Adding wasm-target...");
     build::rustup_add_wasm_target()?;
     info!(&log, "Adding wasm-target was successful.");
diff --git a/src/manifest.rs b/src/manifest.rs
index 297e995..0b901ad 100644
--- a/src/manifest.rs
+++ b/src/manifest.rs
@@ -11,6 +11,7 @@ use PBAR;
 #[derive(Deserialize)]
 struct CargoManifest {
     package: CargoPackage,
+    dependencies: Option<CargoDependencies>,
     lib: Option<CargoLib>,
 }
 
@@ -24,6 +25,12 @@ struct CargoPackage {
     repository: Option<String>,
 }
 
+#[derive(Deserialize)]
+struct CargoDependencies {
+    #[serde(rename = "wasm-bindgen")]
+    wasm_bindgen: Option<String>,
+}
+
 #[derive(Deserialize)]
 struct CargoLib {
     #[serde(rename = "crate-type")]
@@ -144,6 +151,18 @@ pub fn get_crate_name(path: &str) -> Result<String, Error> {
     Ok(read_cargo_toml(path)?.package.name)
 }
 
+pub fn check_wasm_bindgen(path: &str) -> Result<(), Error> {
+    if read_cargo_toml(path)?.dependencies.map_or(false, |x| {
+        !x.wasm_bindgen.unwrap_or("".to_string()).is_empty()
+    }) {
+        return Ok(());
+    }
+    Error::crate_config(&format!(
+        "Ensure that you have \"{}\" as a dependency in your Cargo.toml file:\n[dependencies]\nwasm-bindgen = \"0.2\"",
+        style("wasm-bindgen").bold().dim()
+    ))
+}
+
 fn has_cdylib(path: &str) -> Result<bool, Error> {
     Ok(read_cargo_toml(path)?.lib.map_or(false, |lib| {
         lib.crate_type
@@ -159,4 +178,4 @@ pub fn check_crate_type(path: &str) -> Result<(), Error> {
     } else {
         Ok(())
     }
-}
+}
\ No newline at end of file


Test patch:
diff --git a/tests/fixtures/bad-cargo-toml/Cargo.toml b/tests/fixtures/bad-cargo-toml/Cargo.toml
index c73a430..1c8e304 100644
--- a/tests/fixtures/bad-cargo-toml/Cargo.toml
+++ b/tests/fixtures/bad-cargo-toml/Cargo.toml
@@ -7,4 +7,3 @@ authors = ["Michael Gattozzi <mgattozzi@gmail.com>"]
 crate-type = ["foo"]
 
 [dependencies]
-wasm-bindgen = "0.2"
diff --git a/tests/manifest/main.rs b/tests/manifest/main.rs
index dc2183c..cf15438 100644
--- a/tests/manifest/main.rs
+++ b/tests/manifest/main.rs
@@ -84,3 +84,13 @@ fn it_creates_a_package_json_provided_path_with_scope() {
     let pkg = utils::read_package_json(&path).unwrap();
     assert_eq!(pkg.name, "@test/scopes-hello-world");
 }
+
+#[test]
+fn it_errors_when_wasm_bindgen_is_not_declared() {
+    assert!(manifest::check_wasm_bindgen("tests/fixtures/bad-cargo-toml").is_err());
+}
+
+#[test]
+fn it_does_not_error_when_wasm_bindgen_is_declared() {
+    assert!(manifest::check_wasm_bindgen("tests/fixtures/js-hello-world").is_ok());
+}