Skip to content
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

Added keywords (#707) #838

Merged
merged 1 commit into from
May 4, 2020
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
14 changes: 13 additions & 1 deletion src/manifest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,8 @@ struct NpmData {
files: Vec<String>,
dts_file: Option<String>,
main: String,
homepage: Option<String>, // https://docs.npmjs.com/files/package.json#homepage
homepage: Option<String>, // https://docs.npmjs.com/files/package.json#homepage,
keywords: Option<Vec<String>>, // https://docs.npmjs.com/files/package.json#keywords
}

#[doc(hidden)]
Expand Down Expand Up @@ -607,6 +608,12 @@ impl CrateData {
None
};

let keywords = if pkg.keywords.len() > 0 {
Some(pkg.keywords.clone())
} else {
None
};

if let Ok(entries) = fs::read_dir(out_dir) {
let file_names = entries
.filter_map(|e| e.ok())
Expand All @@ -625,6 +632,7 @@ impl CrateData {
files,
main: js_file,
homepage: self.manifest.package.homepage.clone(),
keywords: keywords,
}
}

Expand Down Expand Up @@ -662,6 +670,7 @@ impl CrateData {
main: data.main,
homepage: data.homepage,
types: data.dts_file,
keywords: data.keywords,
})
}

Expand Down Expand Up @@ -696,6 +705,7 @@ impl CrateData {
homepage: data.homepage,
types: data.dts_file,
side_effects: false,
keywords: data.keywords,
})
}

Expand Down Expand Up @@ -725,6 +735,7 @@ impl CrateData {
homepage: data.homepage,
types: data.dts_file,
side_effects: false,
keywords: data.keywords,
})
}

Expand Down Expand Up @@ -758,6 +769,7 @@ impl CrateData {
browser: data.main,
homepage: data.homepage,
types: data.dts_file,
keywords: data.keywords,
})
}

Expand Down
2 changes: 2 additions & 0 deletions src/manifest/npm/commonjs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ pub struct CommonJSPackage {
pub homepage: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub types: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub keywords: Option<Vec<String>>,
}
2 changes: 2 additions & 0 deletions src/manifest/npm/esmodules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ pub struct ESModulesPackage {
pub types: Option<String>,
#[serde(rename = "sideEffects")]
pub side_effects: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub keywords: Option<Vec<String>>,
}
2 changes: 2 additions & 0 deletions src/manifest/npm/nomodules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ pub struct NoModulesPackage {
pub homepage: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub types: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub keywords: Option<Vec<String>>,
}
57 changes: 57 additions & 0 deletions tests/all/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,63 @@ fn it_sets_homepage_field_if_available_in_cargo_toml() {
assert_eq!(pkg.homepage, None);
}

#[test]
fn it_sets_keywords_field_if_available_in_cargo_toml() {
// When 'homepage' is available
let fixture = utils::fixture::Fixture::new();
fixture.hello_world_src_lib().file(
"Cargo.toml",
r#"
[package]
authors = ["The wasm-pack developers"]
description = "so awesome rust+wasm package"
license = "WTFPL"
name = "homepage-field-test"
repository = "https://github.com/rustwasm/wasm-pack.git"
version = "0.1.0"
keywords = ["wasm"]

[lib]
crate-type = ["cdylib"]

[dependencies]
wasm-bindgen = "=0.2"

[dev-dependencies]
wasm-bindgen-test = "=0.2"
"#,
);

let out_dir = fixture.path.join("pkg");
let crate_data = manifest::CrateData::new(&fixture.path, None).unwrap();

wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap();
crate_data
.write_package_json(&out_dir, &None, true, Target::Bundler)
.unwrap();

let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
let keywords = pkg.keywords.clone().unwrap();
assert!(
keywords.contains(&"wasm".to_string()),
"keywords is not in files: {:?}",
keywords,
);

// When 'keywords' is unavailable
let fixture = fixture::js_hello_world();
let out_dir = fixture.path.join("pkg");
let crate_data = manifest::CrateData::new(&fixture.path, None).unwrap();

wasm_pack::command::utils::create_pkg_dir(&out_dir).unwrap();
crate_data
.write_package_json(&out_dir, &None, true, Target::Bundler)
.unwrap();

let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
assert_eq!(pkg.keywords, None);
}

#[test]
fn it_does_not_error_when_wasm_bindgen_is_declared() {
let fixture = fixture::js_hello_world();
Expand Down
1 change: 1 addition & 0 deletions tests/all/utils/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub struct NpmPackage {
#[serde(default = "default_false", rename = "sideEffects")]
pub side_effects: bool,
pub homepage: Option<String>,
pub keywords: Option<Vec<String>>,
}

fn default_none() -> String {
Expand Down