Skip to content

feat(wasm_bindgen): checks wasm-bindgen declaration in Cargo.toml #162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down
21 changes: 20 additions & 1 deletion src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use PBAR;
#[derive(Deserialize)]
struct CargoManifest {
package: CargoPackage,
dependencies: Option<CargoDependencies>,
lib: Option<CargoLib>,
}

Expand All @@ -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")]
Expand Down Expand Up @@ -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
Expand All @@ -159,4 +178,4 @@ pub fn check_crate_type(path: &str) -> Result<(), Error> {
} else {
Ok(())
}
}
}
1 change: 0 additions & 1 deletion tests/fixtures/bad-cargo-toml/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ authors = ["Michael Gattozzi <[email protected]>"]
crate-type = ["foo"]

[dependencies]
wasm-bindgen = "0.2"
10 changes: 10 additions & 0 deletions tests/manifest/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}