Skip to content

Commit 06160a1

Browse files
Merge pull request #167 from ashleygwilliams/robertohuertasm-feature-wasm-bindgen-detection
Robertohuertasm feature wasm bindgen detection
2 parents 11908d7 + 2100c15 commit 06160a1

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

src/command.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ fn init(
176176

177177
let crate_path = set_crate_path(path);
178178

179+
info!(&log, "Checking wasm-bindgen dependency...");
180+
manifest::check_wasm_bindgen(&crate_path)?;
181+
info!(&log, "wasm-bindgen dependency is correctly declared.");
182+
179183
info!(&log, "Adding wasm-target...");
180184
build::rustup_add_wasm_target()?;
181185
info!(&log, "Adding wasm-target was successful.");

src/manifest.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use PBAR;
1111
#[derive(Deserialize)]
1212
struct CargoManifest {
1313
package: CargoPackage,
14+
dependencies: Option<CargoDependencies>,
1415
lib: Option<CargoLib>,
1516
}
1617

@@ -24,6 +25,12 @@ struct CargoPackage {
2425
repository: Option<String>,
2526
}
2627

28+
#[derive(Deserialize)]
29+
struct CargoDependencies {
30+
#[serde(rename = "wasm-bindgen")]
31+
wasm_bindgen: Option<String>,
32+
}
33+
2734
#[derive(Deserialize)]
2835
struct CargoLib {
2936
#[serde(rename = "crate-type")]
@@ -144,6 +151,18 @@ pub fn get_crate_name(path: &str) -> Result<String, Error> {
144151
Ok(read_cargo_toml(path)?.package.name)
145152
}
146153

154+
pub fn check_wasm_bindgen(path: &str) -> Result<(), Error> {
155+
if read_cargo_toml(path)?.dependencies.map_or(false, |x| {
156+
!x.wasm_bindgen.unwrap_or("".to_string()).is_empty()
157+
}) {
158+
return Ok(());
159+
}
160+
Error::crate_config(&format!(
161+
"Ensure that you have \"{}\" as a dependency in your Cargo.toml file:\n[dependencies]\nwasm-bindgen = \"0.2\"",
162+
style("wasm-bindgen").bold().dim()
163+
))
164+
}
165+
147166
fn has_cdylib(path: &str) -> Result<bool, Error> {
148167
Ok(read_cargo_toml(path)?.lib.map_or(false, |lib| {
149168
lib.crate_type

tests/fixtures/bad-cargo-toml/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@ authors = ["Michael Gattozzi <[email protected]>"]
77
crate-type = ["foo"]
88

99
[dependencies]
10-
wasm-bindgen = "0.2"

tests/manifest/main.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,13 @@ fn it_creates_a_package_json_provided_path_with_scope() {
8484
let pkg = utils::read_package_json(&path).unwrap();
8585
assert_eq!(pkg.name, "@test/scopes-hello-world");
8686
}
87+
88+
#[test]
89+
fn it_errors_when_wasm_bindgen_is_not_declared() {
90+
assert!(manifest::check_wasm_bindgen("tests/fixtures/bad-cargo-toml").is_err());
91+
}
92+
93+
#[test]
94+
fn it_does_not_error_when_wasm_bindgen_is_declared() {
95+
assert!(manifest::check_wasm_bindgen("tests/fixtures/js-hello-world").is_ok());
96+
}

0 commit comments

Comments
 (0)