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

modules modules modules #312

Merged
merged 13 commits into from
Sep 20, 2018
Merged
Show file tree
Hide file tree
Changes from 10 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
128 changes: 82 additions & 46 deletions src/manifest.rs → src/manifest/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
//! Reading and writing Cargo.toml and package.json manifests.

mod npm;

use std::collections::HashMap;
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;

use self::npm::{repository::Repository, CommonJSPackage, ESModulesPackage, NpmPackage};
use console::style;
use emoji;
use error::Error;
Expand Down Expand Up @@ -75,32 +78,6 @@ struct CargoLib {
crate_type: Option<Vec<String>>,
}

#[derive(Serialize)]
struct NpmPackage {
name: String,
#[serde(skip_serializing_if = "Vec::is_empty")]
collaborators: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
description: Option<String>,
version: String,
#[serde(skip_serializing_if = "Option::is_none")]
license: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
repository: Option<Repository>,
#[serde(skip_serializing_if = "Vec::is_empty")]
files: Vec<String>,
main: String,
#[serde(skip_serializing_if = "Option::is_none")]
types: Option<String>,
}

#[derive(Serialize)]
struct Repository {
#[serde(rename = "type")]
ty: String,
url: String,
}

fn read_cargo_toml(path: &Path) -> Result<CargoManifest, Error> {
let manifest_path = path.join("Cargo.toml");
if !manifest_path.is_file() {
Expand All @@ -120,7 +97,7 @@ fn read_cargo_toml(path: &Path) -> Result<CargoManifest, Error> {
}

impl CargoManifest {
fn into_npm(mut self, scope: &Option<String>, disable_dts: bool, target: &str) -> NpmPackage {
fn into_commonjs(mut self, scope: &Option<String>, disable_dts: bool) -> NpmPackage {
let filename = self.package.name.replace("-", "_");
let wasm_file = format!("{}_bg.wasm", filename);
let js_file = format!("{}.js", filename);
Expand All @@ -131,11 +108,7 @@ impl CargoManifest {
Some(format!("{}.d.ts", filename))
};

let js_bg_file = if target == "nodejs" {
Some(format!("{}_bg.js", filename))
} else {
None
};
let js_bg_file = Some(format!("{}_bg.js", filename));

if let Some(s) = scope {
self.package.name = format!("@{}/{}", s, self.package.name);
Expand All @@ -156,7 +129,13 @@ impl CargoManifest {
None => {}
}

NpmPackage {
check_optional_fields(
&self.package.description,
&self.package.repository,
&self.package.license,
);

NpmPackage::CommonJSPackage(CommonJSPackage {
name: self.package.name,
collaborators: self.package.authors,
description: self.package.description,
Expand All @@ -169,7 +148,53 @@ impl CargoManifest {
files: files,
main: js_file,
types: dts_file,
})
}

fn into_esmodules(mut self, scope: &Option<String>, disable_dts: bool) -> NpmPackage {
let filename = self.package.name.replace("-", "_");
let wasm_file = format!("{}_bg.wasm", filename);
let js_file = format!("{}.js", filename);

let dts_file = if disable_dts == true {
None
} else {
Some(format!("{}.d.ts", filename))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This dts logic looks duplicated with the above?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is all from master but yeah you are right i think, file an issue?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i guess i can try to fix it in this PR, might as well

};

if let Some(s) = scope {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This scope handling logic looks duplicated between this and the above?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is all from master but yeah you are right i think, file an issue?

self.package.name = format!("@{}/{}", s, self.package.name);
}
let mut files = vec![wasm_file, js_file.clone()];
Copy link
Member Author

@ashleygwilliams ashleygwilliams Sep 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this clone. i hate it but i also love not having a ton of lifetime parameters everywhere. i think it's fine but if we hate it let's talk about ways to make it as clean as possible.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this single allocation that isn't in a loop (or even within any code that we know to be hot) is a problem. 👍


match dts_file {
Some(ref dts_file) => {
files.push(dts_file.to_string());
}
None => {}
}

check_optional_fields(
&self.package.description,
&self.package.repository,
&self.package.license,
);

NpmPackage::ESModulesPackage(ESModulesPackage {
name: self.package.name,
collaborators: self.package.authors,
description: self.package.description,
version: self.package.version,
license: self.package.license,
repository: self.package.repository.map(|repo_url| Repository {
ty: "git".to_string(),
url: repo_url,
}),
files: files,
module: js_file,
types: dts_file,
sideEffects: "false".to_string(),
})
}
}

Expand All @@ -184,32 +209,43 @@ pub fn write_package_json(
) -> Result<(), Error> {
let msg = format!("{}Writing a package.json...", emoji::MEMO);

PBAR.step(step, &msg);
let pkg_file_path = out_dir.join("package.json");
let mut pkg_file = File::create(pkg_file_path)?;
let crate_data = read_cargo_toml(path)?;
let npm_data = if target == "nodejs" {
crate_data.into_commonjs(scope, disable_dts)
} else {
crate_data.into_esmodules(scope, disable_dts)
};

let npm_json = serde_json::to_string_pretty(&npm_data)?;
pkg_file.write_all(npm_json.as_bytes())?;
Ok(())
}

/// Check the data for missing fields and warn
pub fn check_optional_fields(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to have this be a function in the impl of the package type so that you can just call:

self.check_optional_fields();

rather than feeding it from three separate variables and avoiding possibly calling the function incorrectly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh! that's a great idea. can do :)

description: &Option<String>,
repository: &Option<String>,
license: &Option<String>,
) {
let warn_fmt = |field| {
format!(
"Field '{}' is missing from Cargo.toml. It is not necessary, but recommended",
field
)
};

PBAR.step(step, &msg);
let pkg_file_path = out_dir.join("package.json");
let mut pkg_file = File::create(pkg_file_path)?;
let crate_data = read_cargo_toml(path)?;
let npm_data = crate_data.into_npm(scope, disable_dts, target);

if npm_data.description.is_none() {
if description.is_none() {
PBAR.warn(&warn_fmt("description"));
}
if npm_data.repository.is_none() {
if repository.is_none() {
PBAR.warn(&warn_fmt("repository"));
}
if npm_data.license.is_none() {
if license.is_none() {
PBAR.warn(&warn_fmt("license"));
}

let npm_json = serde_json::to_string_pretty(&npm_data)?;
pkg_file.write_all(npm_json.as_bytes())?;
Ok(())
}

/// Get the crate name for the crate at the given path.
Expand Down
20 changes: 20 additions & 0 deletions src/manifest/npm/commonjs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use manifest::npm::repository::Repository;

#[derive(Serialize)]
pub struct CommonJSPackage {
pub name: String,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub collaborators: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
pub version: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub license: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub repository: Option<Repository>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub files: Vec<String>,
pub main: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub types: Option<String>,
}
21 changes: 21 additions & 0 deletions src/manifest/npm/esmodules.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use manifest::npm::repository::Repository;

#[derive(Serialize)]
pub struct ESModulesPackage {
pub name: String,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub collaborators: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
pub version: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub license: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub repository: Option<Repository>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub files: Vec<String>,
pub module: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub types: Option<String>,
pub sideEffects: String,
}
13 changes: 13 additions & 0 deletions src/manifest/npm/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
mod commonjs;
mod esmodules;
pub mod repository;

pub use self::commonjs::CommonJSPackage;
pub use self::esmodules::ESModulesPackage;

#[derive(Serialize)]
#[serde(untagged)]
pub enum NpmPackage {
CommonJSPackage(CommonJSPackage),
ESModulesPackage(ESModulesPackage),
}
6 changes: 6 additions & 0 deletions src/manifest/npm/repository.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#[derive(Serialize)]
pub struct Repository {
#[serde(rename = "type")]
pub ty: String,
pub url: String,
}
43 changes: 18 additions & 25 deletions tests/all/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,11 @@ fn it_creates_a_package_json_default_path() {
pkg.repository.url,
"https://github.com/ashleygwilliams/wasm-pack.git"
);
assert_eq!(pkg.main, "wasm_pack.js");
let types = pkg.types.unwrap_or_default();
assert_eq!(types, "wasm_pack.d.ts");
assert_eq!(pkg.module, "wasm_pack.js");
assert_eq!(pkg.types, "wasm_pack.d.ts");

let actual_files: HashSet<String> = pkg.files.into_iter().collect();
let expected_files: HashSet<String> = ["wasm_pack_bg.wasm", "wasm_pack.d.ts"]
let expected_files: HashSet<String> = ["wasm_pack_bg.wasm", "wasm_pack.d.ts", "wasm_pack.js"]
.iter()
.map(|&s| String::from(s))
.collect();
Expand All @@ -92,10 +91,14 @@ fn it_creates_a_package_json_provided_path() {
assert!(utils::manifest::read_package_json(&fixture.path, &out_dir).is_ok());
let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
assert_eq!(pkg.name, "js-hello-world");
assert_eq!(pkg.main, "js_hello_world.js");
assert_eq!(pkg.module, "js_hello_world.js");

let actual_files: HashSet<String> = pkg.files.into_iter().collect();
let expected_files: HashSet<String> = ["js_hello_world_bg.wasm", "js_hello_world.d.ts"]
let expected_files: HashSet<String> = [
"js_hello_world_bg.wasm",
"js_hello_world.d.ts",
"js_hello_world.js",
]
.iter()
.map(|&s| String::from(s))
.collect();
Expand Down Expand Up @@ -123,10 +126,14 @@ fn it_creates_a_package_json_provided_path_with_scope() {
assert!(utils::manifest::read_package_json(&fixture.path, &out_dir).is_ok());
let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
assert_eq!(pkg.name, "@test/scopes-hello-world");
assert_eq!(pkg.main, "scopes_hello_world.js");
assert_eq!(pkg.module, "scopes_hello_world.js");

let actual_files: HashSet<String> = pkg.files.into_iter().collect();
let expected_files: HashSet<String> = ["scopes_hello_world_bg.wasm", "scopes_hello_world.d.ts"]
let expected_files: HashSet<String> = [
"scopes_hello_world_bg.wasm",
"scopes_hello_world.d.ts",
"scopes_hello_world.js",
]
.iter()
.map(|&s| String::from(s))
.collect();
Expand Down Expand Up @@ -154,8 +161,7 @@ fn it_creates_a_pkg_json_with_correct_files_on_node() {
"https://github.com/ashleygwilliams/wasm-pack.git"
);
assert_eq!(pkg.main, "wasm_pack.js");
let types = pkg.types.unwrap_or_default();
assert_eq!(types, "wasm_pack.d.ts");
assert_eq!(pkg.types, "wasm_pack.d.ts");

let actual_files: HashSet<String> = pkg.files.into_iter().collect();
let expected_files: HashSet<String> =
Expand All @@ -177,19 +183,6 @@ fn it_creates_a_pkg_json_in_out_dir() {
let package_json_path = &fixture.path.join(&out_dir).join("package.json");
assert!(fs::metadata(package_json_path).is_ok());
assert!(utils::manifest::read_package_json(&fixture.path, &out_dir).is_ok());

let pkg = utils::manifest::read_package_json(&fixture.path, &out_dir).unwrap();
assert_eq!(pkg.name, "js-hello-world");
assert_eq!(pkg.main, "js_hello_world.js");

let actual_files: HashSet<String> = pkg.files.into_iter().collect();

let expected_files: HashSet<String> = ["js_hello_world_bg.wasm", "js_hello_world.d.ts"]
.iter()
.map(|&s| String::from(s))
.collect();

assert_eq!(actual_files, expected_files);
}

#[test]
Expand All @@ -209,10 +202,10 @@ fn it_creates_a_package_json_with_correct_keys_when_types_are_skipped() {
pkg.repository.url,
"https://github.com/ashleygwilliams/wasm-pack.git"
);
assert_eq!(pkg.main, "wasm_pack.js");
assert_eq!(pkg.module, "wasm_pack.js");

let actual_files: HashSet<String> = pkg.files.into_iter().collect();
let expected_files: HashSet<String> = ["wasm_pack_bg.wasm"]
let expected_files: HashSet<String> = ["wasm_pack_bg.wasm", "wasm_pack.js"]
.iter()
.map(|&s| String::from(s))
.collect();
Expand Down
10 changes: 9 additions & 1 deletion tests/all/utils/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,16 @@ pub struct NpmPackage {
pub license: String,
pub repository: Repository,
pub files: Vec<String>,
#[serde(default = "default_none")]
pub main: String,
pub types: Option<String>,
#[serde(default = "default_none")]
pub module: String,
#[serde(default = "default_none")]
pub types: String,
}

fn default_none() -> String {
"".to_string()
}

#[derive(Deserialize)]
Expand Down