diff --git a/src/command/build.rs b/src/command/build.rs index 4dd5d79..e683030 100644 --- a/src/command/build.rs +++ b/src/command/build.rs @@ -239,29 +239,29 @@ impl Build { step_add_wasm_target, step_build_wasm, step_create_dir, - step_create_json, step_copy_readme, step_copy_license, step_install_wasm_bindgen, step_run_wasm_bindgen, + step_create_json, ], BuildMode::Noinstall => steps![ step_check_rustc_version, step_check_crate_config, step_build_wasm, step_create_dir, - step_create_json, step_copy_readme, step_copy_license, - step_run_wasm_bindgen + step_run_wasm_bindgen, + step_create_json, ], BuildMode::Force => steps![ step_build_wasm, step_create_dir, - step_create_json, step_copy_readme, step_copy_license, - step_run_wasm_bindgen + step_run_wasm_bindgen, + step_create_json, ], } } diff --git a/src/manifest/mod.rs b/src/manifest/mod.rs index f61a5df..ab5ba74 100644 --- a/src/manifest/mod.rs +++ b/src/manifest/mod.rs @@ -383,11 +383,11 @@ impl CrateData { PBAR.step(step, &msg); let pkg_file_path = out_dir.join("package.json"); let npm_data = if target == "nodejs" { - self.to_commonjs(scope, disable_dts) + self.to_commonjs(scope, disable_dts, out_dir) } else if target == "no-modules" { - self.to_nomodules(scope, disable_dts) + self.to_nomodules(scope, disable_dts, out_dir) } else { - self.to_esmodules(scope, disable_dts) + self.to_esmodules(scope, disable_dts, out_dir) }; let npm_json = serde_json::to_string_pretty(&npm_data)?; @@ -401,6 +401,7 @@ impl CrateData { scope: &Option, include_commonjs_shim: bool, disable_dts: bool, + out_dir: &Path, ) -> NpmData { let crate_name = self.crate_name(); let wasm_file = format!("{}_bg.wasm", crate_name); @@ -426,6 +427,23 @@ impl CrateData { } else { None }; + + let readme_file = out_dir.join("README.md"); + if readme_file.is_file() { + files.push("README.md".to_string()); + } + + if let Ok(entries) = fs::read_dir(out_dir) { + let file_names = entries + .filter_map(|e| e.ok()) + .filter(|e| e.metadata().map(|m| m.is_file()).unwrap_or(false)) + .filter_map(|e| e.file_name().into_string().ok()) + .filter(|f| f.starts_with("LICENSE")); + for file_name in file_names { + files.push(file_name); + } + } + NpmData { name: npm_name, dts_file, @@ -444,8 +462,8 @@ impl CrateData { }) } - fn to_commonjs(&self, scope: &Option, disable_dts: bool) -> NpmPackage { - let data = self.npm_data(scope, true, disable_dts); + fn to_commonjs(&self, scope: &Option, disable_dts: bool, out_dir: &Path) -> NpmPackage { + let data = self.npm_data(scope, true, disable_dts, out_dir); let pkg = &self.data.packages[self.current_idx]; self.check_optional_fields(); @@ -472,8 +490,13 @@ impl CrateData { }) } - fn to_esmodules(&self, scope: &Option, disable_dts: bool) -> NpmPackage { - let data = self.npm_data(scope, false, disable_dts); + fn to_esmodules( + &self, + scope: &Option, + disable_dts: bool, + out_dir: &Path, + ) -> NpmPackage { + let data = self.npm_data(scope, false, disable_dts, out_dir); let pkg = &self.data.packages[self.current_idx]; self.check_optional_fields(); @@ -501,8 +524,13 @@ impl CrateData { }) } - fn to_nomodules(&self, scope: &Option, disable_dts: bool) -> NpmPackage { - let data = self.npm_data(scope, false, disable_dts); + fn to_nomodules( + &self, + scope: &Option, + disable_dts: bool, + out_dir: &Path, + ) -> NpmPackage { + let data = self.npm_data(scope, false, disable_dts, out_dir); let pkg = &self.data.packages[self.current_idx]; self.check_optional_fields(); diff --git a/tests/all/manifest.rs b/tests/all/manifest.rs index b47174b..7105794 100644 --- a/tests/all/manifest.rs +++ b/tests/all/manifest.rs @@ -5,7 +5,7 @@ use std::path::PathBuf; use structopt::StructOpt; use utils::{self, fixture}; -use wasm_pack::{self, manifest, Cli}; +use wasm_pack::{self, license, manifest, readme, Cli}; #[test] fn it_gets_the_crate_name_default_path() { @@ -425,3 +425,64 @@ fn parse_crate_data_returns_unused_keys_in_cargo_toml() { assert!(unused_keys.contains("package.metadata.wasm-pack.profile.production")); } + +#[test] +fn it_lists_license_files_in_files_field_of_package_json() { + let fixture = fixture::dual_license(); + let out_dir = fixture.path.join("pkg"); + + let crate_data = manifest::CrateData::new(&fixture.path).unwrap(); + + let step = wasm_pack::progressbar::Step::new(3); + wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); + license::copy_from_crate(&crate_data, &fixture.path, &out_dir, &step).unwrap(); + crate_data + .write_package_json(&out_dir, &None, false, "", &step) + .unwrap(); + + let package_json_path = &fixture.path.join("pkg").join("package.json"); + fs::metadata(package_json_path).unwrap(); + let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap(); + + assert!( + pkg.files.contains(&"LICENSE-WTFPL".to_string()), + "LICENSE-WTFPL is not in files: {:?}", + pkg.files, + ); + + assert!( + pkg.files.contains(&"LICENSE-MIT".to_string()), + "LICENSE-MIT is not in files: {:?}", + pkg.files, + ); +} + +#[test] +fn it_lists_readme_in_files_field_of_package_json() { + let fixture = utils::fixture::Fixture::new(); + fixture + .readme() + .hello_world_src_lib() + .cargo_toml("readme-test-for-package-json"); + + let out_dir = fixture.path.join("pkg"); + + let crate_data = manifest::CrateData::new(&fixture.path).unwrap(); + + let step = wasm_pack::progressbar::Step::new(3); + wasm_pack::command::utils::create_pkg_dir(&out_dir, &step).unwrap(); + readme::copy_from_crate(&fixture.path, &out_dir, &step).unwrap(); + crate_data + .write_package_json(&out_dir, &None, false, "", &step) + .unwrap(); + + let package_json_path = &fixture.path.join("pkg").join("package.json"); + fs::metadata(package_json_path).unwrap(); + let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap(); + + assert!( + pkg.files.contains(&"README.md".to_string()), + "README.md is not in files: {:?}", + pkg.files, + ); +}